@triflux/core 10.35.3 → 10.36.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/package.json +1 -1
- package/scripts/lib/stealth-fetch.mjs +176 -0
package/package.json
CHANGED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SSRF boundary: stealthFetch only accepts http: and https: URLs. It does not
|
|
3
|
+
// guard private IPs, metadata hosts such as 169.254.x.x, or localhost; callers
|
|
4
|
+
// must treat the URL as already inside their trust boundary.
|
|
5
|
+
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
|
|
8
|
+
export class CloakBrowserUnavailableError extends Error {}
|
|
9
|
+
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
11
|
+
const DEFAULT_WAIT_UNTIL = "load";
|
|
12
|
+
const SUPPORTED_PLATFORMS = new Set([
|
|
13
|
+
"linux:x64",
|
|
14
|
+
"linux:arm64",
|
|
15
|
+
"darwin:x64",
|
|
16
|
+
"darwin:arm64",
|
|
17
|
+
"win32:x64",
|
|
18
|
+
]);
|
|
19
|
+
const EXIT_CODES = {
|
|
20
|
+
not_installed: 3,
|
|
21
|
+
unsupported_platform: 4,
|
|
22
|
+
runtime_error: 5,
|
|
23
|
+
blocked_scheme: 6,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function isSupportedPlatform(platform, arch) {
|
|
27
|
+
return SUPPORTED_PLATFORMS.has(`${platform}:${arch}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isModuleNotFound(error) {
|
|
31
|
+
return (
|
|
32
|
+
error?.code === "ERR_MODULE_NOT_FOUND" || error?.code === "MODULE_NOT_FOUND"
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function htmlToText(html) {
|
|
37
|
+
return String(html || "")
|
|
38
|
+
.replace(/<script\b[^>]*>[\s\S]*?<\/script>/giu, " ")
|
|
39
|
+
.replace(/<style\b[^>]*>[\s\S]*?<\/style>/giu, " ")
|
|
40
|
+
.replace(/<[^>]+>/gu, " ")
|
|
41
|
+
.replace(/ /giu, " ")
|
|
42
|
+
.replace(/&/giu, "&")
|
|
43
|
+
.replace(/</giu, "<")
|
|
44
|
+
.replace(/>/giu, ">")
|
|
45
|
+
.replace(/"/giu, '"')
|
|
46
|
+
.replace(/'/gu, "'")
|
|
47
|
+
.replace(/\s+/gu, " ")
|
|
48
|
+
.trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function responseStatus(response) {
|
|
52
|
+
if (!response) return null;
|
|
53
|
+
if (typeof response.status === "function") return response.status();
|
|
54
|
+
return response.status ?? null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function responseUrl(response, fallbackUrl) {
|
|
58
|
+
if (!response) return fallbackUrl;
|
|
59
|
+
if (typeof response.url === "function") return response.url();
|
|
60
|
+
return response.url ?? fallbackUrl;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function parseAllowedFetchUrl(url) {
|
|
64
|
+
let parsed;
|
|
65
|
+
try {
|
|
66
|
+
parsed = new URL(url);
|
|
67
|
+
} catch {
|
|
68
|
+
return { ok: false, scheme: null };
|
|
69
|
+
}
|
|
70
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
71
|
+
return { ok: false, scheme: parsed.protocol };
|
|
72
|
+
}
|
|
73
|
+
return { ok: true, href: parsed.href };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function closeBrowser(browser) {
|
|
77
|
+
if (!browser || typeof browser.close !== "function") return;
|
|
78
|
+
try {
|
|
79
|
+
await browser.close();
|
|
80
|
+
} catch {
|
|
81
|
+
// best effort: fetch callers only need the primary result signal
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function stealthFetch(url, opts = {}) {
|
|
86
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
87
|
+
const waitUntil = opts.waitUntil ?? DEFAULT_WAIT_UNTIL;
|
|
88
|
+
const loadCloakBrowser = opts._import ?? (() => import("cloakbrowser"));
|
|
89
|
+
const platform = opts._platform ?? process.platform;
|
|
90
|
+
const arch = opts._arch ?? process.arch;
|
|
91
|
+
|
|
92
|
+
if (!isSupportedPlatform(platform, arch)) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
reason: "unsupported_platform",
|
|
96
|
+
platform,
|
|
97
|
+
arch,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const parsedUrl = parseAllowedFetchUrl(url);
|
|
102
|
+
if (!parsedUrl.ok) {
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
reason: "blocked_scheme",
|
|
106
|
+
scheme: parsedUrl.scheme,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let cloakbrowser;
|
|
111
|
+
try {
|
|
112
|
+
cloakbrowser = await loadCloakBrowser();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (isModuleNotFound(error)) return { ok: false, reason: "not_installed" };
|
|
115
|
+
return { ok: false, reason: "runtime_error", error: String(error) };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let browser;
|
|
119
|
+
try {
|
|
120
|
+
const launch = cloakbrowser?.launch;
|
|
121
|
+
if (typeof launch !== "function") {
|
|
122
|
+
throw new CloakBrowserUnavailableError("cloakbrowser launch unavailable");
|
|
123
|
+
}
|
|
124
|
+
browser = await launch();
|
|
125
|
+
const page = await browser.newPage();
|
|
126
|
+
const response = await page.goto(parsedUrl.href, {
|
|
127
|
+
waitUntil,
|
|
128
|
+
timeout: timeoutMs,
|
|
129
|
+
});
|
|
130
|
+
const html = await page.content();
|
|
131
|
+
return {
|
|
132
|
+
ok: true,
|
|
133
|
+
engine: "cloakbrowser",
|
|
134
|
+
url: parsedUrl.href,
|
|
135
|
+
finalUrl: responseUrl(response, parsedUrl.href),
|
|
136
|
+
status: responseStatus(response),
|
|
137
|
+
html,
|
|
138
|
+
text: htmlToText(html),
|
|
139
|
+
};
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return { ok: false, reason: "runtime_error", error: String(error) };
|
|
142
|
+
} finally {
|
|
143
|
+
await closeBrowser(browser);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function exitCodeFor(result) {
|
|
148
|
+
return EXIT_CODES[result?.reason] ?? 5;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function main(argv = process.argv) {
|
|
152
|
+
const url = argv[2];
|
|
153
|
+
if (!url) {
|
|
154
|
+
console.error("usage: stealth-fetch <url>");
|
|
155
|
+
process.exitCode = 2;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const result = await stealthFetch(url);
|
|
160
|
+
if (result.ok) {
|
|
161
|
+
console.log(JSON.stringify(result));
|
|
162
|
+
process.exitCode = 0;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.error(`[stealth-fetch] 폴백: ${result.reason}`);
|
|
167
|
+
console.log(JSON.stringify(result));
|
|
168
|
+
process.exitCode = exitCodeFor(result);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (
|
|
172
|
+
process.argv[1] &&
|
|
173
|
+
import.meta.url === pathToFileURL(process.argv[1]).href
|
|
174
|
+
) {
|
|
175
|
+
await main();
|
|
176
|
+
}
|