@ocap/util 1.30.25 → 1.30.26
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/esm/url.mjs +17 -8
- package/lib/url.cjs +17 -8
- package/package.json +2 -2
package/esm/url.mjs
CHANGED
|
@@ -4,20 +4,29 @@ import ipaddr from "ipaddr.js";
|
|
|
4
4
|
|
|
5
5
|
//#region src/url.ts
|
|
6
6
|
const MAX_URL_LENGTH = 256;
|
|
7
|
+
function stripTrailingDots(host) {
|
|
8
|
+
return host.replace(/\.+$/, "");
|
|
9
|
+
}
|
|
10
|
+
function stripIpv6Brackets(host) {
|
|
11
|
+
const trimmed = host.trim();
|
|
12
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) return trimmed.slice(1, -1);
|
|
13
|
+
return trimmed;
|
|
14
|
+
}
|
|
15
|
+
function normalizeHostname(host) {
|
|
16
|
+
return stripTrailingDots(stripIpv6Brackets(stripTrailingDots(host)));
|
|
17
|
+
}
|
|
7
18
|
function parseIp(host) {
|
|
8
|
-
|
|
19
|
+
const normalized = normalizeHostname(host);
|
|
20
|
+
if (!normalized || !ipaddr.isValid(normalized)) return null;
|
|
9
21
|
try {
|
|
10
|
-
return ipaddr.parse(
|
|
22
|
+
return ipaddr.parse(normalized);
|
|
11
23
|
} catch {
|
|
12
24
|
return null;
|
|
13
25
|
}
|
|
14
26
|
}
|
|
15
|
-
function stripTrailingDots(host) {
|
|
16
|
-
return host.replace(/\.+$/, "");
|
|
17
|
-
}
|
|
18
27
|
function isBlockedHostname(host, blockedHosts, blockedSuffixes) {
|
|
19
|
-
const normalized =
|
|
20
|
-
if (blockedHosts.some((entry) =>
|
|
28
|
+
const normalized = normalizeHostname(host).toLowerCase();
|
|
29
|
+
if (blockedHosts.some((entry) => normalizeHostname(entry).toLowerCase() === normalized)) return true;
|
|
21
30
|
if (blockedSuffixes.some((suffix) => {
|
|
22
31
|
const trimmed = stripTrailingDots(suffix).toLowerCase();
|
|
23
32
|
const clean = trimmed.replace(/^\.+/, "");
|
|
@@ -56,7 +65,7 @@ async function verifyUrl(url, options = {}) {
|
|
|
56
65
|
if (url.length > maxLength) throw new Error(`URL exceeds maximum length of ${maxLength} characters: ${url.length}`);
|
|
57
66
|
const { hostname, protocol } = new URL(url);
|
|
58
67
|
if (!protocols.includes(protocol)) throw new Error(`Protocol '${protocol}' is not allowed. Allowed protocols: ${protocols.join(", ")}`);
|
|
59
|
-
const normalizedHost =
|
|
68
|
+
const normalizedHost = normalizeHostname(hostname);
|
|
60
69
|
if (isBlockedHostname(normalizedHost, blockedHosts, blockedSuffixes)) throw new Error(`Hostname '${normalizedHost}' is blocked`);
|
|
61
70
|
const isIP = Boolean(parseIp(normalizedHost));
|
|
62
71
|
if (isIP) {
|
package/lib/url.cjs
CHANGED
|
@@ -6,20 +6,29 @@ ipaddr_js = require_rolldown_runtime.__toESM(ipaddr_js);
|
|
|
6
6
|
|
|
7
7
|
//#region src/url.ts
|
|
8
8
|
const MAX_URL_LENGTH = 256;
|
|
9
|
+
function stripTrailingDots(host) {
|
|
10
|
+
return host.replace(/\.+$/, "");
|
|
11
|
+
}
|
|
12
|
+
function stripIpv6Brackets(host) {
|
|
13
|
+
const trimmed = host.trim();
|
|
14
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) return trimmed.slice(1, -1);
|
|
15
|
+
return trimmed;
|
|
16
|
+
}
|
|
17
|
+
function normalizeHostname(host) {
|
|
18
|
+
return stripTrailingDots(stripIpv6Brackets(stripTrailingDots(host)));
|
|
19
|
+
}
|
|
9
20
|
function parseIp(host) {
|
|
10
|
-
|
|
21
|
+
const normalized = normalizeHostname(host);
|
|
22
|
+
if (!normalized || !ipaddr_js.default.isValid(normalized)) return null;
|
|
11
23
|
try {
|
|
12
|
-
return ipaddr_js.default.parse(
|
|
24
|
+
return ipaddr_js.default.parse(normalized);
|
|
13
25
|
} catch {
|
|
14
26
|
return null;
|
|
15
27
|
}
|
|
16
28
|
}
|
|
17
|
-
function stripTrailingDots(host) {
|
|
18
|
-
return host.replace(/\.+$/, "");
|
|
19
|
-
}
|
|
20
29
|
function isBlockedHostname(host, blockedHosts, blockedSuffixes) {
|
|
21
|
-
const normalized =
|
|
22
|
-
if (blockedHosts.some((entry) =>
|
|
30
|
+
const normalized = normalizeHostname(host).toLowerCase();
|
|
31
|
+
if (blockedHosts.some((entry) => normalizeHostname(entry).toLowerCase() === normalized)) return true;
|
|
23
32
|
if (blockedSuffixes.some((suffix) => {
|
|
24
33
|
const trimmed = stripTrailingDots(suffix).toLowerCase();
|
|
25
34
|
const clean = trimmed.replace(/^\.+/, "");
|
|
@@ -58,7 +67,7 @@ async function verifyUrl(url$1, options = {}) {
|
|
|
58
67
|
if (url$1.length > maxLength) throw new Error(`URL exceeds maximum length of ${maxLength} characters: ${url$1.length}`);
|
|
59
68
|
const { hostname, protocol } = new url.URL(url$1);
|
|
60
69
|
if (!protocols.includes(protocol)) throw new Error(`Protocol '${protocol}' is not allowed. Allowed protocols: ${protocols.join(", ")}`);
|
|
61
|
-
const normalizedHost =
|
|
70
|
+
const normalizedHost = normalizeHostname(hostname);
|
|
62
71
|
if (isBlockedHostname(normalizedHost, blockedHosts, blockedSuffixes)) throw new Error(`Hostname '${normalizedHost}' is blocked`);
|
|
63
72
|
const isIP = Boolean(parseIp(normalizedHost));
|
|
64
73
|
if (isIP) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/util",
|
|
3
|
-
"version": "1.30.
|
|
3
|
+
"version": "1.30.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "utils shared across multiple forge js libs, works in both node.js and browser",
|
|
6
6
|
"keywords": [
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@ocap/types": "1.30.
|
|
15
|
+
"@ocap/types": "1.30.26",
|
|
16
16
|
"@types/bn.js": "^5.2.0",
|
|
17
17
|
"base64-url": "^2.3.3",
|
|
18
18
|
"bn.js": "5.2.3",
|