Skip to content

Commit

Permalink
fix(proxy-agent): ditch ip npm dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-urbanas-qatalog committed Feb 9, 2024
1 parent c881a18 commit f1b4210
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-planets-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pac-resolver': patch
---

fix [GHSA-78xj-cgh5-2h22](https://github.com/advisories/GHSA-78xj-cgh5-2h22) vulnerability
2 changes: 0 additions & 2 deletions packages/pac-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
],
"dependencies": {
"degenerator": "^5.0.0",
"ip": "^1.1.8",
"netmask": "^2.0.2"
},
"devDependencies": {
"@tootallnate/quickjs-emscripten": "^0.23.0",
"@types/ip": "^1.1.0",
"@types/jest": "^29.5.2",
"@types/netmask": "^1.0.30",
"@types/node": "^14.18.52",
Expand Down
57 changes: 57 additions & 0 deletions packages/pac-resolver/src/ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os from 'os';

export const ip = {
address(): string {
const interfaces = os.networkInterfaces();

// Default to `ipv4`
const family = normalizeFamily();

const all = Object.values(interfaces).map((addrs = []) => {
const addresses = addrs.filter((details) => {
const detailsFamily = normalizeFamily(details.family);
if (detailsFamily !== family || ip.isLoopback(details.address)) {
return false;
}
return true;

});

return addresses.length ? addresses[0].address : undefined;
}).filter(Boolean);

return !all.length ? ip.loopback(family) : all[0] as string;
},

isLoopback(addr: string): boolean {
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/
.test(addr)
|| /^fe80::1$/.test(addr)
|| /^::1$/.test(addr)
|| /^::$/.test(addr);
},

loopback(family: IpFamily): string {
// Default to `ipv4`
family = normalizeFamily(family);

if (family !== 'ipv4' && family !== 'ipv6') {
throw new Error('family must be ipv4 or ipv6');
}

return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
}

};

function normalizeFamily(family?: unknown): IpFamily {
if (family === 4) {
return 'ipv4';
}
if (family === 6) {
return 'ipv6';
}
return family ? (family as string).toLowerCase() as IpFamily : 'ipv4';
}

type IpFamily = 'ipv4' | 'ipv6'
2 changes: 1 addition & 1 deletion packages/pac-resolver/src/myIpAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ip from 'ip';
import { ip } from './ip';
import net, { AddressInfo } from 'net';

/**
Expand Down
Loading

0 comments on commit f1b4210

Please sign in to comment.