@acepaste/ingress 0.0.0-stage → 0.8.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/LICENSE +105 -0
- package/README.md +15 -2
- package/dist/src/acepaste-alphabet.d.ts +26 -0
- package/dist/src/acepaste-alphabet.js +47 -0
- package/dist/src/acquire.d.ts +42 -0
- package/dist/src/acquire.js +271 -0
- package/dist/src/adapters.d.ts +30 -0
- package/dist/src/adapters.js +160 -0
- package/dist/src/character.d.ts +22 -0
- package/dist/src/character.js +291 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +75 -0
- package/dist/src/color.d.ts +26 -0
- package/dist/src/color.js +111 -0
- package/dist/src/engine.d.ts +131 -0
- package/dist/src/engine.js +715 -0
- package/dist/src/evidence.d.ts +21 -0
- package/dist/src/evidence.js +77 -0
- package/dist/src/index.d.ts +28 -0
- package/dist/src/index.js +20 -0
- package/dist/src/meaning.d.ts +28 -0
- package/dist/src/meaning.js +227 -0
- package/dist/src/netpolicy.d.ts +34 -0
- package/dist/src/netpolicy.js +122 -0
- package/dist/src/normalize.d.ts +34 -0
- package/dist/src/normalize.js +204 -0
- package/dist/src/policyfile.d.ts +2 -0
- package/dist/src/policyfile.js +50 -0
- package/dist/src/sandbox.d.ts +45 -0
- package/dist/src/sandbox.js +231 -0
- package/dist/src/script.d.ts +23 -0
- package/dist/src/script.js +220 -0
- package/dist/src/secrets.d.ts +24 -0
- package/dist/src/secrets.js +192 -0
- package/dist/src/summary.d.ts +40 -0
- package/dist/src/summary.js +62 -0
- package/dist/src/supplychain.d.ts +24 -0
- package/dist/src/supplychain.js +182 -0
- package/dist/src/taint.d.ts +37 -0
- package/dist/src/taint.js +39 -0
- package/dist/src/types.d.ts +202 -0
- package/dist/src/types.js +33 -0
- package/dist/src/visibility.d.ts +41 -0
- package/dist/src/visibility.js +424 -0
- package/package.json +64 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 ThinkWell Labs LLC
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @acepaste/ingress
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The AcePaste Ingress engine: a security gate between internet content and AI ingestion. Anything a model would receive that a human looking at the same source would not see is suspect by default.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { IngressEngine } from "@acepaste/ingress";
|
|
7
|
+
const ingress = new IngressEngine({ signingKey: process.env.INGRESS_SIGNING_KEY });
|
|
8
|
+
const r = await ingress.fetch("https://example.com/doc"); // or ingress.scan(text)
|
|
9
|
+
model.push(r.envelope); // clean text in a provenance envelope, or a refusal with the reason
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Verdicts: `clean` (delivered with a signed receipt), `suspect` (refused, human-overridable), `hostile` (refused, not overridable). Passes: acquire, policy, visibility, character (the production AcePaste invisible alphabet), script and supply-chain (underlight lineage), sandbox (optional headless Chromium), meaning, plugins.
|
|
13
|
+
|
|
14
|
+
CLI: `npx @acepaste/ingress https://example.com` or `ingress --text < page.html`.
|
|
15
|
+
|
|
16
|
+
Full documentation: https://github.com/sugarcypher/acepasteINGRESS
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AcePaste invisible-character alphabet — ported verbatim from the
|
|
3
|
+
* production tool (sugarcypher/ACE-ASTE-PRO, AcePasteBrowserExtension/
|
|
4
|
+
* cleaner.js, INVISIBLE_RE and INVISIBLE_SUPPL_RE). This is the authoritative
|
|
5
|
+
* definition of "hostile invisible character" shared by the web tool, the
|
|
6
|
+
* Chrome extension, and this gate, so a verdict here matches what AcePaste
|
|
7
|
+
* itself would strip. Keep it in sync with cleaner.js; do not fork it.
|
|
8
|
+
*
|
|
9
|
+
* Provenance: these ranges are AcePaste's, not Claude's reconstruction.
|
|
10
|
+
* The gate's script-aware exceptions (emoji/Indic/Arabic context for joiners,
|
|
11
|
+
* bidi marks inside right-to-left runs) live in character.ts and are applied
|
|
12
|
+
* on top of this set.
|
|
13
|
+
*/
|
|
14
|
+
/** Basic-plane hostile invisibles (cleaner.js INVISIBLE_RE). */
|
|
15
|
+
export declare const ACEPASTE_INVISIBLE_SOURCE: string;
|
|
16
|
+
/** Supplementary-plane hostile invisibles (cleaner.js INVISIBLE_SUPPL_RE): tag chars + variation selectors supplement. */
|
|
17
|
+
export declare const ACEPASTE_INVISIBLE_SUPPL_SOURCE = "[\\u{E0000}-\\u{E007F}\\u{E0100}-\\u{E01EF}]";
|
|
18
|
+
export declare const acepasteInvisible: () => RegExp;
|
|
19
|
+
export declare const acepasteInvisibleSuppl: () => RegExp;
|
|
20
|
+
/**
|
|
21
|
+
* Count hostile invisibles the way the AcePaste extension's page scanner does
|
|
22
|
+
* (scanner.js countHostile): the sum of matches of both sets. The gate uses
|
|
23
|
+
* this only for the receipt's parity note; its own findings come from the
|
|
24
|
+
* script-aware passes in character.ts.
|
|
25
|
+
*/
|
|
26
|
+
export declare function countHostileInvisibles(str: string): number;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AcePaste invisible-character alphabet — ported verbatim from the
|
|
3
|
+
* production tool (sugarcypher/ACE-ASTE-PRO, AcePasteBrowserExtension/
|
|
4
|
+
* cleaner.js, INVISIBLE_RE and INVISIBLE_SUPPL_RE). This is the authoritative
|
|
5
|
+
* definition of "hostile invisible character" shared by the web tool, the
|
|
6
|
+
* Chrome extension, and this gate, so a verdict here matches what AcePaste
|
|
7
|
+
* itself would strip. Keep it in sync with cleaner.js; do not fork it.
|
|
8
|
+
*
|
|
9
|
+
* Provenance: these ranges are AcePaste's, not Claude's reconstruction.
|
|
10
|
+
* The gate's script-aware exceptions (emoji/Indic/Arabic context for joiners,
|
|
11
|
+
* bidi marks inside right-to-left runs) live in character.ts and are applied
|
|
12
|
+
* on top of this set.
|
|
13
|
+
*/
|
|
14
|
+
/** Basic-plane hostile invisibles (cleaner.js INVISIBLE_RE). */
|
|
15
|
+
export const ACEPASTE_INVISIBLE_SOURCE = "[" +
|
|
16
|
+
"\\u00AD" + // SOFT HYPHEN
|
|
17
|
+
"\\u034F" + // COMBINING GRAPHEME JOINER
|
|
18
|
+
"\\u061C" + // ARABIC LETTER MARK
|
|
19
|
+
"\\u115F\\u1160" + // HANGUL FILLERS
|
|
20
|
+
"\\u17B4\\u17B5" + // KHMER INHERENT VOWELS
|
|
21
|
+
"\\u180E" + // MONGOLIAN VOWEL SEPARATOR
|
|
22
|
+
"\\u200B-\\u200F" + // ZERO WIDTH SPACE/NJ/J + LRM/RLM
|
|
23
|
+
"\\u202A-\\u202E" + // BIDI EMBEDDINGS / OVERRIDES (Trojan Source)
|
|
24
|
+
"\\u2060-\\u206F" + // WORD JOINER + general format chars
|
|
25
|
+
"\\u2800" + // BRAILLE PATTERN BLANK (looks like a space)
|
|
26
|
+
"\\u3164" + // HANGUL FILLER
|
|
27
|
+
"\\uFE00-\\uFE0F" + // VARIATION SELECTORS 1-16
|
|
28
|
+
"\\uFEFF" + // ZERO WIDTH NO-BREAK SPACE / BOM
|
|
29
|
+
"\\uFFA0" + // HALFWIDTH HANGUL FILLER
|
|
30
|
+
"]";
|
|
31
|
+
/** Supplementary-plane hostile invisibles (cleaner.js INVISIBLE_SUPPL_RE): tag chars + variation selectors supplement. */
|
|
32
|
+
export const ACEPASTE_INVISIBLE_SUPPL_SOURCE = "[\\u{E0000}-\\u{E007F}\\u{E0100}-\\u{E01EF}]";
|
|
33
|
+
export const acepasteInvisible = () => new RegExp(ACEPASTE_INVISIBLE_SOURCE, "g");
|
|
34
|
+
export const acepasteInvisibleSuppl = () => new RegExp(ACEPASTE_INVISIBLE_SUPPL_SOURCE, "gu");
|
|
35
|
+
/**
|
|
36
|
+
* Count hostile invisibles the way the AcePaste extension's page scanner does
|
|
37
|
+
* (scanner.js countHostile): the sum of matches of both sets. The gate uses
|
|
38
|
+
* this only for the receipt's parity note; its own findings come from the
|
|
39
|
+
* script-aware passes in character.ts.
|
|
40
|
+
*/
|
|
41
|
+
export function countHostileInvisibles(str) {
|
|
42
|
+
if (!str)
|
|
43
|
+
return 0;
|
|
44
|
+
const a = str.match(acepasteInvisible());
|
|
45
|
+
const b = str.match(acepasteInvisibleSuppl());
|
|
46
|
+
return (a ? a.length : 0) + (b ? b.length : 0);
|
|
47
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Acquire pass.
|
|
3
|
+
*
|
|
4
|
+
* Fetches a URL under one of two fingerprints. The "agent" fingerprint looks
|
|
5
|
+
* like what an automated fetch tool sends; the "human" fingerprint looks like
|
|
6
|
+
* a desktop browser. The cloaking pass compares the two.
|
|
7
|
+
*
|
|
8
|
+
* Every hop is checked against the network policy before connecting, and
|
|
9
|
+
* the connection is pinned to the addresses that were checked (DNS rebinding
|
|
10
|
+
* defense). Redirects are followed manually so each hop is checked.
|
|
11
|
+
*/
|
|
12
|
+
import { type Dispatcher } from "undici";
|
|
13
|
+
import { NetPolicyError, type NetPolicy } from "./netpolicy.js";
|
|
14
|
+
import type { AcquireResult, IngressConfig } from "./types.js";
|
|
15
|
+
/** the gate's own name, used when agentUserAgents is empty */
|
|
16
|
+
export declare const HONEST_USER_AGENT = "AcePasteIngress/0.8 (+https://acepaste.xyz/ingress)";
|
|
17
|
+
export declare const MAX_REDIRECTS = 5;
|
|
18
|
+
export declare class AcquireError extends Error {
|
|
19
|
+
readonly code: "unsupported_scheme" | "too_large" | "timeout" | "network" | "binary" | "too_many_redirects" | "policy";
|
|
20
|
+
readonly policyCode?: NetPolicyError["code"] | undefined;
|
|
21
|
+
constructor(message: string, code: "unsupported_scheme" | "too_large" | "timeout" | "network" | "binary" | "too_many_redirects" | "policy", policyCode?: NetPolicyError["code"] | undefined);
|
|
22
|
+
}
|
|
23
|
+
export type FetchLike = (url: string, init: RequestInit & {
|
|
24
|
+
dispatcher?: Dispatcher;
|
|
25
|
+
}) => Promise<Response>;
|
|
26
|
+
export interface AcquireOptions {
|
|
27
|
+
/** user-agent to send (agent fingerprint); defaults to the gate's own name */
|
|
28
|
+
userAgent?: string;
|
|
29
|
+
/** HTTP(S) proxy for this fetch */
|
|
30
|
+
proxyUrl?: string;
|
|
31
|
+
}
|
|
32
|
+
export declare function acquire(url: string, fingerprint: "human" | "agent", config: IngressConfig, policy: NetPolicy, fetchImpl?: FetchLike, resolver?: (host: string) => Promise<string[]>, opts?: AcquireOptions): Promise<AcquireResult>;
|
|
33
|
+
/**
|
|
34
|
+
* Human fetch in a real headless Chromium: real browser handshake, JavaScript on,
|
|
35
|
+
* a fresh profile. Every request the page makes is checked against the network
|
|
36
|
+
* policy first, so a page cannot use the browser to reach private addresses.
|
|
37
|
+
* Returns the document after scripts have run.
|
|
38
|
+
*/
|
|
39
|
+
export declare function acquireWithBrowser(url: string, config: IngressConfig, policy: NetPolicy, resolver?: (host: string) => Promise<string[]>, opts?: {
|
|
40
|
+
proxyUrl?: string;
|
|
41
|
+
executablePath?: string;
|
|
42
|
+
}): Promise<AcquireResult>;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Acquire pass.
|
|
3
|
+
*
|
|
4
|
+
* Fetches a URL under one of two fingerprints. The "agent" fingerprint looks
|
|
5
|
+
* like what an automated fetch tool sends; the "human" fingerprint looks like
|
|
6
|
+
* a desktop browser. The cloaking pass compares the two.
|
|
7
|
+
*
|
|
8
|
+
* Every hop is checked against the network policy before connecting, and
|
|
9
|
+
* the connection is pinned to the addresses that were checked (DNS rebinding
|
|
10
|
+
* defense). Redirects are followed manually so each hop is checked.
|
|
11
|
+
*/
|
|
12
|
+
import { Agent, ProxyAgent, fetch as undiciFetch } from "undici";
|
|
13
|
+
import { checkTarget, NetPolicyError } from "./netpolicy.js";
|
|
14
|
+
const HUMAN_HEADERS = {
|
|
15
|
+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
|
16
|
+
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
|
17
|
+
"accept-language": "en-US,en;q=0.9",
|
|
18
|
+
"sec-fetch-dest": "document",
|
|
19
|
+
"sec-fetch-mode": "navigate",
|
|
20
|
+
"sec-fetch-site": "none",
|
|
21
|
+
"sec-fetch-user": "?1",
|
|
22
|
+
"upgrade-insecure-requests": "1",
|
|
23
|
+
};
|
|
24
|
+
/** the gate's own name, used when agentUserAgents is empty */
|
|
25
|
+
export const HONEST_USER_AGENT = "AcePasteIngress/0.8 (+https://acepaste.xyz/ingress)";
|
|
26
|
+
const AGENT_HEADERS = {
|
|
27
|
+
"user-agent": HONEST_USER_AGENT,
|
|
28
|
+
accept: "text/html,application/xhtml+xml,text/markdown,text/plain;q=0.9,application/json;q=0.8,*/*;q=0.5",
|
|
29
|
+
};
|
|
30
|
+
export const MAX_REDIRECTS = 5;
|
|
31
|
+
export class AcquireError extends Error {
|
|
32
|
+
code;
|
|
33
|
+
policyCode;
|
|
34
|
+
constructor(message, code, policyCode) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.policyCode = policyCode;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** undici fetch pinned to pre-checked addresses */
|
|
41
|
+
function pinnedFetch(addrs) {
|
|
42
|
+
const agent = new Agent({
|
|
43
|
+
connect: {
|
|
44
|
+
lookup: (_host, _opts, cb) => cb(null, addrs.map((address) => ({ address, family: address.includes(":") ? 6 : 4 }))),
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
return (url, init) => undiciFetch(url, { ...init, dispatcher: agent });
|
|
48
|
+
}
|
|
49
|
+
/** undici fetch through an HTTP(S) proxy. The target was already checked against the network policy; the proxy sits outside our network, so it cannot be used to reach it. */
|
|
50
|
+
const proxyAgents = new Map();
|
|
51
|
+
function proxiedFetch(proxyUrl) {
|
|
52
|
+
let agent = proxyAgents.get(proxyUrl);
|
|
53
|
+
if (!agent) {
|
|
54
|
+
agent = new ProxyAgent(proxyUrl);
|
|
55
|
+
proxyAgents.set(proxyUrl, agent);
|
|
56
|
+
}
|
|
57
|
+
const a = agent;
|
|
58
|
+
return (url, init) => undiciFetch(url, { ...init, dispatcher: a });
|
|
59
|
+
}
|
|
60
|
+
export async function acquire(url, fingerprint, config, policy, fetchImpl, resolver, opts = {}) {
|
|
61
|
+
let current = new URL(url);
|
|
62
|
+
if (current.protocol !== "http:" && current.protocol !== "https:") {
|
|
63
|
+
throw new AcquireError(`unsupported scheme ${current.protocol}`, "unsupported_scheme");
|
|
64
|
+
}
|
|
65
|
+
const chain = [];
|
|
66
|
+
const headersIn = fingerprint === "human" ? HUMAN_HEADERS : { ...AGENT_HEADERS, ...(opts.userAgent ? { "user-agent": opts.userAgent } : {}) };
|
|
67
|
+
for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
|
|
68
|
+
let addrs;
|
|
69
|
+
try {
|
|
70
|
+
addrs = await checkTarget(current, policy, resolver);
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (e instanceof NetPolicyError)
|
|
74
|
+
throw new AcquireError(e.message, "policy", e.code);
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
const doFetch = fetchImpl ?? (opts.proxyUrl ? proxiedFetch(opts.proxyUrl) : pinnedFetch(addrs));
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
const timer = setTimeout(() => controller.abort(), config.fetchTimeoutMs);
|
|
80
|
+
let res;
|
|
81
|
+
try {
|
|
82
|
+
res = await doFetch(current.toString(), { headers: headersIn, redirect: "manual", signal: controller.signal });
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
const aborted = e.name === "AbortError";
|
|
87
|
+
throw new AcquireError(aborted ? "fetch timed out" : `network error: ${e.message}`, aborted ? "timeout" : "network");
|
|
88
|
+
}
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
if (res.status >= 300 && res.status < 400 && res.headers.get("location")) {
|
|
91
|
+
chain.push(current.toString());
|
|
92
|
+
let next;
|
|
93
|
+
try {
|
|
94
|
+
next = new URL(res.headers.get("location"), current);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
throw new AcquireError("bad redirect location", "network");
|
|
98
|
+
}
|
|
99
|
+
if (next.protocol !== "http:" && next.protocol !== "https:")
|
|
100
|
+
throw new AcquireError(`redirect to unsupported scheme ${next.protocol}`, "unsupported_scheme");
|
|
101
|
+
try {
|
|
102
|
+
await res.body?.cancel();
|
|
103
|
+
}
|
|
104
|
+
catch { /* ignore */ }
|
|
105
|
+
current = next;
|
|
106
|
+
if (hop === MAX_REDIRECTS)
|
|
107
|
+
throw new AcquireError(`more than ${MAX_REDIRECTS} redirects`, "too_many_redirects");
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
111
|
+
const lengthHeader = Number(res.headers.get("content-length") ?? "0");
|
|
112
|
+
if (lengthHeader > config.maxBodyBytes)
|
|
113
|
+
throw new AcquireError(`body ${lengthHeader} bytes exceeds limit`, "too_large");
|
|
114
|
+
if (contentType && !/^(text\/|application\/(xhtml\+xml|xml|json|ld\+json|javascript|x-javascript|markdown))/i.test(contentType)) {
|
|
115
|
+
throw new AcquireError(`binary or unsupported content-type ${contentType}`, "binary");
|
|
116
|
+
}
|
|
117
|
+
// stream with a hard cap so a lying content-length cannot exhaust memory
|
|
118
|
+
const reader = res.body?.getReader();
|
|
119
|
+
const chunks = [];
|
|
120
|
+
let total = 0;
|
|
121
|
+
if (reader) {
|
|
122
|
+
for (;;) {
|
|
123
|
+
const { done, value } = await reader.read();
|
|
124
|
+
if (done)
|
|
125
|
+
break;
|
|
126
|
+
total += value.byteLength;
|
|
127
|
+
if (total > config.maxBodyBytes) {
|
|
128
|
+
try {
|
|
129
|
+
await reader.cancel();
|
|
130
|
+
}
|
|
131
|
+
catch { /* ignore */ }
|
|
132
|
+
throw new AcquireError(`body exceeds ${config.maxBodyBytes} bytes`, "too_large");
|
|
133
|
+
}
|
|
134
|
+
chunks.push(value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const buf = new Uint8Array(total);
|
|
138
|
+
let off = 0;
|
|
139
|
+
for (const c of chunks) {
|
|
140
|
+
buf.set(c, off);
|
|
141
|
+
off += c.byteLength;
|
|
142
|
+
}
|
|
143
|
+
const body = new TextDecoder("utf-8", { fatal: false }).decode(buf);
|
|
144
|
+
const headers = {};
|
|
145
|
+
res.headers.forEach((v, k) => (headers[k] = v));
|
|
146
|
+
return {
|
|
147
|
+
url,
|
|
148
|
+
finalUrl: current.toString(),
|
|
149
|
+
redirectChain: chain,
|
|
150
|
+
status: res.status,
|
|
151
|
+
headers,
|
|
152
|
+
contentType,
|
|
153
|
+
body,
|
|
154
|
+
fingerprint,
|
|
155
|
+
userAgent: headersIn["user-agent"],
|
|
156
|
+
via: "http",
|
|
157
|
+
fetchedAt: new Date().toISOString(),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
throw new AcquireError("unreachable", "network");
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Human fetch in a real headless Chromium: real browser handshake, JavaScript on,
|
|
164
|
+
* a fresh profile. Every request the page makes is checked against the network
|
|
165
|
+
* policy first, so a page cannot use the browser to reach private addresses.
|
|
166
|
+
* Returns the document after scripts have run.
|
|
167
|
+
*/
|
|
168
|
+
export async function acquireWithBrowser(url, config, policy, resolver, opts = {}) {
|
|
169
|
+
const first = new URL(url);
|
|
170
|
+
if (first.protocol !== "http:" && first.protocol !== "https:")
|
|
171
|
+
throw new AcquireError(`unsupported scheme ${first.protocol}`, "unsupported_scheme");
|
|
172
|
+
try {
|
|
173
|
+
await checkTarget(first, policy, resolver);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
if (e instanceof NetPolicyError)
|
|
177
|
+
throw new AcquireError(e.message, "policy", e.code);
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
let pw;
|
|
181
|
+
try {
|
|
182
|
+
pw = await import("playwright");
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
throw new AcquireError("browser fetch needs playwright installed", "network");
|
|
186
|
+
}
|
|
187
|
+
let proxy;
|
|
188
|
+
if (opts.proxyUrl) {
|
|
189
|
+
const p = new URL(opts.proxyUrl);
|
|
190
|
+
proxy = { server: `${p.protocol}//${p.host}`, username: decodeURIComponent(p.username) || undefined, password: decodeURIComponent(p.password) || undefined };
|
|
191
|
+
}
|
|
192
|
+
let browser;
|
|
193
|
+
try {
|
|
194
|
+
browser = await pw.chromium.launch({
|
|
195
|
+
headless: true, proxy,
|
|
196
|
+
executablePath: opts.executablePath ?? process.env.INGRESS_CHROMIUM ?? undefined,
|
|
197
|
+
args: ["--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage", "--disable-extensions", "--no-first-run"],
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
throw new AcquireError(`browser launch failed: ${e.message.split("\n")[0]}`, "network");
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
// headless Chromium announces itself ("HeadlessChrome", navigator.webdriver); present as ordinary desktop Chrome
|
|
205
|
+
const ctx = await browser.newContext({ userAgent: HUMAN_HEADERS["user-agent"], viewport: { width: 1280, height: 900 }, locale: "en-US", storageState: { cookies: [], origins: [] } });
|
|
206
|
+
await ctx.addInitScript(() => {
|
|
207
|
+
Object.defineProperty(navigator, "webdriver", { get: () => false });
|
|
208
|
+
Object.defineProperty(navigator, "languages", { get: () => ["en-US", "en"] });
|
|
209
|
+
});
|
|
210
|
+
const checked = new Map();
|
|
211
|
+
await ctx.route("**/*", async (route) => {
|
|
212
|
+
let u;
|
|
213
|
+
try {
|
|
214
|
+
u = new URL(route.request().url());
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return route.abort("blockedbyclient");
|
|
218
|
+
}
|
|
219
|
+
if (u.protocol === "data:" || u.protocol === "blob:")
|
|
220
|
+
return route.continue();
|
|
221
|
+
if (u.protocol !== "http:" && u.protocol !== "https:")
|
|
222
|
+
return route.abort("blockedbyclient");
|
|
223
|
+
const key = `${u.protocol}//${u.host}`;
|
|
224
|
+
let ok = checked.get(key);
|
|
225
|
+
if (ok === undefined) {
|
|
226
|
+
try {
|
|
227
|
+
await checkTarget(u, policy, resolver);
|
|
228
|
+
ok = true;
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
ok = false;
|
|
232
|
+
}
|
|
233
|
+
checked.set(key, ok);
|
|
234
|
+
}
|
|
235
|
+
return ok ? route.continue() : route.abort("blockedbyclient");
|
|
236
|
+
});
|
|
237
|
+
const page = await ctx.newPage();
|
|
238
|
+
let status = 0;
|
|
239
|
+
let contentType = "";
|
|
240
|
+
try {
|
|
241
|
+
const res = await page.goto(url, { waitUntil: "domcontentloaded", timeout: config.fetchTimeoutMs });
|
|
242
|
+
status = res?.status() ?? 0;
|
|
243
|
+
contentType = res?.headers()["content-type"] ?? "";
|
|
244
|
+
await page.waitForTimeout(Math.min(1500, config.fetchTimeoutMs / 4));
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
const msg = e.message.split("\n")[0];
|
|
248
|
+
throw new AcquireError(/timeout/i.test(msg) ? "browser fetch timed out" : `network error: ${msg}`, /timeout/i.test(msg) ? "timeout" : "network");
|
|
249
|
+
}
|
|
250
|
+
const finalUrl = page.url();
|
|
251
|
+
try {
|
|
252
|
+
await checkTarget(new URL(finalUrl), policy, resolver);
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
if (e instanceof NetPolicyError)
|
|
256
|
+
throw new AcquireError(e.message, "policy", e.code);
|
|
257
|
+
throw e;
|
|
258
|
+
}
|
|
259
|
+
const body = await page.content();
|
|
260
|
+
if (body.length > config.maxBodyBytes)
|
|
261
|
+
throw new AcquireError(`body exceeds ${config.maxBodyBytes} bytes`, "too_large");
|
|
262
|
+
const userAgent = await page.evaluate(() => navigator.userAgent);
|
|
263
|
+
return {
|
|
264
|
+
url, finalUrl, redirectChain: finalUrl === url ? [] : [url], status, headers: { "content-type": contentType },
|
|
265
|
+
contentType: contentType || "text/html", body, fingerprint: "human", userAgent, via: "browser", fetchedAt: new Date().toISOString(),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
await browser.close().catch(() => { });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content adapters: turn any supported content kind into the common shape
|
|
3
|
+
* the passes consume (segments with visibility, scripts, decoded blobs).
|
|
4
|
+
*
|
|
5
|
+
* HTML goes through visibility.ts. Everything else comes through here.
|
|
6
|
+
*/
|
|
7
|
+
import type { ContentKind, Finding, TextSegment } from "./types.js";
|
|
8
|
+
export declare function detectKind(contentType: string, body: string, urlHint?: string): ContentKind;
|
|
9
|
+
/**
|
|
10
|
+
* Decode numeric and the common named entities so entity-smuggled
|
|
11
|
+
* characters (​ ​ ‍) reach the character pass as the
|
|
12
|
+
* characters they are. Returns the decoded text and the count of
|
|
13
|
+
* suspicious decodes (format controls, tags, selectors).
|
|
14
|
+
*/
|
|
15
|
+
export declare function decodeEntities(text: string): {
|
|
16
|
+
text: string;
|
|
17
|
+
suspicious: number;
|
|
18
|
+
sample: string;
|
|
19
|
+
};
|
|
20
|
+
export interface AdapterResult {
|
|
21
|
+
segments: TextSegment[];
|
|
22
|
+
findings: Finding[];
|
|
23
|
+
}
|
|
24
|
+
/** Markdown: comments and non-rendered constructs are hidden; rendered text is visible. */
|
|
25
|
+
export declare function adaptMarkdown(raw: string): AdapterResult;
|
|
26
|
+
/** JSON: every string value is a segment; keys hint at intent. Nothing is "hidden" in JSON, but long strings in unusual keys are attribute-like. */
|
|
27
|
+
export declare function adaptJson(raw: string): AdapterResult;
|
|
28
|
+
export declare function adaptText(raw: string): AdapterResult;
|
|
29
|
+
/** Plain JavaScript fetched directly (a module, a bundle): the whole thing is a script; any string literals are visible text. */
|
|
30
|
+
export declare function adaptJavascript(raw: string): AdapterResult;
|