Skip to content

Commit

Permalink
[pac-resolver] Remove ip dependency (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-urbanas-qatalog committed Feb 12, 2024
1 parent e7e0e56 commit a954da3
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
@@ -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
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
@@ -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
@@ -1,4 +1,4 @@
import ip from 'ip';
import { ip } from './ip';
import net, { AddressInfo } from 'net';

/**
Expand Down

0 comments on commit a954da3

Please sign in to comment.