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

[pac-resolver] Remove ip dependency #281

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
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
Loading