Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(backend): replace private-ip with ipaddr.js #11041

Merged
merged 2 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"hpagent": "1.2.0",
"ioredis": "5.3.2",
"ip-cidr": "3.1.0",
"ipaddr.js": "2.1.0",
"is-svg": "4.3.2",
"js-yaml": "4.1.0",
"jsdom": "22.1.0",
Expand All @@ -120,7 +121,6 @@
"otpauth": "9.1.2",
"parse5": "7.1.2",
"pg": "8.11.0",
"private-ip": "3.0.0",
"probe-image-size": "7.2.3",
"promise-limit": "2.7.0",
"pug": "3.0.2",
Expand Down
18 changes: 9 additions & 9 deletions packages/backend/src/core/DownloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as fs from 'node:fs';
import * as stream from 'node:stream';
import * as util from 'node:util';
import { Inject, Injectable } from '@nestjs/common';
import IPCIDR from 'ip-cidr';
import PrivateIp from 'private-ip';
import ipaddr from 'ipaddr.js';
import chalk from 'chalk';
import got, * as Got from 'got';
import { parse } from 'content-disposition';
Expand Down Expand Up @@ -123,15 +122,15 @@ export class DownloadService {
public async downloadTextFile(url: string): Promise<string> {
// Create temp file
const [path, cleanup] = await createTemp();

this.logger.info(`text file: Temp file is ${path}`);

try {
// write content at URL to temp file
await this.downloadUrl(url, path);

const text = await util.promisify(fs.readFile)(path, 'utf8');

return text;
} finally {
cleanup();
Expand All @@ -140,13 +139,14 @@ export class DownloadService {

@bindThis
private isPrivateIp(ip: string): boolean {
const parsedIp = ipaddr.parse(ip);

for (const net of this.config.allowedPrivateNetworks ?? []) {
const cidr = new IPCIDR(net);
if (cidr.contains(ip)) {
if (parsedIp.match(ipaddr.parseCIDR(net))) {
return false;
}
}

return PrivateIp(ip) ?? false;
return parsedIp.range() !== 'unicast';
}
}
2 changes: 1 addition & 1 deletion packages/backend/src/misc/get-ip-hash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IPCIDR from 'ip-cidr';

export function getIpHash(ip: string) {
export function getIpHash(ip: string): string {
try {
// because a single person may control many IPv6 addresses,
// only a /64 subnet prefix of any IP will be taken into account.
Expand Down
31 changes: 6 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.