@crabcrop/mcp 0.0.0-stage → 0.9.11
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 +62 -2
- package/dist/index.js +424 -0
- package/node_modules/@crabcrop/url/README.md +97 -0
- package/node_modules/@crabcrop/url/dist/explain.d.ts +42 -0
- package/node_modules/@crabcrop/url/dist/explain.js +1050 -0
- package/node_modules/@crabcrop/url/dist/index.d.ts +161 -0
- package/node_modules/@crabcrop/url/dist/index.js +347 -0
- package/node_modules/@crabcrop/url/dist/operations.d.ts +285 -0
- package/node_modules/@crabcrop/url/dist/operations.js +722 -0
- package/node_modules/@crabcrop/url/package.json +31 -0
- package/package.json +48 -4
- package/server.json +1 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute the signature segment for a remainder: the lowercase
|
|
3
|
+
* hex HMAC-SHA256 of `remainder` under `secret`. The remainder
|
|
4
|
+
* is everything after the signature segment in the URL,
|
|
5
|
+
* exactly as it will appear.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sign(secret: string, remainder: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The purge signature for a target path (the request
|
|
10
|
+
* remainder without its signature): HMAC-SHA256 of the
|
|
11
|
+
* path under a subkey derived from the secret (the raw
|
|
12
|
+
* HMAC of the secret over the label `crabcrop/purge-key`),
|
|
13
|
+
* so an image signature can never be replayed as a purge
|
|
14
|
+
* signature. This is the signature segment for
|
|
15
|
+
* `POST /purge/<signature>/<path>`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function purgeSignature(secret: string, target: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* The purge-group signature for a cache group: HMAC-SHA256
|
|
20
|
+
* of the group name under a subkey derived from the secret
|
|
21
|
+
* (the raw HMAC of the secret over the label
|
|
22
|
+
* `crabcrop/purge-group-key`), its own domain beside the
|
|
23
|
+
* purge subkey, so a per-target purge signature can never
|
|
24
|
+
* be transplanted to the purge-group endpoint. The helper
|
|
25
|
+
* exists so nobody has to know the signed string. This is
|
|
26
|
+
* the signature segment for
|
|
27
|
+
* `POST /purge-group/<signature>/<group>`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function purgeGroupSignature(secret: string, group: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* The upload signature for an object name: HMAC-SHA256 of
|
|
32
|
+
* the name under a subkey derived from the secret (the raw
|
|
33
|
+
* HMAC of the secret over the label `crabcrop/upload-key`),
|
|
34
|
+
* so neither an image signature nor a purge signature can
|
|
35
|
+
* ever be replayed as an upload signature. This is the
|
|
36
|
+
* signature segment for `PUT /upload/<signature>/<name>`.
|
|
37
|
+
*
|
|
38
|
+
* With `expires` (unix seconds), the signature covers
|
|
39
|
+
* `exp:<expires>/<name>` instead, and the request path is
|
|
40
|
+
* `PUT /upload/<signature>/exp:<expires>/<name>`: the
|
|
41
|
+
* expiry sits inside the signed string, so it can neither
|
|
42
|
+
* be stripped nor extended, and the server refuses the
|
|
43
|
+
* upload once that second is reached. Delegated upload
|
|
44
|
+
* tokens should always use the expiring form; without it
|
|
45
|
+
* a token replays until the secret rotates.
|
|
46
|
+
*/
|
|
47
|
+
export declare function uploadSignature(secret: string, name: string, expires?: number): string;
|
|
48
|
+
export interface BuildOptions {
|
|
49
|
+
/** Unix-seconds expiry, emitted as a leading `exp:` segment. */
|
|
50
|
+
expires?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Key ID to stamp on the signature segment (`<key-id>:<tag>`),
|
|
53
|
+
* for servers running a secret rotation: they verify a keyed
|
|
54
|
+
* segment against the named key only. Requires a non-null
|
|
55
|
+
* secret (an `unsafe` URL has no key to name).
|
|
56
|
+
*/
|
|
57
|
+
keyId?: string;
|
|
58
|
+
}
|
|
59
|
+
/** Whether `id` is a well-formed key ID (1-32 of `[A-Za-z0-9_-]`). */
|
|
60
|
+
export declare function isValidKeyId(id: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* The signature segment for a keyed signature: `<key-id>:`
|
|
63
|
+
* followed by the hex tag `sign` computes.
|
|
64
|
+
*/
|
|
65
|
+
export declare function signWithKeyId(secret: string, keyId: string, remainder: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* The largest remainder {@link seal} will encrypt, in bytes
|
|
68
|
+
* (issue #255). A recipe longer than this belongs in a
|
|
69
|
+
* preset; the cap also bounds the token a server must
|
|
70
|
+
* decode.
|
|
71
|
+
*/
|
|
72
|
+
export declare const MAX_SEALED_REMAINDER = 2048;
|
|
73
|
+
/**
|
|
74
|
+
* Seal a remainder into a token segment with a
|
|
75
|
+
* caller-supplied nonce: `s1.<base64url(nonce ||
|
|
76
|
+
* ciphertext || tag)>`, AES-256-GCM under the derived seal
|
|
77
|
+
* subkey with the fixed associated data `crabcrop/sealed/1`.
|
|
78
|
+
*
|
|
79
|
+
* DANGER: a nonce must never repeat under one secret.
|
|
80
|
+
* Reusing a nonce with AES-GCM destroys the construction's
|
|
81
|
+
* confidentiality and integrity guarantees. This function
|
|
82
|
+
* is the deterministic seam the spec's test vectors use;
|
|
83
|
+
* production minting calls {@link seal}, which draws a
|
|
84
|
+
* fresh random nonce every time.
|
|
85
|
+
*/
|
|
86
|
+
export declare function sealWithNonce(secret: string, remainder: string, nonce: Uint8Array, keyId?: string): string;
|
|
87
|
+
/**
|
|
88
|
+
* Seal a remainder into a token segment (issue #255), with
|
|
89
|
+
* a fresh random 96-bit nonce. The token is the whole
|
|
90
|
+
* signature segment; pass `keyId` to stamp `<key-id>:`
|
|
91
|
+
* in front of it, so servers in a rotation window open it
|
|
92
|
+
* against the named key only.
|
|
93
|
+
*/
|
|
94
|
+
export declare function seal(secret: string, remainder: string, keyId?: string): string;
|
|
95
|
+
/**
|
|
96
|
+
* Open a token segment, with or without a `<key-id>:`
|
|
97
|
+
* prefix, under one secret, returning the remainder it
|
|
98
|
+
* seals. The key ID is routing metadata and is not
|
|
99
|
+
* consulted: the question is whether this secret opens the
|
|
100
|
+
* token. Every failure (malformed body, wrong secret,
|
|
101
|
+
* tampered bytes, unknown version) throws the one error,
|
|
102
|
+
* `the token did not open under this secret`: openers must
|
|
103
|
+
* never learn which stage failed.
|
|
104
|
+
*/
|
|
105
|
+
export declare function unseal(secret: string, segment: string): string;
|
|
106
|
+
/** Options for {@link buildSealedURL}. */
|
|
107
|
+
export interface SealOptions {
|
|
108
|
+
/** Unix-seconds expiry, sealed inside the token as a leading `exp:` segment. */
|
|
109
|
+
expires?: number;
|
|
110
|
+
/** Key ID to stamp on the token segment (`<key-id>:s1.…`), for rotation. */
|
|
111
|
+
keyId?: string;
|
|
112
|
+
/**
|
|
113
|
+
* The cosmetic friendly name after the token. The server
|
|
114
|
+
* never parses, validates, or authenticates it; one
|
|
115
|
+
* filename-like segment is the dependable shape. Omit it
|
|
116
|
+
* and the URL ends at the token.
|
|
117
|
+
*/
|
|
118
|
+
name?: string;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Assemble a sealed crabcrop URL:
|
|
122
|
+
* `{base}/[<key-id>:]s1.<token>/{friendly-name}` (issue
|
|
123
|
+
* #255). The remainder is assembled exactly as
|
|
124
|
+
* {@link buildURL} assembles it (expiry segment,
|
|
125
|
+
* operations, source) and then sealed whole, pre-expansion,
|
|
126
|
+
* so the sealed and plain forms of one render are one cache
|
|
127
|
+
* entry and one purge target on the server. A secret is
|
|
128
|
+
* required: there is no `unsafe` spelling of a token.
|
|
129
|
+
*/
|
|
130
|
+
export declare function buildSealedURL(base: string, secret: string, operations: string, source: string, options?: SealOptions): string;
|
|
131
|
+
/**
|
|
132
|
+
* Assemble a signed (or `unsafe`) crabcrop URL. `operations`
|
|
133
|
+
* is the operation string verbatim (several `/`-separated
|
|
134
|
+
* segments and `preset:` references included). A null secret
|
|
135
|
+
* produces the literal `unsafe` signature segment, for servers
|
|
136
|
+
* running with unsafe or presets-only configurations.
|
|
137
|
+
*/
|
|
138
|
+
export declare function buildURL(base: string, secret: string | null, operations: string, source: string, options?: BuildOptions): string;
|
|
139
|
+
/**
|
|
140
|
+
* Build an `srcset` attribute value: one `{width}x0` URL per
|
|
141
|
+
* width, entries formatted `{url} {width}w` and joined by
|
|
142
|
+
* `, `. An expiry, when given, applies to every entry.
|
|
143
|
+
*/
|
|
144
|
+
export declare function buildSrcSet(base: string, secret: string | null, source: string, widths: number[], options?: BuildOptions): string;
|
|
145
|
+
export { explainUrl } from "./explain.ts";
|
|
146
|
+
export { FILTER_DESCRIPTIONS } from "./explain.ts";
|
|
147
|
+
export type { ExplainedSegment, Explanation, SegmentKind, SignatureKind } from "./explain.ts";
|
|
148
|
+
export { filter, joinOps, ops } from "./operations.ts";
|
|
149
|
+
export type { Anchor, BlendMode, CropAlign, CurveChannel, Dimension, ImageFormat, MaskShape, StudioMode, StudioOptions, StudioShadow, } from "./operations.ts";
|
|
150
|
+
/**
|
|
151
|
+
* Verify a URL's signature locally. Returns true or false, or
|
|
152
|
+
* null when the URL is unsigned or malformed.
|
|
153
|
+
*
|
|
154
|
+
* With a single secret string, the key ID (when the URL
|
|
155
|
+
* carries one) is ignored: the question is whether this
|
|
156
|
+
* secret made the tag. With a keyring (a map of key ID to
|
|
157
|
+
* secret), the server's rotation semantics apply: a keyed
|
|
158
|
+
* URL consults exactly its named key (an unknown ID is
|
|
159
|
+
* false), and an unkeyed URL consults every key in the map.
|
|
160
|
+
*/
|
|
161
|
+
export declare function verifyUrlSignature(secret: string | Record<string, string>, input: string): boolean | null;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build and sign crabcrop URLs, per `spec/url-signing.md` in
|
|
3
|
+
* the crabcrop repository. Standard library only.
|
|
4
|
+
*/
|
|
5
|
+
import { createCipheriv, createDecipheriv, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
6
|
+
/**
|
|
7
|
+
* Compute the signature segment for a remainder: the lowercase
|
|
8
|
+
* hex HMAC-SHA256 of `remainder` under `secret`. The remainder
|
|
9
|
+
* is everything after the signature segment in the URL,
|
|
10
|
+
* exactly as it will appear.
|
|
11
|
+
*/
|
|
12
|
+
export function sign(secret, remainder) {
|
|
13
|
+
return createHmac("sha256", secret).update(remainder, "utf8").digest("hex");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The purge signature for a target path (the request
|
|
17
|
+
* remainder without its signature): HMAC-SHA256 of the
|
|
18
|
+
* path under a subkey derived from the secret (the raw
|
|
19
|
+
* HMAC of the secret over the label `crabcrop/purge-key`),
|
|
20
|
+
* so an image signature can never be replayed as a purge
|
|
21
|
+
* signature. This is the signature segment for
|
|
22
|
+
* `POST /purge/<signature>/<path>`.
|
|
23
|
+
*/
|
|
24
|
+
export function purgeSignature(secret, target) {
|
|
25
|
+
const subkey = Buffer.from(sign(secret, "crabcrop/purge-key"), "hex");
|
|
26
|
+
return createHmac("sha256", subkey).update(target, "utf8").digest("hex");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The purge-group signature for a cache group: HMAC-SHA256
|
|
30
|
+
* of the group name under a subkey derived from the secret
|
|
31
|
+
* (the raw HMAC of the secret over the label
|
|
32
|
+
* `crabcrop/purge-group-key`), its own domain beside the
|
|
33
|
+
* purge subkey, so a per-target purge signature can never
|
|
34
|
+
* be transplanted to the purge-group endpoint. The helper
|
|
35
|
+
* exists so nobody has to know the signed string. This is
|
|
36
|
+
* the signature segment for
|
|
37
|
+
* `POST /purge-group/<signature>/<group>`.
|
|
38
|
+
*/
|
|
39
|
+
export function purgeGroupSignature(secret, group) {
|
|
40
|
+
const subkey = Buffer.from(sign(secret, "crabcrop/purge-group-key"), "hex");
|
|
41
|
+
return createHmac("sha256", subkey).update(group, "utf8").digest("hex");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The upload signature for an object name: HMAC-SHA256 of
|
|
45
|
+
* the name under a subkey derived from the secret (the raw
|
|
46
|
+
* HMAC of the secret over the label `crabcrop/upload-key`),
|
|
47
|
+
* so neither an image signature nor a purge signature can
|
|
48
|
+
* ever be replayed as an upload signature. This is the
|
|
49
|
+
* signature segment for `PUT /upload/<signature>/<name>`.
|
|
50
|
+
*
|
|
51
|
+
* With `expires` (unix seconds), the signature covers
|
|
52
|
+
* `exp:<expires>/<name>` instead, and the request path is
|
|
53
|
+
* `PUT /upload/<signature>/exp:<expires>/<name>`: the
|
|
54
|
+
* expiry sits inside the signed string, so it can neither
|
|
55
|
+
* be stripped nor extended, and the server refuses the
|
|
56
|
+
* upload once that second is reached. Delegated upload
|
|
57
|
+
* tokens should always use the expiring form; without it
|
|
58
|
+
* a token replays until the secret rotates.
|
|
59
|
+
*/
|
|
60
|
+
export function uploadSignature(secret, name, expires) {
|
|
61
|
+
const subkey = Buffer.from(sign(secret, "crabcrop/upload-key"), "hex");
|
|
62
|
+
const signed = expires === undefined ? name : `exp:${expires}/${name}`;
|
|
63
|
+
return createHmac("sha256", subkey).update(signed, "utf8").digest("hex");
|
|
64
|
+
}
|
|
65
|
+
/** Whether `id` is a well-formed key ID (1-32 of `[A-Za-z0-9_-]`). */
|
|
66
|
+
export function isValidKeyId(id) {
|
|
67
|
+
return /^[A-Za-z0-9_-]{1,32}$/.test(id);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The signature segment for a keyed signature: `<key-id>:`
|
|
71
|
+
* followed by the hex tag `sign` computes.
|
|
72
|
+
*/
|
|
73
|
+
export function signWithKeyId(secret, keyId, remainder) {
|
|
74
|
+
if (!isValidKeyId(keyId)) {
|
|
75
|
+
throw new Error(`invalid key ID ${JSON.stringify(keyId)}: 1 to 32 characters of [A-Za-z0-9_-]`);
|
|
76
|
+
}
|
|
77
|
+
return `${keyId}:${sign(secret, remainder)}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The largest remainder {@link seal} will encrypt, in bytes
|
|
81
|
+
* (issue #255). A recipe longer than this belongs in a
|
|
82
|
+
* preset; the cap also bounds the token a server must
|
|
83
|
+
* decode.
|
|
84
|
+
*/
|
|
85
|
+
export const MAX_SEALED_REMAINDER = 2048;
|
|
86
|
+
/** The AAD every sealed token is bound to. */
|
|
87
|
+
const SEAL_AAD = Buffer.from("crabcrop/sealed/1", "utf8");
|
|
88
|
+
/**
|
|
89
|
+
* The seal subkey: the raw bytes of the HMAC-SHA256 of the
|
|
90
|
+
* label `crabcrop/seal-key` under the secret, its own domain
|
|
91
|
+
* beside purge, purge-group, and upload, so there is no
|
|
92
|
+
* second secret to configure or rotate: each keyring entry
|
|
93
|
+
* yields its own seal subkey, and the rotation window for
|
|
94
|
+
* signatures is the rotation window for tokens.
|
|
95
|
+
*/
|
|
96
|
+
function sealKey(secret) {
|
|
97
|
+
return Buffer.from(sign(secret, "crabcrop/seal-key"), "hex");
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Seal a remainder into a token segment with a
|
|
101
|
+
* caller-supplied nonce: `s1.<base64url(nonce ||
|
|
102
|
+
* ciphertext || tag)>`, AES-256-GCM under the derived seal
|
|
103
|
+
* subkey with the fixed associated data `crabcrop/sealed/1`.
|
|
104
|
+
*
|
|
105
|
+
* DANGER: a nonce must never repeat under one secret.
|
|
106
|
+
* Reusing a nonce with AES-GCM destroys the construction's
|
|
107
|
+
* confidentiality and integrity guarantees. This function
|
|
108
|
+
* is the deterministic seam the spec's test vectors use;
|
|
109
|
+
* production minting calls {@link seal}, which draws a
|
|
110
|
+
* fresh random nonce every time.
|
|
111
|
+
*/
|
|
112
|
+
export function sealWithNonce(secret, remainder, nonce, keyId) {
|
|
113
|
+
if (nonce.length !== 12) {
|
|
114
|
+
throw new Error(`sealed token nonce must be 12 bytes, got ${nonce.length}`);
|
|
115
|
+
}
|
|
116
|
+
if (Buffer.byteLength(remainder, "utf8") > MAX_SEALED_REMAINDER) {
|
|
117
|
+
throw new Error(`remainder is over the ${MAX_SEALED_REMAINDER}-byte sealing cap: a recipe that long belongs in a preset`);
|
|
118
|
+
}
|
|
119
|
+
const cipher = createCipheriv("aes-256-gcm", sealKey(secret), Buffer.from(nonce));
|
|
120
|
+
cipher.setAAD(SEAL_AAD);
|
|
121
|
+
const ciphertext = Buffer.concat([cipher.update(remainder, "utf8"), cipher.final()]);
|
|
122
|
+
const body = Buffer.concat([Buffer.from(nonce), ciphertext, cipher.getAuthTag()]);
|
|
123
|
+
const token = `s1.${body.toString("base64url")}`;
|
|
124
|
+
if (keyId === undefined) {
|
|
125
|
+
return token;
|
|
126
|
+
}
|
|
127
|
+
if (!isValidKeyId(keyId)) {
|
|
128
|
+
throw new Error(`invalid key ID ${JSON.stringify(keyId)}: 1 to 32 characters of [A-Za-z0-9_-]`);
|
|
129
|
+
}
|
|
130
|
+
return `${keyId}:${token}`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Seal a remainder into a token segment (issue #255), with
|
|
134
|
+
* a fresh random 96-bit nonce. The token is the whole
|
|
135
|
+
* signature segment; pass `keyId` to stamp `<key-id>:`
|
|
136
|
+
* in front of it, so servers in a rotation window open it
|
|
137
|
+
* against the named key only.
|
|
138
|
+
*/
|
|
139
|
+
export function seal(secret, remainder, keyId) {
|
|
140
|
+
return sealWithNonce(secret, remainder, randomBytes(12), keyId);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Open a token segment, with or without a `<key-id>:`
|
|
144
|
+
* prefix, under one secret, returning the remainder it
|
|
145
|
+
* seals. The key ID is routing metadata and is not
|
|
146
|
+
* consulted: the question is whether this secret opens the
|
|
147
|
+
* token. Every failure (malformed body, wrong secret,
|
|
148
|
+
* tampered bytes, unknown version) throws the one error,
|
|
149
|
+
* `the token did not open under this secret`: openers must
|
|
150
|
+
* never learn which stage failed.
|
|
151
|
+
*/
|
|
152
|
+
export function unseal(secret, segment) {
|
|
153
|
+
const openFailed = () => new Error("the token did not open under this secret");
|
|
154
|
+
let token = segment;
|
|
155
|
+
const colon = segment.indexOf(":");
|
|
156
|
+
if (colon >= 0) {
|
|
157
|
+
if (!isValidKeyId(segment.slice(0, colon))) {
|
|
158
|
+
throw openFailed();
|
|
159
|
+
}
|
|
160
|
+
token = segment.slice(colon + 1);
|
|
161
|
+
}
|
|
162
|
+
if (!token.startsWith("s1.")) {
|
|
163
|
+
throw openFailed();
|
|
164
|
+
}
|
|
165
|
+
const body = token.slice(3);
|
|
166
|
+
// Node's base64 decoder is lenient (it skips characters
|
|
167
|
+
// outside the alphabet), so canonicity is checked by
|
|
168
|
+
// re-encoding: anything but the exact minted spelling
|
|
169
|
+
// fails here.
|
|
170
|
+
const raw = Buffer.from(body, "base64url");
|
|
171
|
+
if (raw.length < 12 + 16 || raw.toString("base64url") !== body) {
|
|
172
|
+
throw openFailed();
|
|
173
|
+
}
|
|
174
|
+
const decipher = createDecipheriv("aes-256-gcm", sealKey(secret), raw.subarray(0, 12));
|
|
175
|
+
decipher.setAAD(SEAL_AAD);
|
|
176
|
+
decipher.setAuthTag(raw.subarray(raw.length - 16));
|
|
177
|
+
let plain;
|
|
178
|
+
try {
|
|
179
|
+
plain = Buffer.concat([
|
|
180
|
+
decipher.update(raw.subarray(12, raw.length - 16)),
|
|
181
|
+
decipher.final(),
|
|
182
|
+
]);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
throw openFailed();
|
|
186
|
+
}
|
|
187
|
+
const text = plain.toString("utf8");
|
|
188
|
+
if (!Buffer.from(text, "utf8").equals(plain)) {
|
|
189
|
+
throw openFailed();
|
|
190
|
+
}
|
|
191
|
+
return text;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Assemble a sealed crabcrop URL:
|
|
195
|
+
* `{base}/[<key-id>:]s1.<token>/{friendly-name}` (issue
|
|
196
|
+
* #255). The remainder is assembled exactly as
|
|
197
|
+
* {@link buildURL} assembles it (expiry segment,
|
|
198
|
+
* operations, source) and then sealed whole, pre-expansion,
|
|
199
|
+
* so the sealed and plain forms of one render are one cache
|
|
200
|
+
* entry and one purge target on the server. A secret is
|
|
201
|
+
* required: there is no `unsafe` spelling of a token.
|
|
202
|
+
*/
|
|
203
|
+
export function buildSealedURL(base, secret, operations, source, options = {}) {
|
|
204
|
+
if (secret === null || secret === undefined) {
|
|
205
|
+
throw new Error("sealing requires a secret: there is no unsafe token");
|
|
206
|
+
}
|
|
207
|
+
if (options.expires !== undefined) {
|
|
208
|
+
assertExpires(options.expires);
|
|
209
|
+
}
|
|
210
|
+
assertUrlText(operations, "operations");
|
|
211
|
+
assertUrlText(source, "source");
|
|
212
|
+
const trimmedBase = base.replace(/\/+$/, "");
|
|
213
|
+
let remainder = "";
|
|
214
|
+
if (options.expires !== undefined) {
|
|
215
|
+
remainder += `exp:${options.expires}/`;
|
|
216
|
+
}
|
|
217
|
+
if (operations) {
|
|
218
|
+
remainder += `${operations}/`;
|
|
219
|
+
}
|
|
220
|
+
remainder += source;
|
|
221
|
+
const segment = seal(secret, remainder, options.keyId);
|
|
222
|
+
return options.name ? `${trimmedBase}/${segment}/${options.name}` : `${trimmedBase}/${segment}`;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Fail loudly on inputs that can never produce a verifiable URL
|
|
226
|
+
* (audit P-4, P-5): the remainder travels as a raw URL path, so
|
|
227
|
+
* `?`/`#` would truncate it and whitespace, control, and
|
|
228
|
+
* non-ASCII characters cannot be sent as-is; an expiry must be
|
|
229
|
+
* a non-negative integer the server's u64 parser accepts.
|
|
230
|
+
*/
|
|
231
|
+
function assertUrlText(text, what) {
|
|
232
|
+
for (const c of text) {
|
|
233
|
+
const code = c.codePointAt(0);
|
|
234
|
+
if (c === "?" || c === "#" || code <= 0x20 || code === 0x7f || code > 0x7e) {
|
|
235
|
+
throw new Error(`${what} contains a character that cannot appear in a crabcrop URL: ${JSON.stringify(c)}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function assertExpires(expires) {
|
|
240
|
+
if (!Number.isSafeInteger(expires) || expires < 0) {
|
|
241
|
+
throw new Error(`expires must be a non-negative integer number of unix seconds, got ${expires}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* A srcset width becomes a `{width}x0` size segment, so it
|
|
246
|
+
* must satisfy the grammar's dimension rules: a positive
|
|
247
|
+
* integer within the server cap. Anything else mints a
|
|
248
|
+
* URL the server rejects (or worse, `-300` reads as a
|
|
249
|
+
* flip), so it throws here, at build time.
|
|
250
|
+
*/
|
|
251
|
+
function assertWidth(width) {
|
|
252
|
+
if (!Number.isSafeInteger(width) || width < 1 || width > 16383) {
|
|
253
|
+
throw new Error(`srcset width must be a positive integer no larger than 16383, got ${width}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Assemble a signed (or `unsafe`) crabcrop URL. `operations`
|
|
258
|
+
* is the operation string verbatim (several `/`-separated
|
|
259
|
+
* segments and `preset:` references included). A null secret
|
|
260
|
+
* produces the literal `unsafe` signature segment, for servers
|
|
261
|
+
* running with unsafe or presets-only configurations.
|
|
262
|
+
*/
|
|
263
|
+
export function buildURL(base, secret, operations, source, options = {}) {
|
|
264
|
+
if (options.expires !== undefined) {
|
|
265
|
+
assertExpires(options.expires);
|
|
266
|
+
}
|
|
267
|
+
assertUrlText(operations, "operations");
|
|
268
|
+
assertUrlText(source, "source");
|
|
269
|
+
const trimmedBase = base.replace(/\/+$/, "");
|
|
270
|
+
let remainder = "";
|
|
271
|
+
if (options.expires !== undefined) {
|
|
272
|
+
remainder += `exp:${options.expires}/`;
|
|
273
|
+
}
|
|
274
|
+
if (operations) {
|
|
275
|
+
remainder += `${operations}/`;
|
|
276
|
+
}
|
|
277
|
+
remainder += source;
|
|
278
|
+
let signature;
|
|
279
|
+
if (secret === null) {
|
|
280
|
+
if (options.keyId !== undefined) {
|
|
281
|
+
throw new Error("keyId requires a secret: an unsafe URL has no key to name");
|
|
282
|
+
}
|
|
283
|
+
signature = "unsafe";
|
|
284
|
+
}
|
|
285
|
+
else if (options.keyId !== undefined) {
|
|
286
|
+
signature = signWithKeyId(secret, options.keyId, remainder);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
signature = sign(secret, remainder);
|
|
290
|
+
}
|
|
291
|
+
return `${trimmedBase}/${signature}/${remainder}`;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Build an `srcset` attribute value: one `{width}x0` URL per
|
|
295
|
+
* width, entries formatted `{url} {width}w` and joined by
|
|
296
|
+
* `, `. An expiry, when given, applies to every entry.
|
|
297
|
+
*/
|
|
298
|
+
export function buildSrcSet(base, secret, source, widths, options = {}) {
|
|
299
|
+
for (const w of widths) {
|
|
300
|
+
assertWidth(w);
|
|
301
|
+
}
|
|
302
|
+
return widths
|
|
303
|
+
.map((w) => `${buildURL(base, secret, `${w}x0`, source, options)} ${w}w`)
|
|
304
|
+
.join(", ");
|
|
305
|
+
}
|
|
306
|
+
export { explainUrl } from "./explain.js";
|
|
307
|
+
export { FILTER_DESCRIPTIONS } from "./explain.js";
|
|
308
|
+
export { filter, joinOps, ops } from "./operations.js";
|
|
309
|
+
import { explainUrl } from "./explain.js";
|
|
310
|
+
/**
|
|
311
|
+
* Verify a URL's signature locally. Returns true or false, or
|
|
312
|
+
* null when the URL is unsigned or malformed.
|
|
313
|
+
*
|
|
314
|
+
* With a single secret string, the key ID (when the URL
|
|
315
|
+
* carries one) is ignored: the question is whether this
|
|
316
|
+
* secret made the tag. With a keyring (a map of key ID to
|
|
317
|
+
* secret), the server's rotation semantics apply: a keyed
|
|
318
|
+
* URL consults exactly its named key (an unknown ID is
|
|
319
|
+
* false), and an unkeyed URL consults every key in the map.
|
|
320
|
+
*/
|
|
321
|
+
export function verifyUrlSignature(secret, input) {
|
|
322
|
+
const { signature, remainder } = explainUrl(input);
|
|
323
|
+
if (!signature || signature.kind !== "hmac") {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
// Constant-time, like the Rust and Python verifiers: a
|
|
327
|
+
// plain === leaks how much of the tag was right.
|
|
328
|
+
const given = Buffer.from(signature.value, "utf8");
|
|
329
|
+
const matches = (candidate) => {
|
|
330
|
+
const expected = Buffer.from(sign(candidate, remainder), "utf8");
|
|
331
|
+
return expected.length === given.length && timingSafeEqual(expected, given);
|
|
332
|
+
};
|
|
333
|
+
if (typeof secret === "string") {
|
|
334
|
+
return matches(secret);
|
|
335
|
+
}
|
|
336
|
+
if (signature.keyId !== undefined) {
|
|
337
|
+
// Own properties only: key IDs are attacker-shaped text
|
|
338
|
+
// (`constructor`, `toString`, `__proto__` are all legal
|
|
339
|
+
// IDs), and an inherited lookup would hand a non-string
|
|
340
|
+
// to the HMAC and throw instead of answering false.
|
|
341
|
+
const candidate = Object.hasOwn(secret, signature.keyId)
|
|
342
|
+
? secret[signature.keyId]
|
|
343
|
+
: undefined;
|
|
344
|
+
return typeof candidate === "string" ? matches(candidate) : false;
|
|
345
|
+
}
|
|
346
|
+
return Object.values(secret).some(matches);
|
|
347
|
+
}
|