Skip to content

Commit

Permalink
refactor(backend): replace private-ip with ipaddr.js
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Jun 24, 2023
1 parent 5d922e3 commit b532310
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 63 deletions.
3 changes: 1 addition & 2 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"happy-dom": "9.20.3",
"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 +120,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';
}
}
58 changes: 6 additions & 52 deletions pnpm-lock.yaml

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

0 comments on commit b532310

Please sign in to comment.