@tak-ps/node-safeurl 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/doc.yml +45 -0
- package/.github/workflows/release.yml +40 -0
- package/.github/workflows/test.yml +46 -0
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +116 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +116 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/safeurl.ts.html +430 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +178 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/safeurl.ts.html +430 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/tmp/coverage-3190593-1780593894816-0.json +1 -0
- package/coverage/tmp/coverage-3190594-1780593894481-0.json +1 -0
- package/coverage/tmp/coverage-3190618-1780593894774-1.json +1 -0
- package/coverage/tmp/coverage-3190618-1780593894787-0.json +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/safeurl.d.ts +15 -0
- package/dist/lib/safeurl.js +106 -0
- package/dist/lib/safeurl.js.map +1 -0
- package/dist/package.json +54 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.js +15 -0
- package/index.ts +1 -0
- package/lib/safeurl.ts +118 -0
- package/package.json +54 -0
- package/tsconfig.json +24 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isSafeUrl, isPrivateIPv4, isPrivateIPv6 } from './lib/safeurl.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true for IPv4 addresses that fall in private / special-purpose ranges.
|
|
3
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isPrivateIPv4(hostname: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Returns true for IPv6 addresses that fall in private / special-purpose ranges.
|
|
8
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isPrivateIPv6(address: string): boolean;
|
|
11
|
+
export declare function isSafeUrl(href: string): Promise<{
|
|
12
|
+
safe: boolean;
|
|
13
|
+
url?: URL;
|
|
14
|
+
reason?: string;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { lookup } from 'node:dns/promises';
|
|
2
|
+
import { IPAddressRanges } from '@microsoft/antissrf';
|
|
3
|
+
import ipaddr from 'ipaddr.js';
|
|
4
|
+
const blocked = IPAddressRanges.recommendedLatest
|
|
5
|
+
.map((cidr) => {
|
|
6
|
+
try {
|
|
7
|
+
const r = ipaddr.parseCIDR(cidr);
|
|
8
|
+
return { cidr, range: r };
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
.filter((e) => e !== null);
|
|
15
|
+
/** Returns true when `address` matches any blocked (private/special-purpose) CIDR. */
|
|
16
|
+
function isBlockedIP(address) {
|
|
17
|
+
let parsed;
|
|
18
|
+
try {
|
|
19
|
+
parsed = ipaddr.parse(address);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
// If the address is an IPv4-mapped IPv6 (::ffff:x.x.x.x), unwrap to IPv4
|
|
25
|
+
// so it can be checked against IPv4 CIDR ranges.
|
|
26
|
+
if (parsed.kind() === 'ipv6' && parsed.isIPv4MappedAddress()) {
|
|
27
|
+
parsed = parsed.toIPv4Address();
|
|
28
|
+
}
|
|
29
|
+
for (const entry of blocked) {
|
|
30
|
+
// Only compare within the same address family
|
|
31
|
+
if (entry.range[0].kind() !== parsed.kind())
|
|
32
|
+
continue;
|
|
33
|
+
if (parsed.match(entry.range))
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns true for IPv4 addresses that fall in private / special-purpose ranges.
|
|
40
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
41
|
+
*/
|
|
42
|
+
export function isPrivateIPv4(hostname) {
|
|
43
|
+
// Must be exactly four segments; each must be non-empty, numeric, and within 0–255.
|
|
44
|
+
// Number() normalizes whitespace-padded and zero-prefixed strings (e.g. "01" → 1,
|
|
45
|
+
// " 10 " → 10), which handles forms that ipaddr.parse() would otherwise reject.
|
|
46
|
+
const segments = hostname.split('.');
|
|
47
|
+
if (segments.length !== 4)
|
|
48
|
+
return false;
|
|
49
|
+
const parts = segments.map(Number);
|
|
50
|
+
if (parts.some((p, i) => segments[i].trim() === '' || Number.isNaN(p) || p < 0 || p > 255))
|
|
51
|
+
return false;
|
|
52
|
+
return isBlockedIP(parts.join('.'));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns true for IPv6 addresses that fall in private / special-purpose ranges.
|
|
56
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
57
|
+
*/
|
|
58
|
+
export function isPrivateIPv6(address) {
|
|
59
|
+
// Strip zone ID (e.g. %eth0) and normalise to lowercase
|
|
60
|
+
const addr = address.toLowerCase().split('%')[0];
|
|
61
|
+
if (!addr.includes(':'))
|
|
62
|
+
return false;
|
|
63
|
+
return isBlockedIP(addr);
|
|
64
|
+
}
|
|
65
|
+
export async function isSafeUrl(href) {
|
|
66
|
+
let url;
|
|
67
|
+
try {
|
|
68
|
+
url = new URL(href);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return { safe: false, reason: `invalid URL: ${href}` };
|
|
72
|
+
}
|
|
73
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
74
|
+
return { safe: false, url, reason: `unsupported protocol: ${url.protocol}` };
|
|
75
|
+
}
|
|
76
|
+
// Strip IPv6 brackets and any trailing dot (trailing dot is valid per DNS but bypasses
|
|
77
|
+
// literal hostname checks — e.g. "localhost." has the same meaning as "localhost").
|
|
78
|
+
const hostname = url.hostname.toLowerCase().replace(/^\[|\]$/g, '').replace(/\.$/, '');
|
|
79
|
+
// Block known-bad hostname literals, including all subdomains of localhost
|
|
80
|
+
// (modern OS resolvers route *.localhost to 127.0.0.1).
|
|
81
|
+
if (hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '0.0.0.0') {
|
|
82
|
+
return { safe: false, url, reason: `blocked hostname: ${hostname}` };
|
|
83
|
+
}
|
|
84
|
+
// Block private / special-purpose IP literals via the antissrf block list.
|
|
85
|
+
// This catches addresses like 127.0.0.1, 10.x.x.x, 192.168.x.x, fc00::, ::1, etc.
|
|
86
|
+
if (isBlockedIP(hostname)) {
|
|
87
|
+
return { safe: false, url, reason: `blocked IP address: ${hostname}` };
|
|
88
|
+
}
|
|
89
|
+
// Resolve the hostname via DNS and reject any result that maps to a private address.
|
|
90
|
+
// This guards against SSRF via public-looking hostnames that resolve to internal IPs.
|
|
91
|
+
// Fail open on DNS errors so that unreachable-but-legitimate hosts are not silently
|
|
92
|
+
// blocked; the subsequent fetch will surface any connectivity issues on its own.
|
|
93
|
+
try {
|
|
94
|
+
const records = await lookup(hostname, { all: true });
|
|
95
|
+
for (const { address } of records) {
|
|
96
|
+
if (isBlockedIP(address)) {
|
|
97
|
+
return { safe: false, url, reason: `hostname resolves to blocked IP: ${address}` };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// DNS lookup failed (NXDOMAIN, no network) — allow and let the fetch fail
|
|
103
|
+
}
|
|
104
|
+
return { safe: true, url };
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=safeurl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safeurl.js","sourceRoot":"","sources":["../../lib/safeurl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,MAAM,MAAM,WAAW,CAAC;AAW/B,MAAM,OAAO,GAAiB,eAAe,CAAC,iBAAiB;KAC1D,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;IACV,IAAI,CAAC;QACD,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QACjC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC,CAAC;KACD,MAAM,CAAC,CAAC,CAAC,EAAmB,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;AAEhD,sFAAsF;AACtF,SAAS,WAAW,CAAC,OAAe;IAChC,IAAI,MAAiC,CAAC;IACtC,IAAI,CAAC;QACD,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,yEAAyE;IACzE,iDAAiD;I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BAAuB,QAAQ,EAAE,EAAE,CAAC;IAC3E,CAAC;IAED,qFAAqF;IACrF,sFAAsF;IACtF,oFAAoF;IACpF,iFAAiF;IACjF,IAAI,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QACtD,KAAK,MAAM,EAAE,OAAO,EAAE,IAAI,OAAO,EAAE,CAAC;YAChC,IAAI,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;gBACvB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,oCAAoC,OAAO,EAAE,EAAE,CAAC;YACvF,CAAC;QACL,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACL,0EAA0E;IAC9E,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tak-ps/node-safeurl",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "SSRF-safe URL validation library for Node.js",
|
|
6
|
+
"author": "Nick Ingalls <nick@ingalls.ca>",
|
|
7
|
+
"types": "index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "c8 --reporter=lcov --reporter html tsx --test test/*.ts",
|
|
16
|
+
"lint": "eslint *.ts lib/ test/",
|
|
17
|
+
"doc": "typedoc index.ts",
|
|
18
|
+
"build": "tsc --build && cp package.json dist/",
|
|
19
|
+
"pretest": "npm run lint"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">= 22"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@microsoft/antissrf": "^1.0.0",
|
|
26
|
+
"ipaddr.js": "^2.4.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@eslint/js": "^10.0.1",
|
|
30
|
+
"@types/node": "^25.0.0",
|
|
31
|
+
"c8": "^11.0.0",
|
|
32
|
+
"eslint": "^10.0.0",
|
|
33
|
+
"tsx": "^4.20.3",
|
|
34
|
+
"typedoc": "^0.28.0",
|
|
35
|
+
"typescript": "^6.0.0",
|
|
36
|
+
"typescript-eslint": "^8.0.0"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/dfpc-coe/node-safeurl.git"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"ssrf",
|
|
44
|
+
"url",
|
|
45
|
+
"validation",
|
|
46
|
+
"security",
|
|
47
|
+
"safe-url"
|
|
48
|
+
],
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/dfpc-coe/node-safeurl/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/dfpc-coe/node-safeurl#readme"
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../index.ts","../test/safeurl.test.ts","../lib/safeurl.ts"],"version":"6.0.3"}
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import eslint from '@eslint/js';
|
|
2
|
+
import tseslint from 'typescript-eslint';
|
|
3
|
+
|
|
4
|
+
export default tseslint.config(
|
|
5
|
+
{
|
|
6
|
+
ignores: ['dist/**', 'docs/**', 'coverage/**']
|
|
7
|
+
},
|
|
8
|
+
eslint.configs.recommended,
|
|
9
|
+
...tseslint.configs.recommended,
|
|
10
|
+
{
|
|
11
|
+
"rules": {
|
|
12
|
+
"@typescript-eslint/no-explicit-any": "warn"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
);
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isSafeUrl, isPrivateIPv4, isPrivateIPv6 } from './lib/safeurl.js';
|
package/lib/safeurl.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { lookup } from 'node:dns/promises';
|
|
2
|
+
import { IPAddressRanges } from '@microsoft/antissrf';
|
|
3
|
+
import ipaddr from 'ipaddr.js';
|
|
4
|
+
|
|
5
|
+
// Pre-built (CIDR, parsed-range) pairs from Microsoft's maintained SSRF-prevention
|
|
6
|
+
// IP address database. Covers loopback, RFC 1918, link-local, CGNAT, ULA,
|
|
7
|
+
// multicast, and all other special-purpose address blocks.
|
|
8
|
+
// Kept current by updating @microsoft/antissrf.
|
|
9
|
+
interface BlockEntry {
|
|
10
|
+
cidr: string;
|
|
11
|
+
range: [ipaddr.IPv4 | ipaddr.IPv6, number];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const blocked: BlockEntry[] = IPAddressRanges.recommendedLatest
|
|
15
|
+
.map((cidr) => {
|
|
16
|
+
try {
|
|
17
|
+
const r = ipaddr.parseCIDR(cidr);
|
|
18
|
+
return { cidr, range: r };
|
|
19
|
+
} catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
.filter((e): e is BlockEntry => e !== null);
|
|
24
|
+
|
|
25
|
+
/** Returns true when `address` matches any blocked (private/special-purpose) CIDR. */
|
|
26
|
+
function isBlockedIP(address: string): boolean {
|
|
27
|
+
let parsed: ipaddr.IPv4 | ipaddr.IPv6;
|
|
28
|
+
try {
|
|
29
|
+
parsed = ipaddr.parse(address);
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// If the address is an IPv4-mapped IPv6 (::ffff:x.x.x.x), unwrap to IPv4
|
|
35
|
+
// so it can be checked against IPv4 CIDR ranges.
|
|
36
|
+
if (parsed.kind() === 'ipv6' && (parsed as ipaddr.IPv6).isIPv4MappedAddress()) {
|
|
37
|
+
parsed = (parsed as ipaddr.IPv6).toIPv4Address();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for (const entry of blocked) {
|
|
41
|
+
// Only compare within the same address family
|
|
42
|
+
if (entry.range[0].kind() !== parsed.kind()) continue;
|
|
43
|
+
if (parsed.match(entry.range)) return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns true for IPv4 addresses that fall in private / special-purpose ranges.
|
|
50
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
51
|
+
*/
|
|
52
|
+
export function isPrivateIPv4(hostname: string): boolean {
|
|
53
|
+
// Must be exactly four segments; each must be non-empty, numeric, and within 0–255.
|
|
54
|
+
// Number() normalizes whitespace-padded and zero-prefixed strings (e.g. "01" → 1,
|
|
55
|
+
// " 10 " → 10), which handles forms that ipaddr.parse() would otherwise reject.
|
|
56
|
+
const segments = hostname.split('.');
|
|
57
|
+
if (segments.length !== 4) return false;
|
|
58
|
+
const parts = segments.map(Number);
|
|
59
|
+
if (parts.some((p, i) => segments[i].trim() === '' || Number.isNaN(p) || p < 0 || p > 255)) return false;
|
|
60
|
+
return isBlockedIP(parts.join('.'));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns true for IPv6 addresses that fall in private / special-purpose ranges.
|
|
65
|
+
* Delegates to `@microsoft/antissrf` `IPAddressRanges.recommendedLatest`.
|
|
66
|
+
*/
|
|
67
|
+
export function isPrivateIPv6(address: string): boolean {
|
|
68
|
+
// Strip zone ID (e.g. %eth0) and normalise to lowercase
|
|
69
|
+
const addr = address.toLowerCase().split('%')[0];
|
|
70
|
+
if (!addr.includes(':')) return false;
|
|
71
|
+
return isBlockedIP(addr);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function isSafeUrl(href: string): Promise<{ safe: boolean; url?: URL; reason?: string }> {
|
|
75
|
+
let url: URL;
|
|
76
|
+
try {
|
|
77
|
+
url = new URL(href);
|
|
78
|
+
} catch {
|
|
79
|
+
return { safe: false, reason: `invalid URL: ${href}` };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
83
|
+
return { safe: false, url, reason: `unsupported protocol: ${url.protocol}` };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Strip IPv6 brackets and any trailing dot (trailing dot is valid per DNS but bypasses
|
|
87
|
+
// literal hostname checks — e.g. "localhost." has the same meaning as "localhost").
|
|
88
|
+
const hostname = url.hostname.toLowerCase().replace(/^\[|\]$/g, '').replace(/\.$/, '');
|
|
89
|
+
|
|
90
|
+
// Block known-bad hostname literals, including all subdomains of localhost
|
|
91
|
+
// (modern OS resolvers route *.localhost to 127.0.0.1).
|
|
92
|
+
if (hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '0.0.0.0') {
|
|
93
|
+
return { safe: false, url, reason: `blocked hostname: ${hostname}` };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Block private / special-purpose IP literals via the antissrf block list.
|
|
97
|
+
// This catches addresses like 127.0.0.1, 10.x.x.x, 192.168.x.x, fc00::, ::1, etc.
|
|
98
|
+
if (isBlockedIP(hostname)) {
|
|
99
|
+
return { safe: false, url, reason: `blocked IP address: ${hostname}` };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Resolve the hostname via DNS and reject any result that maps to a private address.
|
|
103
|
+
// This guards against SSRF via public-looking hostnames that resolve to internal IPs.
|
|
104
|
+
// Fail open on DNS errors so that unreachable-but-legitimate hosts are not silently
|
|
105
|
+
// blocked; the subsequent fetch will surface any connectivity issues on its own.
|
|
106
|
+
try {
|
|
107
|
+
const records = await lookup(hostname, { all: true });
|
|
108
|
+
for (const { address } of records) {
|
|
109
|
+
if (isBlockedIP(address)) {
|
|
110
|
+
return { safe: false, url, reason: `hostname resolves to blocked IP: ${address}` };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
// DNS lookup failed (NXDOMAIN, no network) — allow and let the fetch fail
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { safe: true, url };
|
|
118
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tak-ps/node-safeurl",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "SSRF-safe URL validation library for Node.js",
|
|
6
|
+
"author": "Nick Ingalls <nick@ingalls.ca>",
|
|
7
|
+
"types": "index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "c8 --reporter=lcov --reporter html tsx --test test/*.ts",
|
|
16
|
+
"lint": "eslint *.ts lib/ test/",
|
|
17
|
+
"doc": "typedoc index.ts",
|
|
18
|
+
"build": "tsc --build && cp package.json dist/",
|
|
19
|
+
"pretest": "npm run lint"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">= 22"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@microsoft/antissrf": "^1.0.0",
|
|
26
|
+
"ipaddr.js": "^2.4.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@eslint/js": "^10.0.1",
|
|
30
|
+
"@types/node": "^25.0.0",
|
|
31
|
+
"c8": "^11.0.0",
|
|
32
|
+
"eslint": "^10.0.0",
|
|
33
|
+
"tsx": "^4.20.3",
|
|
34
|
+
"typedoc": "^0.28.0",
|
|
35
|
+
"typescript": "^6.0.0",
|
|
36
|
+
"typescript-eslint": "^8.0.0"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/dfpc-coe/node-safeurl.git"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"ssrf",
|
|
44
|
+
"url",
|
|
45
|
+
"validation",
|
|
46
|
+
"security",
|
|
47
|
+
"safe-url"
|
|
48
|
+
],
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/dfpc-coe/node-safeurl/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/dfpc-coe/node-safeurl#readme"
|
|
54
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": true,
|
|
4
|
+
"strictNullChecks": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"module": "es2022",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"target": "es2022",
|
|
9
|
+
"lib": ["es2022", "dom"],
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"noImplicitAny": true,
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"types": ["node"],
|
|
17
|
+
"outDir": "dist"
|
|
18
|
+
},
|
|
19
|
+
"include": [
|
|
20
|
+
"index.ts",
|
|
21
|
+
"test/**/*",
|
|
22
|
+
"lib/*.ts"
|
|
23
|
+
]
|
|
24
|
+
}
|