reguard 0.9.0 → 0.11.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 +2 -2
- package/dist/chunks/{chunk-CGXTVKRB.js → chunk-2N762ZXU.js} +152 -37
- package/dist/chunks/{chunk-CUIFGML3.js → chunk-EV6D7UZF.js} +82 -36
- package/dist/chunks/{chunk-W4FDH5AD.js → chunk-VZKXPEWQ.js} +9 -7
- package/dist/chunks/{chunk-PRPLLMXA.js → chunk-XCSVBLHF.js} +39 -34
- package/dist/cjs/fastify.cjs +235 -86
- package/dist/cjs/fetch.cjs +190 -74
- package/dist/cjs/index.cjs +234 -77
- package/dist/cjs/next.cjs +211 -92
- package/dist/cjs/testing.cjs +0 -104
- package/dist/fastify.js +4 -12
- package/dist/fetch.js +3 -3
- package/dist/index.js +3 -3
- package/dist/next.js +24 -22
- package/dist/testing.js +1 -1
- package/dist/types/node/src/fastify.d.ts +15 -7
- package/dist/types/node/src/fetch.d.ts +6 -2
- package/dist/types/node/src/next.d.ts +38 -17
- package/dist/types/shared/src/address.d.ts +25 -6
- package/dist/types/shared/src/collect.d.ts +7 -1
- package/dist/types/shared/src/index.d.ts +17 -5
- package/dist/types/shared/src/paths.d.ts +6 -2
- package/dist/types/shared/src/tools.d.ts +10 -0
- package/dist/types-cjs/node/src/fastify.d.cts +15 -7
- package/dist/types-cjs/node/src/fetch.d.cts +6 -2
- package/dist/types-cjs/node/src/next.d.cts +38 -17
- package/dist/types-cjs/shared/src/address.d.cts +25 -6
- package/dist/types-cjs/shared/src/collect.d.cts +7 -1
- package/dist/types-cjs/shared/src/index.d.cts +17 -5
- package/dist/types-cjs/shared/src/paths.d.cts +6 -2
- package/dist/types-cjs/shared/src/tools.d.cts +10 -0
- package/package.json +3 -2
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
* reguard/next: Reguard for Next.js. Runtime-neutral (no node: imports), so it also runs in
|
|
3
3
|
* middleware and on the edge runtime. Built on reguard/fetch.
|
|
4
4
|
*
|
|
5
|
-
* //
|
|
6
|
-
*
|
|
7
|
-
* export
|
|
5
|
+
* // middleware.ts (proxy.ts on Next 16): serves /_s/* (the script and its reports) from Reguard,
|
|
6
|
+
* // first-party, and answers flagged visitors with { block: true }
|
|
7
|
+
* export const middleware = reguardMiddleware();
|
|
8
8
|
*
|
|
9
9
|
* // app/layout.tsx (App Router), or pages/_document.tsx (Pages Router)
|
|
10
10
|
* import Script from "next/script";
|
|
11
11
|
* import { reguardScriptProps } from "reguard/next";
|
|
12
12
|
* <Script {...reguardScriptProps()} />
|
|
13
13
|
*
|
|
14
|
+
* // next.config.js: the site id for pages rendered where REGUARD_KEY isn't (build, browser)
|
|
15
|
+
* export default withReguard({ ...yourConfig });
|
|
16
|
+
*
|
|
14
17
|
* // anywhere on the server: route handler, server action, middleware
|
|
15
18
|
* const verdict = await reguardVerdict(request);
|
|
16
|
-
*
|
|
17
|
-
* // middleware.ts (proxy.ts on Next 16): answer flagged visitors, like reguard({ block: true })
|
|
18
|
-
* export const middleware = reguardMiddleware({ block: true });
|
|
19
19
|
*/
|
|
20
20
|
import { type CollectOptions } from "../../shared/src/index.cjs";
|
|
21
21
|
import { type ClientIpOption, type LoggerLike } from "./config.cjs";
|
|
@@ -27,14 +27,18 @@ export { type BlockReason, ReguardConfigError } from "./config.cjs";
|
|
|
27
27
|
export { type EventsPage, type ReguardApiOptions, type ReguardEvent, type ReguardEventType, type VisitorStatus, ReguardApiError, ReguardWebhookError, reguardApi, verifyWebhook, } from "./api.cjs";
|
|
28
28
|
export type { AddressesMode, CollectOptions, Label, PagesMode, Stage, VerdictClaim } from "../../shared/src/index.cjs";
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* Makes the site id (public: it is in the script tag anyway) available as NEXT_PUBLIC_REGUARD_SITE,
|
|
31
|
+
* so `reguardScriptProps()` works in pages rendered where REGUARD_KEY isn't: prerendered at build
|
|
32
|
+
* time, or in components that also render in the browser. The id comes from `site`, else from
|
|
33
|
+
* REGUARD_KEY when the build has it. Takes a config object or function.
|
|
34
|
+
*
|
|
35
|
+
* Earlier releases also routed /_s/* here with a rewrite; reguardMiddleware() serves it now, so the
|
|
36
|
+
* visitor's sign-in cookies and headers stay on your server and the collector address is read at
|
|
37
|
+
* run time.
|
|
34
38
|
*/
|
|
35
39
|
export declare function withReguard<T extends object>(config: T, options?: {
|
|
36
|
-
collector?: string;
|
|
37
40
|
key?: string;
|
|
41
|
+
site?: string;
|
|
38
42
|
}): T;
|
|
39
43
|
/** The visitor's verified verdict from a Request or NextRequest (null when there is none). */
|
|
40
44
|
export declare function reguardVerdict(request: {
|
|
@@ -70,17 +74,34 @@ export interface ReguardMiddlewareOptions {
|
|
|
70
74
|
block?: boolean | FetchBlockOptions;
|
|
71
75
|
key?: string;
|
|
72
76
|
previousKey?: string;
|
|
77
|
+
/** Where /_s/* goes (default https://api.reguard.site, or REGUARD_COLLECTOR), read at run time. */
|
|
78
|
+
collector?: string;
|
|
79
|
+
/** What Reguard receives from your server: `requests: false` sends nothing but the script's traffic. */
|
|
80
|
+
collect?: CollectOptions;
|
|
81
|
+
/** Paths Reguard leaves alone entirely (health checks, webhooks). */
|
|
82
|
+
ignore?: string[];
|
|
73
83
|
sourceMaps?: "deny" | "allow";
|
|
74
84
|
clientIp?: ClientIpOption<Request>;
|
|
85
|
+
timeoutMs?: number;
|
|
75
86
|
debug?: boolean;
|
|
76
87
|
logger?: LoggerLike | false;
|
|
77
88
|
}
|
|
78
89
|
/**
|
|
79
|
-
* Next.js middleware (edge or Node runtime)
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
90
|
+
* Next.js middleware (edge or Node runtime). It serves /_s/* (the script and its reports) from
|
|
91
|
+
* Reguard, first-party: the visitor's cookies other than Reguard's own, Authorization, the full
|
|
92
|
+
* referrer and address or location headers stay on your server, and the visitor's address goes as a
|
|
93
|
+
* keyed hash. Keep /_s/:path* in your matcher.
|
|
94
|
+
*
|
|
95
|
+
* With `block` it answers blocked visitors itself, like `reguard({ block: true })` in Express:
|
|
96
|
+
* Reguard's default answer (or `block.respond`'s), with `block.dryRun`, `block.allow` and
|
|
97
|
+
* `block.onBlock` as there. It also paces pages for clients that don't present as a browser (each
|
|
98
|
+
* instance counts on its own) and denies source maps (keep `*.map` in your matcher for that).
|
|
84
99
|
* `block.require` needs reguard() in a Node server and is refused here.
|
|
100
|
+
*
|
|
101
|
+
* Middleware never sees your response, so it reports what it can without one: what Reguard decided
|
|
102
|
+
* (blocks, and what a dry run would have blocked, which the dashboard counts) and requests from
|
|
103
|
+
* clients that don't present as a browser. Everyone else gets `undefined`, so Next carries on.
|
|
85
104
|
*/
|
|
86
|
-
export declare function reguardMiddleware(options?: ReguardMiddlewareOptions): (request: Request
|
|
105
|
+
export declare function reguardMiddleware(options?: ReguardMiddlewareOptions): (request: Request, event?: {
|
|
106
|
+
waitUntil?(p: Promise<unknown>): void;
|
|
107
|
+
}) => Promise<Response | undefined>;
|
|
@@ -10,12 +10,15 @@
|
|
|
10
10
|
* `trust proxy`, Fastify with `trustProxy`);
|
|
11
11
|
* 2. the host's own client header when the host is known from the environment (Fly, Vercel,
|
|
12
12
|
* Railway, Netlify, App Engine, Cloudflare Workers);
|
|
13
|
-
* 3.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
13
|
+
* 3. the chain [...X-Forwarded-For, connecting peer] read right to left. Each hop is skipped when it
|
|
14
|
+
* is one of yours (private, loopback, shared-network: load balancers, proxies, the cluster) or
|
|
15
|
+
* CloudFront's (its origin-facing ranges; CloudFront appends the address it saw, so the next entry
|
|
16
|
+
* left is that). A Cloudflare address ends the walk at `cf-connecting-ip`, which Cloudflare sets
|
|
17
|
+
* itself. The first other address is the visitor.
|
|
18
|
+
* Without a connecting peer (Next.js middleware, fetch runtimes) the walk starts at the right end of
|
|
19
|
+
* X-Forwarded-For, which the nearest proxy wrote. When no public address appears at all, the request
|
|
20
|
+
* came from your own network (server-side rendering, health checks, internal services): it is marked
|
|
21
|
+
* internal and never paced.
|
|
19
22
|
*/
|
|
20
23
|
export type ClientIpMode = "auto" | "socket" | {
|
|
21
24
|
header: string;
|
|
@@ -47,6 +50,16 @@ export interface ResolvedAddress {
|
|
|
47
50
|
}
|
|
48
51
|
/** Cloudflare's published ranges (cloudflare.com/ips-v4, /ips-v6; checked 2026-12-03). */
|
|
49
52
|
export declare const CLOUDFLARE_RANGES: readonly string[];
|
|
53
|
+
/**
|
|
54
|
+
* CloudFront's origin-facing ranges (AWS's published ip-ranges.json, service CLOUDFRONT_ORIGIN_FACING;
|
|
55
|
+
* checked 2026-12-04): the addresses CloudFront connects to origins from. Only CloudFront uses them.
|
|
56
|
+
*/
|
|
57
|
+
export declare const CLOUDFRONT_RANGES: readonly string[];
|
|
58
|
+
/**
|
|
59
|
+
* Headers whose value is always "address:port", so the last ":port" is cut even from an IPv6 address
|
|
60
|
+
* written without brackets (CloudFront-Viewer-Address: "2001:db8::1:443" is 2001:db8::1, port 443).
|
|
61
|
+
*/
|
|
62
|
+
export declare const PORT_SUFFIXED_HEADERS: readonly string[];
|
|
50
63
|
/** Addresses that are never a visitor's: unspecified, private, shared (carrier-grade NAT), loopback, link-local. */
|
|
51
64
|
export declare const NON_PUBLIC_RANGES: readonly string[];
|
|
52
65
|
/** At most this many X-Forwarded-For entries are read (from the right). */
|
|
@@ -67,6 +80,12 @@ interface Cidr {
|
|
|
67
80
|
}
|
|
68
81
|
/** "a.b.c.d/n", "v6/n" or a bare address; null when malformed. */
|
|
69
82
|
export declare function parseCidr(text: string): Cidr | null;
|
|
83
|
+
/**
|
|
84
|
+
* What pace, claims and visitor hashes key an address by: an IPv6 address by its /64 (one host or
|
|
85
|
+
* home network's whole block, which its owner can rotate through freely), an IPv4 one as it is.
|
|
86
|
+
* "" for anything that isn't an address.
|
|
87
|
+
*/
|
|
88
|
+
export declare function addressKey(address: string | undefined): string;
|
|
70
89
|
/** Whether an address (any text form) belongs to one of the ranges. */
|
|
71
90
|
export declare function ipInRanges(address: string, ranges: readonly string[] | readonly Cidr[]): boolean;
|
|
72
91
|
/** A visitor's address could be this one: not private, shared, loopback, link-local or unspecified. */
|
|
@@ -43,8 +43,14 @@ export declare function resolveCollect(input: unknown, warn?: (message: string)
|
|
|
43
43
|
/**
|
|
44
44
|
* A matcher for skip patterns: `*` matches any characters (slashes included), and a pattern ending in
|
|
45
45
|
* "/*" also matches the path without it ("/account/*" covers "/account"). Query and fragment are ignored.
|
|
46
|
+
* Exemptions (block.allow) match exactly; protections use protectionMatcher.
|
|
46
47
|
*/
|
|
47
|
-
export declare function skipMatcher(patterns: readonly string[]): (path: string) => boolean;
|
|
48
|
+
export declare function skipMatcher(patterns: readonly string[], ignoreCase?: boolean): (path: string) => boolean;
|
|
49
|
+
/**
|
|
50
|
+
* A matcher for protections (require, collect.skip, ignore): as skipMatcher, ignoring case, since
|
|
51
|
+
* routers that match paths case-insensitively (Express's default) serve /API/x as /api/x.
|
|
52
|
+
*/
|
|
53
|
+
export declare function protectionMatcher(patterns: readonly string[]): (path: string) => boolean;
|
|
48
54
|
/** The script tag's data-* attributes for the settings that differ from the defaults. */
|
|
49
55
|
export declare function collectAttributes(c: CollectSettings): Record<string, string>;
|
|
50
56
|
export declare function collectReport(c: CollectSettings): CollectReport;
|
|
@@ -18,6 +18,11 @@ export interface InitRequest {
|
|
|
18
18
|
site: string;
|
|
19
19
|
/** This browser's previous session id (the `_s` cookie), for linking reloads when the cookie can't travel (cross-origin script). */
|
|
20
20
|
prev?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The session this tab's previous page load started (sessionStorage `_s_tab`, script 0.9.3+): page
|
|
23
|
+
* loads in one tab stay one investigation when the `_s` cookie is cleared or blocked between them.
|
|
24
|
+
*/
|
|
25
|
+
tab?: string;
|
|
21
26
|
/** SDK version (packages/sdk/package.json). */
|
|
22
27
|
sdk?: string;
|
|
23
28
|
/** Verification-run id from the `lab_run` query parameter; the hosted collector ignores it. */
|
|
@@ -53,18 +58,25 @@ export interface InitResponse {
|
|
|
53
58
|
config: SdkConfig;
|
|
54
59
|
}
|
|
55
60
|
export interface SdkConfig {
|
|
56
|
-
|
|
61
|
+
/**
|
|
62
|
+
* The debugger trap (SDK 0.9.5+: served under /node_modules/, which Chrome DevTools ignore-lists, so
|
|
63
|
+
* DevTools doesn't stop there while tools that turn the Debugger on without ignore lists do).
|
|
64
|
+
* "worker" is read as "main". "probe" (lab) runs only the attach probe (SDK 0.9.6), to measure it alone.
|
|
65
|
+
*/
|
|
66
|
+
debuggerTrap: "main" | "worker" | "off" | "probe";
|
|
57
67
|
/** Period of the debugger trap; shorter catches briefer debugger attachments. */
|
|
58
68
|
trapIntervalMs: number;
|
|
69
|
+
/** Where the trap runs: "public" (default) skips localhost, private addresses and .local/.test hosts; "all" (lab) runs anywhere. */
|
|
70
|
+
trapScope?: "public" | "all";
|
|
59
71
|
/** Hidden display:none nodes that make DOM snapshots expensive (E tarpit). 0 disables. */
|
|
60
72
|
tarpitNodes: number;
|
|
61
73
|
beaconIntervalMs: number;
|
|
62
74
|
/** Opt-in experimental probes (research), e.g. "ax_mutation", "jit_ratio". */
|
|
63
75
|
experiments: string[];
|
|
64
76
|
/**
|
|
65
|
-
* Quiet mode (SDK 0.7+): the browser signal and event ids some rule reads.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
77
|
+
* Quiet mode (SDK 0.7+): the browser signal and event ids some rule reads. The SDK reports only when
|
|
78
|
+
* one of these turns up (plus refreshes a site's server asks for) instead of every
|
|
79
|
+
* `beaconIntervalMs`; from SDK 0.9.5 also with the debugger trap on. Absent: the periodic heartbeat.
|
|
68
80
|
*/
|
|
69
81
|
news?: string[];
|
|
70
82
|
}
|
|
@@ -84,7 +96,7 @@ export interface BeaconRequest {
|
|
|
84
96
|
* neither establishes extraction. Everything else (DevTools, QA automation, assistive technology,
|
|
85
97
|
* curious customers) is negative unless it meets one of these definitions.
|
|
86
98
|
*/
|
|
87
|
-
export type Label = "
|
|
99
|
+
export type Label = "reverse_engineering" | "automated_exploration" | "unauthorized_extraction";
|
|
88
100
|
/**
|
|
89
101
|
* Decision outcomes. Uncertainty never becomes an accusation: without a validated alert the
|
|
90
102
|
* outcome is insufficient_evidence and the session keeps being observed.
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* one. A rule checked against only one form can be walked around with the other (`/%61pi/items`
|
|
8
8
|
* reaches `/api/items`; `/app.js%2Emap` is served as `/app.js.map`). So every rule sees both:
|
|
9
9
|
* - `raw`: the path as sent, without query or fragment (absolute-form targets cut to their path);
|
|
10
|
-
* - `decoded`: percent-decoded once, `//` collapsed and
|
|
10
|
+
* - `decoded`: percent-decoded once, `\` read as `/` (as URL parsers read it), `//` collapsed and
|
|
11
|
+
* `.`/`..` segments resolved.
|
|
11
12
|
* Protections apply when either form matches; exemptions only when both do. Summaries keep the raw
|
|
12
13
|
* path.
|
|
13
14
|
*/
|
|
@@ -24,7 +25,10 @@ export declare function decodeOnce(s: string): string;
|
|
|
24
25
|
export declare function normalizeSegments(path: string): string;
|
|
25
26
|
/** Both forms of a request target (`req.url`, a RequestURI, a WSGI path): see the module comment. */
|
|
26
27
|
export declare function canonicalPath(target: string | undefined): CanonicalPath;
|
|
27
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* A protection (require, source maps, skip, ignore) applies when either form matches; build its
|
|
30
|
+
* matcher with protectionMatcher, which ignores case (routers like Express's match /API as /api).
|
|
31
|
+
*/
|
|
28
32
|
export declare function matchesEither(matcher: (path: string) => boolean, p: CanonicalPath): boolean;
|
|
29
33
|
/** An exemption (block.allow) applies only when both forms match. */
|
|
30
34
|
export declare function matchesBoth(matcher: (path: string) => boolean, p: CanonicalPath): boolean;
|
|
@@ -52,6 +52,16 @@ export interface BrowserHeaders {
|
|
|
52
52
|
* devices stay coherent: only fetch metadata depends on the version, and only for current ones.
|
|
53
53
|
*/
|
|
54
54
|
export declare function browserIncoherence(h: BrowserHeaders): string[];
|
|
55
|
+
/**
|
|
56
|
+
* Whether a request presents as a person's browser: a browser's user agent (not a tool's), the
|
|
57
|
+
* fetch metadata every current browser sends to a secure origin (Sec-Fetch-Dest), and nothing
|
|
58
|
+
* incoherent (Accept-Language present, client hints matching the user agent). A script that adds
|
|
59
|
+
* one browser header doesn't pass. Such requests get to run the page and earn their own verdict:
|
|
60
|
+
* pace and claims keyed by an address never apply to them.
|
|
61
|
+
*/
|
|
62
|
+
export declare function presentsAsBrowser(h: BrowserHeaders & {
|
|
63
|
+
secFetchDest?: string;
|
|
64
|
+
}): boolean;
|
|
55
65
|
/**
|
|
56
66
|
* Paths scanners and agents probe and normal browsing never requests. Matched on the path only;
|
|
57
67
|
* the forced-browsing evidence needs several distinct families in one investigation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reguard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
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",
|
|
@@ -113,7 +113,8 @@
|
|
|
113
113
|
"url": "https://github.com/cadenticai/reguard-js/issues"
|
|
114
114
|
},
|
|
115
115
|
"devDependencies": {
|
|
116
|
-
"@re-detect/shared": "0.1.0"
|
|
116
|
+
"@re-detect/shared": "0.1.0",
|
|
117
|
+
"fastify": "5.12.8"
|
|
117
118
|
},
|
|
118
119
|
"publishConfig": {
|
|
119
120
|
"access": "public"
|