reguard 0.7.1 → 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/README.md +51 -31
- package/dist/chunks/chunk-3AYS7WSY.js +350 -0
- package/dist/chunks/chunk-KNKCUJL3.js +969 -0
- package/dist/chunks/{chunk-W56LN2M5.js → chunk-PA46KSOR.js} +385 -33
- package/dist/chunks/chunk-YDDBWUPV.js +438 -0
- package/dist/cjs/fastify.cjs +2109 -0
- package/dist/cjs/fetch.cjs +1453 -0
- package/dist/cjs/index.cjs +2074 -0
- package/dist/cjs/next.cjs +1518 -0
- package/dist/cjs/testing.cjs +214 -0
- package/dist/fastify.js +59 -0
- package/dist/fetch.js +13 -0
- package/dist/index.js +15 -716
- package/dist/next.js +41 -52
- package/dist/testing.js +39 -0
- package/dist/types/node/src/config.d.ts +107 -0
- package/dist/types/node/src/core.d.ts +66 -72
- package/dist/types/node/src/fastify.d.ts +41 -0
- package/dist/types/node/src/fetch.d.ts +76 -0
- package/dist/types/node/src/health.d.ts +41 -0
- package/dist/types/node/src/index.d.ts +114 -34
- package/dist/types/node/src/next.d.ts +40 -27
- package/dist/types/node/src/testing.d.ts +25 -0
- package/dist/types/shared/src/address.d.ts +81 -0
- package/dist/types/shared/src/index.d.ts +6 -0
- package/dist/types/shared/src/paths.d.ts +32 -0
- package/dist/types-cjs/node/src/config.d.cts +107 -0
- package/dist/types-cjs/node/src/core.d.cts +107 -0
- package/dist/types-cjs/node/src/fastify.d.cts +41 -0
- package/dist/types-cjs/node/src/fetch.d.cts +76 -0
- package/dist/types-cjs/node/src/health.d.cts +41 -0
- package/dist/types-cjs/node/src/index.d.cts +203 -0
- package/dist/types-cjs/node/src/next.d.cts +85 -0
- package/dist/types-cjs/node/src/testing.d.cts +25 -0
- package/dist/types-cjs/shared/src/address.d.cts +81 -0
- package/dist/types-cjs/shared/src/allowance.d.cts +17 -0
- package/dist/types-cjs/shared/src/collect.d.cts +52 -0
- package/dist/types-cjs/shared/src/exchange.d.cts +69 -0
- package/dist/types-cjs/shared/src/index.d.cts +105 -0
- package/dist/types-cjs/shared/src/keys.d.cts +13 -0
- package/dist/types-cjs/shared/src/paths.d.cts +32 -0
- package/dist/types-cjs/shared/src/tools.d.cts +101 -0
- package/dist/types-cjs/shared/src/verdict.d.cts +37 -0
- package/package.json +73 -10
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The visitor's address, as a site's server can trust it. Runtime-neutral (no node: imports), shared
|
|
3
|
+
* by the Node middleware and the fetch/edge helpers; the Python and Go SDKs implement the same rules
|
|
4
|
+
* (checked against sdks/conformance/vectors.json).
|
|
5
|
+
*
|
|
6
|
+
* X-Forwarded-For is a list every proxy appends to, so only its right end can be trusted: anything to
|
|
7
|
+
* the left of the last proxy you run was written by the client. "auto" therefore never takes the first
|
|
8
|
+
* entry. In order, it uses:
|
|
9
|
+
* 1. an address the web framework already resolved from its own proxy settings (Express with
|
|
10
|
+
* `trust proxy`, Fastify with `trustProxy`);
|
|
11
|
+
* 2. the host's own client header when the host is known from the environment (Fly, Vercel,
|
|
12
|
+
* Railway, Netlify, App Engine, Cloudflare Workers);
|
|
13
|
+
* 3. `cf-connecting-ip` when the connection comes from Cloudflare's published ranges;
|
|
14
|
+
* 4. the X-Forwarded-For chain read right to left from the connecting peer, skipping private,
|
|
15
|
+
* loopback and shared-network addresses (load balancers and proxies inside your network): the
|
|
16
|
+
* first public address is the visitor.
|
|
17
|
+
* When no public address appears at all, the request came from your own network (server-side
|
|
18
|
+
* rendering, health checks, internal services): it is marked internal and never paced.
|
|
19
|
+
*/
|
|
20
|
+
export type ClientIpMode = "auto" | "socket" | {
|
|
21
|
+
header: string;
|
|
22
|
+
} | {
|
|
23
|
+
proxies: readonly string[];
|
|
24
|
+
} | {
|
|
25
|
+
hops: number;
|
|
26
|
+
};
|
|
27
|
+
/** Hosts whose own client header "auto" trusts once they are detected from the environment. */
|
|
28
|
+
export type Platform = "fly" | "vercel" | "railway" | "netlify" | "appengine" | "workers";
|
|
29
|
+
export declare const PLATFORM_HEADERS: Readonly<Record<Platform, string>>;
|
|
30
|
+
/** The host, from environment variables it sets for every app (null when not one of the known hosts). */
|
|
31
|
+
export declare function detectPlatform(env: Record<string, string | undefined> | undefined): Platform | null;
|
|
32
|
+
export interface AddressRequest {
|
|
33
|
+
/** The connecting peer (the socket's remote address); "" when unknown. */
|
|
34
|
+
peer: string;
|
|
35
|
+
/** A request header by lower-case name; several values joined with ", ". */
|
|
36
|
+
header(name: string): string | undefined;
|
|
37
|
+
/** An address the web framework already resolved from its own proxy settings. */
|
|
38
|
+
framework?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface ResolvedAddress {
|
|
41
|
+
/** The visitor's address, normalized (IPv4 dotted, IPv6 compressed); "" when none could be read. */
|
|
42
|
+
address: string;
|
|
43
|
+
/** No public address anywhere in the chain: one of your own servers. Never paced. */
|
|
44
|
+
internal: boolean;
|
|
45
|
+
/** Where it came from: "framework", "platform:<host>", "cloudflare", "forwarded", "socket", "header", "hops", "proxies". */
|
|
46
|
+
via: string;
|
|
47
|
+
}
|
|
48
|
+
/** Cloudflare's published ranges (cloudflare.com/ips-v4, /ips-v6; checked 2026-12-03). */
|
|
49
|
+
export declare const CLOUDFLARE_RANGES: readonly string[];
|
|
50
|
+
/** Addresses that are never a visitor's: unspecified, private, shared (carrier-grade NAT), loopback, link-local. */
|
|
51
|
+
export declare const NON_PUBLIC_RANGES: readonly string[];
|
|
52
|
+
/** At most this many X-Forwarded-For entries are read (from the right). */
|
|
53
|
+
export declare const MAX_FORWARDED = 32;
|
|
54
|
+
/** An address as 16 bytes, or null. Accepts IPv4, IPv6 (with an IPv4 tail) and IPv4-mapped IPv6. */
|
|
55
|
+
export declare function parseIp(text: string): Uint8Array | null;
|
|
56
|
+
/** The canonical text of a parsed address: dotted IPv4 (mapped forms unmapped), RFC 5952 IPv6. */
|
|
57
|
+
export declare function formatIp(b: Uint8Array): string;
|
|
58
|
+
/**
|
|
59
|
+
* One address out of a header token: quotes, brackets, ports ("1.2.3.4:80", "[::1]:443",
|
|
60
|
+
* "2001:db8::1:46532" as CloudFront sends it) and zone ids removed, then normalized. Null when the
|
|
61
|
+
* token is not an address ("unknown", obfuscated identifiers, garbage).
|
|
62
|
+
*/
|
|
63
|
+
export declare function normalizeIp(token: string | undefined): string | null;
|
|
64
|
+
interface Cidr {
|
|
65
|
+
bytes: Uint8Array;
|
|
66
|
+
bits: number;
|
|
67
|
+
}
|
|
68
|
+
/** "a.b.c.d/n", "v6/n" or a bare address; null when malformed. */
|
|
69
|
+
export declare function parseCidr(text: string): Cidr | null;
|
|
70
|
+
/** Whether an address (any text form) belongs to one of the ranges. */
|
|
71
|
+
export declare function ipInRanges(address: string, ranges: readonly string[] | readonly Cidr[]): boolean;
|
|
72
|
+
/** A visitor's address could be this one: not private, shared, loopback, link-local or unspecified. */
|
|
73
|
+
export declare function isPublicIp(address: string): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The visitor's address under `mode` (default "auto"). `platform` is the host detected from the
|
|
76
|
+
* environment (detectPlatform), passed in so the result never depends on hidden state.
|
|
77
|
+
*/
|
|
78
|
+
export declare function resolveClientAddress(req: AddressRequest, mode?: ClientIpMode, platform?: Platform | null): ResolvedAddress;
|
|
79
|
+
/** Whether `value` is a valid clientIp mode (for config validation). Functions are checked by the caller. */
|
|
80
|
+
export declare function clientIpModeProblem(value: unknown): string | null;
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token buckets, one per key: `take` answers whether this use still fits. Runtime-neutral (a Map and
|
|
3
|
+
* numbers), so the collector's usage metering and the middleware's pace limits share it, on Node and
|
|
4
|
+
* on the edge. Memory stays bounded: past `maxKeys`, the least recently used key is forgotten (it
|
|
5
|
+
* starts full again if it returns).
|
|
6
|
+
*/
|
|
7
|
+
export declare class Allowance {
|
|
8
|
+
private readonly capacity;
|
|
9
|
+
private readonly refillPerMs;
|
|
10
|
+
private readonly maxKeys;
|
|
11
|
+
private readonly buckets;
|
|
12
|
+
constructor(capacity: number, refillPerMs: number, maxKeys?: number);
|
|
13
|
+
take(key: string, now?: number): boolean;
|
|
14
|
+
/** Whether `take` would succeed now, without using anything. */
|
|
15
|
+
has(key: string, now?: number): boolean;
|
|
16
|
+
private refill;
|
|
17
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a site lets Reguard receive: `collect` in reguard, in the SDK's `init()` and in the
|
|
3
|
+
* script tag's data-* attributes. Every setting has a default; a value that isn't understood falls
|
|
4
|
+
* back to its default with one warning, and nothing here ever throws into the host app.
|
|
5
|
+
*/
|
|
6
|
+
export type PagesMode = "masked" | "hashed" | "off";
|
|
7
|
+
export type AddressesMode = "hashed" | "off";
|
|
8
|
+
export interface CollectOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Page and API addresses. "masked" (default): ids, emails and long tokens are replaced before
|
|
11
|
+
* anything leaves. "hashed": only a keyed hash of each address and the kind of request. "off":
|
|
12
|
+
* only the kind of request.
|
|
13
|
+
*/
|
|
14
|
+
pages?: PagesMode;
|
|
15
|
+
/** Visitor addresses. "hashed" (default): only as a keyed hash. "off": Reguard keeps none. */
|
|
16
|
+
addresses?: AddressesMode;
|
|
17
|
+
/** Click, key, scroll and pointer timings from the browser. Default true. */
|
|
18
|
+
behavior?: boolean;
|
|
19
|
+
/** Request summaries from your server (reguard only). Default true. */
|
|
20
|
+
requests?: boolean;
|
|
21
|
+
/** Pages Reguard never runs on or reports. `*` matches anything; "/account/*" also covers "/account". */
|
|
22
|
+
skip?: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface CollectSettings {
|
|
25
|
+
pages: PagesMode;
|
|
26
|
+
addresses: AddressesMode;
|
|
27
|
+
behavior: boolean;
|
|
28
|
+
requests: boolean;
|
|
29
|
+
skip: string[];
|
|
30
|
+
}
|
|
31
|
+
/** The site's choices as the browser and the site's server report them, so reviewers know what was withheld. */
|
|
32
|
+
export interface CollectReport {
|
|
33
|
+
pages: PagesMode;
|
|
34
|
+
addresses: AddressesMode;
|
|
35
|
+
behavior: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare const DEFAULT_COLLECT: Readonly<CollectSettings>;
|
|
38
|
+
/**
|
|
39
|
+
* The settings `input` asks for. Anything unrecognized is reported through `warn` and replaced by its
|
|
40
|
+
* default; unknown fields are ignored. `skip` takes an array or a comma-separated string.
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolveCollect(input: unknown, warn?: (message: string) => void): CollectSettings;
|
|
43
|
+
/**
|
|
44
|
+
* A matcher for skip patterns: `*` matches any characters (slashes included), and a pattern ending in
|
|
45
|
+
* "/*" also matches the path without it ("/account/*" covers "/account"). Query and fragment are ignored.
|
|
46
|
+
*/
|
|
47
|
+
export declare function skipMatcher(patterns: readonly string[]): (path: string) => boolean;
|
|
48
|
+
/** The script tag's data-* attributes for the settings that differ from the defaults. */
|
|
49
|
+
export declare function collectAttributes(c: CollectSettings): Record<string, string>;
|
|
50
|
+
export declare function collectReport(c: CollectSettings): CollectReport;
|
|
51
|
+
/** A report from the wire, kept only if every field is one of the known values. */
|
|
52
|
+
export declare function parseCollectReport(v: unknown): CollectReport | undefined;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the server side may learn about one request/response without keeping its values. Shared by
|
|
3
|
+
* the collector (when it proxies a site) and by reguard (inside a site's own server), so
|
|
4
|
+
* raw parameter and body values never leave the process that saw them: each value is reduced to a
|
|
5
|
+
* keyed hash (to count distinct values), a coarse shape, and a few probe flags.
|
|
6
|
+
*
|
|
7
|
+
* Whether a flagged value counts as probing is decided later, against what that parameter normally
|
|
8
|
+
* receives on that site (by Reguard's collector): an injection string in a parameter that always
|
|
9
|
+
* carries ids is a probe; the same string in a chat message on a site about coding is not.
|
|
10
|
+
*/
|
|
11
|
+
export type Shape = "empty" | "int" | "decimal" | "bool" | "uuid" | "hex" | "slug" | "word" | "email" | "text" | "json";
|
|
12
|
+
export type Boundary = "neg" | "zero" | "overflow";
|
|
13
|
+
export interface ParamDescription {
|
|
14
|
+
/** Keyed hash of name=value (per site): equal values hash equally, nothing else is recoverable. */
|
|
15
|
+
h: string;
|
|
16
|
+
shape: Shape;
|
|
17
|
+
/** The value carries an injection, traversal, template or markup marker. */
|
|
18
|
+
strong?: true;
|
|
19
|
+
/** Integer-shaped boundary value: negative, zero, or overflow-sized. */
|
|
20
|
+
boundary?: Boundary;
|
|
21
|
+
}
|
|
22
|
+
export interface ExchangeDescription {
|
|
23
|
+
method: string;
|
|
24
|
+
/** Path with ids normalized (`/api/rounds/123/turn` → `/api/rounds/:n/turn`). */
|
|
25
|
+
endpoint: string;
|
|
26
|
+
/** Keyed hash of the raw path: distinct items fetched, even when endpoints normalize to one. */
|
|
27
|
+
item?: string;
|
|
28
|
+
/** Query parameters and top-level body fields. */
|
|
29
|
+
params: Record<string, ParamDescription>;
|
|
30
|
+
requestBytes: number;
|
|
31
|
+
status: number;
|
|
32
|
+
responseBytes: number;
|
|
33
|
+
/** Length of the top-level JSON array in the response (or of its first array field). */
|
|
34
|
+
listLength: number | null;
|
|
35
|
+
/** Pagination parameter (page/offset/cursor/after/skip) present and numeric. */
|
|
36
|
+
pageValue: number | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Framework cache-busters and routing parameters: unique on every request by design (Next.js
|
|
40
|
+
* `_rsc`, jQuery `_`, generic `cb`/`ts`/`v`), so their values say nothing about the visitor.
|
|
41
|
+
*/
|
|
42
|
+
export declare const IGNORED_PARAMS: Set<string>;
|
|
43
|
+
/** Path with id-like segments masked: numbers, UUIDs, hex, emails, long opaque tokens. */
|
|
44
|
+
export declare function normalizeEndpoint(path: string): string;
|
|
45
|
+
/** A URL as it may leave a site's server: masked path, parameter names, no values. */
|
|
46
|
+
export declare function privateUrl(url: string): string;
|
|
47
|
+
export declare function shapeOf(v: string): Shape;
|
|
48
|
+
export declare function strongMarker(v: string): boolean;
|
|
49
|
+
export declare function boundaryOf(v: string): Boundary | undefined;
|
|
50
|
+
export interface ExchangeInput {
|
|
51
|
+
method: string;
|
|
52
|
+
/** Path and query, as requested. */
|
|
53
|
+
url: string;
|
|
54
|
+
requestContentType?: string;
|
|
55
|
+
/** Request body as text (first 64 KB is plenty); empty when there is none. */
|
|
56
|
+
requestBody?: string;
|
|
57
|
+
requestBytes: number;
|
|
58
|
+
status: number;
|
|
59
|
+
responseContentType?: string;
|
|
60
|
+
/** First bytes of the response as text, for its list length only; never stored. */
|
|
61
|
+
responseHead?: string;
|
|
62
|
+
responseBytes: number;
|
|
63
|
+
}
|
|
64
|
+
/** `hash(name=value)` must be a keyed hash (HMAC with a per-site key). */
|
|
65
|
+
export declare function describeExchange(hash: (s: string) => string, x: ExchangeInput): ExchangeDescription;
|
|
66
|
+
/** Requests worth describing: APIs, RSC payloads, JSON, form posts. Pages and assets are counted elsewhere. */
|
|
67
|
+
export declare function isApiLike(url: string, responseContentType: string, requestBytes: number): boolean;
|
|
68
|
+
/** Requests the browser or framework issued on speculation (router prefetch, prerender), not the visitor. */
|
|
69
|
+
export declare function isSpeculativeHeaders(get: (name: string) => string | undefined): boolean;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire contract between the SDK (packages/sdk), the server package (packages/node) and Reguard's
|
|
3
|
+
* collector and detection model. Probe ids are stable strings; renaming one is a breaking change
|
|
4
|
+
* for stored datasets, so add new ids instead.
|
|
5
|
+
*/
|
|
6
|
+
export declare const WIRE_VERSION = 1;
|
|
7
|
+
/** Timestamped observation recorded by a probe (ms since page time origin). */
|
|
8
|
+
export interface ProbeEvent {
|
|
9
|
+
/** Probe id, e.g. "a3.gap" or "b.mark_target". */
|
|
10
|
+
id: string;
|
|
11
|
+
t: number;
|
|
12
|
+
data?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
/** Cumulative, last-write-wins measurements (the SDK resends the full map each beacon). */
|
|
15
|
+
export type Signals = Record<string, unknown>;
|
|
16
|
+
export interface InitRequest {
|
|
17
|
+
v: number;
|
|
18
|
+
site: string;
|
|
19
|
+
/** This browser's previous session id (the `_s` cookie), for linking reloads when the cookie can't travel (cross-origin script). */
|
|
20
|
+
prev?: string;
|
|
21
|
+
/** SDK version (packages/sdk/package.json). */
|
|
22
|
+
sdk?: string;
|
|
23
|
+
/** Verification-run id from the `lab_run` query parameter; the hosted collector ignores it. */
|
|
24
|
+
labRun?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The page's path, masked before it leaves the browser (ids, emails and long tokens replaced);
|
|
27
|
+
* absent when the site keeps its page addresses to itself (`collect.pages` "hashed" or "off").
|
|
28
|
+
*/
|
|
29
|
+
path?: string;
|
|
30
|
+
deviceId: string;
|
|
31
|
+
/** The site's data choices (SDK 0.6+), so the collector honours them and reviewers see them. */
|
|
32
|
+
collect?: CollectReport;
|
|
33
|
+
/** Seconds between verdict refreshes the site asks for (data-fresh, block.require): never sampled out (SDK 0.8+). */
|
|
34
|
+
fresh?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface InitResponse {
|
|
37
|
+
sid: string;
|
|
38
|
+
token: string;
|
|
39
|
+
/** A signed verdict with no claims for this session (collector 0.8+): the SDK sets it as `_sv` at once. */
|
|
40
|
+
verdict?: string;
|
|
41
|
+
/** Canary token used for the first-party source-map tripwire (F1). */
|
|
42
|
+
canary: string;
|
|
43
|
+
/** First-party prefix the collector is reachable at on this origin, e.g. "/_s". */
|
|
44
|
+
prefix: string;
|
|
45
|
+
config: SdkConfig;
|
|
46
|
+
}
|
|
47
|
+
export interface SdkConfig {
|
|
48
|
+
debuggerTrap: "main" | "worker" | "off";
|
|
49
|
+
/** Period of the debugger trap; shorter catches briefer debugger attachments. */
|
|
50
|
+
trapIntervalMs: number;
|
|
51
|
+
/** Hidden display:none nodes that make DOM snapshots expensive (E tarpit). 0 disables. */
|
|
52
|
+
tarpitNodes: number;
|
|
53
|
+
beaconIntervalMs: number;
|
|
54
|
+
/** Opt-in experimental probes (research), e.g. "ax_mutation", "jit_ratio". */
|
|
55
|
+
experiments: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Quiet mode (SDK 0.7+): the browser signal and event ids some rule reads. With the debugger trap
|
|
58
|
+
* off, the SDK reports only when one of these turns up (plus refreshes a site's server asks for)
|
|
59
|
+
* instead of every `beaconIntervalMs`. Absent: the SDK keeps the periodic heartbeat.
|
|
60
|
+
*/
|
|
61
|
+
news?: string[];
|
|
62
|
+
}
|
|
63
|
+
export interface BeaconRequest {
|
|
64
|
+
v: number;
|
|
65
|
+
sid: string;
|
|
66
|
+
token: string;
|
|
67
|
+
seq: number;
|
|
68
|
+
/** ms since page time origin when the beacon was built. */
|
|
69
|
+
t: number;
|
|
70
|
+
signals: Signals;
|
|
71
|
+
events: ProbeEvent[];
|
|
72
|
+
final?: boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Positive labels are kept separate: detecting automation does not establish REA usage, and
|
|
76
|
+
* neither establishes extraction. Everything else (DevTools, QA automation, assistive technology,
|
|
77
|
+
* curious customers) is negative unless it meets one of these definitions.
|
|
78
|
+
*/
|
|
79
|
+
export type Label = "rea" | "automated_exploration" | "unauthorized_extraction";
|
|
80
|
+
/**
|
|
81
|
+
* Decision outcomes. Uncertainty never becomes an accusation: without a validated alert the
|
|
82
|
+
* outcome is insufficient_evidence and the session keeps being observed.
|
|
83
|
+
*/
|
|
84
|
+
export type Outcome = "insufficient_evidence" | "review" | "restrict";
|
|
85
|
+
/**
|
|
86
|
+
* What the page learns after each beacon. Rule internals and evidence stay server-side, where
|
|
87
|
+
* reviewers see them; `token` is the signed form the site's own server verifies (packages/node).
|
|
88
|
+
*/
|
|
89
|
+
export interface Verdict {
|
|
90
|
+
sid: string;
|
|
91
|
+
outcome: Outcome;
|
|
92
|
+
/** Claims the investigation currently meets, shadow (`candidate`) ones included. */
|
|
93
|
+
claims?: VerdictClaim[];
|
|
94
|
+
token?: string;
|
|
95
|
+
}
|
|
96
|
+
export * from "./exchange.cjs";
|
|
97
|
+
export * from "./verdict.cjs";
|
|
98
|
+
export * from "./keys.cjs";
|
|
99
|
+
export * from "./tools.cjs";
|
|
100
|
+
export * from "./collect.cjs";
|
|
101
|
+
export * from "./allowance.cjs";
|
|
102
|
+
export * from "./address.cjs";
|
|
103
|
+
export * from "./paths.cjs";
|
|
104
|
+
import type { VerdictClaim } from "./verdict.cjs";
|
|
105
|
+
import type { CollectReport } from "./collect.cjs";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Customer site keys: `rgk.<siteId>.v<version>.<secret>`. One value to configure (REGUARD_KEY):
|
|
3
|
+
* the site id and key version are readable from it, the secret part is what verifies verdicts and
|
|
4
|
+
* authenticates request summaries. Bare secrets (the format before v0.4) still parse, without a site.
|
|
5
|
+
*/
|
|
6
|
+
export interface ParsedSiteKey {
|
|
7
|
+
site?: string;
|
|
8
|
+
version?: number;
|
|
9
|
+
secret: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const SITE_KEY_PREFIX = "rgk";
|
|
12
|
+
export declare function formatSiteKey(site: string, version: number, secret: string): string;
|
|
13
|
+
export declare function parseSiteKey(key: string | null | undefined): ParsedSiteKey | null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request paths for matching rules (`require`, `allow`, `collect.skip`, `ignore`, source-map denial).
|
|
3
|
+
* Runtime-neutral; the Python and Go SDKs implement the same rules (sdks/conformance/vectors.json).
|
|
4
|
+
*
|
|
5
|
+
* Frameworks route on different forms of the same path: Express on the raw path, Flask, Django,
|
|
6
|
+
* Starlette and Go's ServeMux on the decoded one, static file servers on a decoded and normalized
|
|
7
|
+
* one. A rule checked against only one form can be walked around with the other (`/%61pi/items`
|
|
8
|
+
* reaches `/api/items`; `/app.js%2Emap` is served as `/app.js.map`). So every rule sees both:
|
|
9
|
+
* - `raw`: the path as sent, without query or fragment (absolute-form targets cut to their path);
|
|
10
|
+
* - `decoded`: percent-decoded once, `//` collapsed and `.`/`..` segments resolved.
|
|
11
|
+
* Protections apply when either form matches; exemptions only when both do. Summaries keep the raw
|
|
12
|
+
* path.
|
|
13
|
+
*/
|
|
14
|
+
export interface CanonicalPath {
|
|
15
|
+
raw: string;
|
|
16
|
+
decoded: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Percent-decoding, once. When the path holds a malformed escape or the bytes aren't UTF-8, only
|
|
20
|
+
* escapes of ASCII characters (%00-%7F) are decoded and the rest stay as they are.
|
|
21
|
+
*/
|
|
22
|
+
export declare function decodeOnce(s: string): string;
|
|
23
|
+
/** `//` collapsed, then `.` and `..` resolved (RFC 3986 section 5.2.4); never climbs above `/`. */
|
|
24
|
+
export declare function normalizeSegments(path: string): string;
|
|
25
|
+
/** Both forms of a request target (`req.url`, a RequestURI, a WSGI path): see the module comment. */
|
|
26
|
+
export declare function canonicalPath(target: string | undefined): CanonicalPath;
|
|
27
|
+
/** A protection (require, source maps, skip, ignore) applies when either form matches. */
|
|
28
|
+
export declare function matchesEither(matcher: (path: string) => boolean, p: CanonicalPath): boolean;
|
|
29
|
+
/** An exemption (block.allow) applies only when both forms match. */
|
|
30
|
+
export declare function matchesBoth(matcher: (path: string) => boolean, p: CanonicalPath): boolean;
|
|
31
|
+
/** A source map request (`*.map`), in either form. */
|
|
32
|
+
export declare function isSourceMapPath(p: CanonicalPath): boolean;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverse-engineering and extraction tools, recognized from the requests they send. Shared by the
|
|
3
|
+
* Reguard's collector and a site's own server (packages/node), so both decide the same way which
|
|
4
|
+
* cookieless traffic is worth watching. Everything here is evidence only: user agents are
|
|
5
|
+
* trivially spoofed, so a tool match never claims anything on its own.
|
|
6
|
+
* Fingerprints were captured from the tools themselves.
|
|
7
|
+
*/
|
|
8
|
+
export type ToolFamily =
|
|
9
|
+
/** Path fuzzers and vulnerability scanners: ffuf, feroxbuster, gobuster, nikto, sqlmap, nuclei, ... */
|
|
10
|
+
"scanner"
|
|
11
|
+
/** Link-following crawlers and spiders: katana, gospider, colly, scrapy, crawl4ai, ... */
|
|
12
|
+
| "crawler"
|
|
13
|
+
/** Whole-site copiers: HTTrack, wget --mirror, website downloaders. */
|
|
14
|
+
| "cloner"
|
|
15
|
+
/** Declared AI crawlers and fetchers (training, search, agents). */
|
|
16
|
+
| "ai_crawler"
|
|
17
|
+
/** Generic HTTP libraries and command-line clients: requests, Go net/http, curl, node fetch, ... */
|
|
18
|
+
| "http_library"
|
|
19
|
+
/** A browser that says it is headless. */
|
|
20
|
+
| "headless_browser"
|
|
21
|
+
/** Search engines and link-preview bots: wanted by most sites, never evidence by themselves. */
|
|
22
|
+
| "search_bot"
|
|
23
|
+
/** Uptime and synthetic monitors. */
|
|
24
|
+
| "monitor";
|
|
25
|
+
export interface ToolMatch {
|
|
26
|
+
family: ToolFamily;
|
|
27
|
+
tool: string;
|
|
28
|
+
}
|
|
29
|
+
/** Families a site normally wants. They are recognized so they are left alone, never flagged. */
|
|
30
|
+
export declare const BENIGN_TOOL_FAMILIES: ReadonlySet<ToolFamily>;
|
|
31
|
+
/** The tool a request's user agent names, if any. An empty user agent counts as a bare HTTP client. */
|
|
32
|
+
export declare function toolOf(userAgent: string | undefined): ToolMatch | undefined;
|
|
33
|
+
export interface BrowserHeaders {
|
|
34
|
+
userAgent: string;
|
|
35
|
+
acceptLanguage?: string;
|
|
36
|
+
accept?: string;
|
|
37
|
+
secFetchMode?: string;
|
|
38
|
+
/** The Sec-CH-UA client hint (Chromium browsers send it to secure origins). */
|
|
39
|
+
secChUa?: string;
|
|
40
|
+
/** Whether the request reached a secure origin (HTTPS or localhost). Browsers send fetch metadata and
|
|
41
|
+
* client hints only there; `false` turns those checks off, `undefined` assumes a secure origin. */
|
|
42
|
+
secure?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Ways a request claiming to be a browser does not behave like one. Every current browser sends
|
|
46
|
+
* Accept-Language; Chrome/Edge 90+, Firefox 90+ and Safari 17+ send fetch metadata (Sec-Fetch-*)
|
|
47
|
+
* to secure origins; no browser puts runs of spaces in its headers; a Chromium browser's client
|
|
48
|
+
* hints name the same version (and, headless or not, the same browser) as its user agent. HTTP
|
|
49
|
+
* clients wearing a browser user agent miss some of these (captured: katana and hakrawler send no
|
|
50
|
+
* Accept-Language, LinkFinder's user agent and Accept carry runs of spaces, crawl4ai's fixed user
|
|
51
|
+
* agent disagrees with the client hints of the Chromium that actually runs). Old browsers on old
|
|
52
|
+
* devices stay coherent: only fetch metadata depends on the version, and only for current ones.
|
|
53
|
+
*/
|
|
54
|
+
export declare function browserIncoherence(h: BrowserHeaders): string[];
|
|
55
|
+
/**
|
|
56
|
+
* Paths scanners and agents probe and normal browsing never requests. Matched on the path only;
|
|
57
|
+
* the forced-browsing evidence needs several distinct families in one investigation.
|
|
58
|
+
*/
|
|
59
|
+
export declare const SENSITIVE_PATH: RegExp;
|
|
60
|
+
export interface WatchInput extends BrowserHeaders {
|
|
61
|
+
path: string;
|
|
62
|
+
status?: number;
|
|
63
|
+
/** What the site's server said about the path instead of sending it (pages "hashed" or "off"). */
|
|
64
|
+
traits?: PathTraits;
|
|
65
|
+
}
|
|
66
|
+
/** Files crawlers read to find URLs (browsers never request them on their own). */
|
|
67
|
+
export declare const DISCOVERY_FILE: RegExp;
|
|
68
|
+
/**
|
|
69
|
+
* Should cookieless traffic like this be watched as a visitor investigation? Yes for named
|
|
70
|
+
* non-benign tools, clients that only pretend to be browsers, scanner paths, discovery files,
|
|
71
|
+
* source maps and not-found answers (fuzzing). Ordinary browsers without a session (a first page
|
|
72
|
+
* view before the script runs, a visitor with the script blocked) are not.
|
|
73
|
+
*/
|
|
74
|
+
export declare function watchworthy(w: WatchInput): boolean;
|
|
75
|
+
export type RequestKind = "document" | "api" | "rsc" | "static";
|
|
76
|
+
/**
|
|
77
|
+
* What a request's path says without the path itself. Sent by a site's server in place of the path
|
|
78
|
+
* when the site keeps its addresses to itself (`collect.pages` "hashed" or "off"), and computed by
|
|
79
|
+
* the collector from the path otherwise, so both judge a request the same way. Only well-known
|
|
80
|
+
* names ever appear: scanner targets and the files crawlers read, never the site's own pages.
|
|
81
|
+
*/
|
|
82
|
+
export interface PathTraits {
|
|
83
|
+
kind: RequestKind;
|
|
84
|
+
/** A well-known scanner target (/.env, /.git, wp-admin, admin, …), by its family. */
|
|
85
|
+
family?: string;
|
|
86
|
+
/** A file crawlers read to find URLs (robots.txt, sitemaps, security.txt), by its standard path. */
|
|
87
|
+
discovery?: string;
|
|
88
|
+
/** A path browsers request on their own (favicon, manifest). */
|
|
89
|
+
auto?: true;
|
|
90
|
+
/** A source map. */
|
|
91
|
+
map?: true;
|
|
92
|
+
/** A script file. */
|
|
93
|
+
script?: true;
|
|
94
|
+
/** A long run of random hex: wildcard calibration (ffuf -ac, feroxbuster), git object dumps. */
|
|
95
|
+
random?: true;
|
|
96
|
+
}
|
|
97
|
+
/** Page, API call, React Server Components payload or file, from the path, query and the `rsc` header. */
|
|
98
|
+
export declare function requestKind(url: string, rsc?: boolean): RequestKind;
|
|
99
|
+
export declare function pathTraits(url: string, rsc?: boolean): PathTraits;
|
|
100
|
+
/** Traits from the wire, kept only when well-formed (a discovery path must be one of the known files). */
|
|
101
|
+
export declare function parsePathTraits(v: unknown): PathTraits | undefined;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signed verdicts: the collector signs what it concluded about a session with the site's key, the
|
|
3
|
+
* SDK stores the token in the first-party `_sv` cookie, and the site's own server verifies it
|
|
4
|
+
* (packages/node) before deciding what to serve. WebCrypto only, so it runs in browsers, Node 20+
|
|
5
|
+
* and edge runtimes alike. A token is useless on another site or for another session.
|
|
6
|
+
*/
|
|
7
|
+
import type { Label, Outcome } from "./index.cjs";
|
|
8
|
+
export type Stage = "candidate" | "alert" | "enforce";
|
|
9
|
+
export interface VerdictClaim {
|
|
10
|
+
rule: string;
|
|
11
|
+
label: Label;
|
|
12
|
+
/** candidate = shadow (not yet validated), alert = validated for review, enforce = validated for restriction. */
|
|
13
|
+
stage: Stage;
|
|
14
|
+
}
|
|
15
|
+
export interface VerdictPayload {
|
|
16
|
+
v: 1;
|
|
17
|
+
sid: string;
|
|
18
|
+
site: string;
|
|
19
|
+
outcome: Outcome;
|
|
20
|
+
claims: VerdictClaim[];
|
|
21
|
+
/** Expiry, epoch ms. */
|
|
22
|
+
exp: number;
|
|
23
|
+
}
|
|
24
|
+
export declare const VERDICT_COOKIE = "_sv";
|
|
25
|
+
export declare const SESSION_COOKIE = "_s";
|
|
26
|
+
export declare function signVerdict(siteKey: string, payload: VerdictPayload): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* The payload if the token is authentic, unexpired and (when given) for this site and session;
|
|
29
|
+
* otherwise null. Never throws.
|
|
30
|
+
*/
|
|
31
|
+
export declare function verifyVerdict(siteKey: string, token: string | undefined, expect?: {
|
|
32
|
+
site?: string;
|
|
33
|
+
sid?: string;
|
|
34
|
+
now?: number;
|
|
35
|
+
}): Promise<VerdictPayload | null>;
|
|
36
|
+
/** Read one cookie from a Cookie header. */
|
|
37
|
+
export declare function cookieValue(header: string | null | undefined, name: string): string | undefined;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reguard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Reguard for your server: app.use(reguard()) serves /_s/*, adds the script tag, verifies verdicts and sends request summaries (hashes and shapes only). Next.js helpers in reguard/next.",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Reguard for your server: catch AI agents and tools reverse engineering your web app. app.use(reguard()) serves /_s/*, adds the script tag, verifies verdicts, blocks (or dry-runs) flagged visitors and sends request summaries (hashes and shapes only). Next.js helpers in reguard/next, fetch runtimes in reguard/fetch, Fastify in reguard/fastify, test helpers in reguard/testing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"reguard",
|
|
7
7
|
"reverse-engineering",
|
|
@@ -11,19 +11,82 @@
|
|
|
11
11
|
"security",
|
|
12
12
|
"middleware",
|
|
13
13
|
"express",
|
|
14
|
-
"nextjs"
|
|
14
|
+
"nextjs",
|
|
15
|
+
"fastify",
|
|
16
|
+
"hono",
|
|
17
|
+
"bot-protection",
|
|
18
|
+
"anti-scraping"
|
|
15
19
|
],
|
|
16
20
|
"type": "module",
|
|
17
|
-
"main": "./dist/index.
|
|
21
|
+
"main": "./dist/cjs/index.cjs",
|
|
18
22
|
"types": "./dist/types/node/src/index.d.ts",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"typesVersions": {
|
|
25
|
+
"*": {
|
|
26
|
+
"next": [
|
|
27
|
+
"dist/types/node/src/next.d.ts"
|
|
28
|
+
],
|
|
29
|
+
"fetch": [
|
|
30
|
+
"dist/types/node/src/fetch.d.ts"
|
|
31
|
+
],
|
|
32
|
+
"fastify": [
|
|
33
|
+
"dist/types/node/src/fastify.d.ts"
|
|
34
|
+
],
|
|
35
|
+
"testing": [
|
|
36
|
+
"dist/types/node/src/testing.d.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
19
40
|
"exports": {
|
|
20
41
|
".": {
|
|
21
|
-
"
|
|
22
|
-
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./dist/types/node/src/index.d.ts",
|
|
44
|
+
"default": "./dist/index.js"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./dist/types-cjs/node/src/index.d.cts",
|
|
48
|
+
"default": "./dist/cjs/index.cjs"
|
|
49
|
+
}
|
|
23
50
|
},
|
|
24
51
|
"./next": {
|
|
25
|
-
"
|
|
26
|
-
|
|
52
|
+
"import": {
|
|
53
|
+
"types": "./dist/types/node/src/next.d.ts",
|
|
54
|
+
"default": "./dist/next.js"
|
|
55
|
+
},
|
|
56
|
+
"require": {
|
|
57
|
+
"types": "./dist/types-cjs/node/src/next.d.cts",
|
|
58
|
+
"default": "./dist/cjs/next.cjs"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"./fetch": {
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/types/node/src/fetch.d.ts",
|
|
64
|
+
"default": "./dist/fetch.js"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./dist/types-cjs/node/src/fetch.d.cts",
|
|
68
|
+
"default": "./dist/cjs/fetch.cjs"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"./fastify": {
|
|
72
|
+
"import": {
|
|
73
|
+
"types": "./dist/types/node/src/fastify.d.ts",
|
|
74
|
+
"default": "./dist/fastify.js"
|
|
75
|
+
},
|
|
76
|
+
"require": {
|
|
77
|
+
"types": "./dist/types-cjs/node/src/fastify.d.cts",
|
|
78
|
+
"default": "./dist/cjs/fastify.cjs"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"./testing": {
|
|
82
|
+
"import": {
|
|
83
|
+
"types": "./dist/types/node/src/testing.d.ts",
|
|
84
|
+
"default": "./dist/testing.js"
|
|
85
|
+
},
|
|
86
|
+
"require": {
|
|
87
|
+
"types": "./dist/types-cjs/node/src/testing.d.cts",
|
|
88
|
+
"default": "./dist/cjs/testing.cjs"
|
|
89
|
+
}
|
|
27
90
|
},
|
|
28
91
|
"./package.json": "./package.json"
|
|
29
92
|
},
|
|
@@ -42,12 +105,12 @@
|
|
|
42
105
|
"license": "MIT",
|
|
43
106
|
"repository": {
|
|
44
107
|
"type": "git",
|
|
45
|
-
"url": "git+https://github.com/
|
|
108
|
+
"url": "git+https://github.com/cadenticai/reguard-js.git",
|
|
46
109
|
"directory": "packages/node"
|
|
47
110
|
},
|
|
48
111
|
"homepage": "https://reguard.site/docs/",
|
|
49
112
|
"bugs": {
|
|
50
|
-
"url": "https://github.com/
|
|
113
|
+
"url": "https://github.com/cadenticai/reguard-js/issues"
|
|
51
114
|
},
|
|
52
115
|
"devDependencies": {
|
|
53
116
|
"@re-detect/shared": "0.1.0"
|