@crabcrop/url 0.9.10 → 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/dist/explain.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  /** The segment kinds an explanation is made of. */
9
9
  export type SegmentKind = "expiry" | "maxage" | "debug" | "preset" | "operation";
10
10
  /** The signature kinds an explanation can report. */
11
- export type SignatureKind = "hmac" | "unsafe" | "invalid";
11
+ export type SignatureKind = "hmac" | "unsafe" | "sealed" | "invalid";
12
12
  export interface ExplainedSegment {
13
13
  raw: string;
14
14
  kind: SegmentKind;
package/dist/explain.js CHANGED
@@ -607,6 +607,9 @@ const U64_MAX = 18446744073709551615n;
607
607
  * decoded class names (duplicates folded, first spelling
608
608
  * kept, as the server's canonical form folds them) and the
609
609
  * scale, or null when the server would reject the form.
610
+ * The 32-class cap counts distinct names, as the server's
611
+ * does: repeats are free, and a 33rd distinct name rejects
612
+ * the form (issue #408).
610
613
  */
611
614
  function parseObjectsAlign(align) {
612
615
  if (!align.startsWith("objects("))
@@ -629,8 +632,6 @@ function parseObjectsAlign(align) {
629
632
  return null;
630
633
  }
631
634
  const parts = list.split(",");
632
- if (parts.length > 32)
633
- return null;
634
635
  const classes = [];
635
636
  const seen = new Set();
636
637
  for (const part of parts) {
@@ -645,6 +646,8 @@ function parseObjectsAlign(align) {
645
646
  }
646
647
  const key = name.toLowerCase();
647
648
  if (!seen.has(key)) {
649
+ if (classes.length === 32)
650
+ return null;
648
651
  seen.add(key);
649
652
  classes.push(name);
650
653
  }
@@ -947,6 +950,21 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
947
950
  path = path.slice(0, cut);
948
951
  }
949
952
  const errors = [];
953
+ // A sealed URL is a single segment: the token carries
954
+ // the whole remainder encrypted, so there is no slash
955
+ // and nothing visible to itemize (issue #388).
956
+ const sealedWhole = path.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
957
+ if (sealedWhole) {
958
+ const signature = {
959
+ kind: "sealed",
960
+ value: `s1.${sealedWhole[2]}`,
961
+ detail: "sealed token (AES-256-GCM): the operations and source are encrypted inside it, so there is nothing visible to itemize; the server opens it and serves the URL it carries",
962
+ };
963
+ if (sealedWhole[1]) {
964
+ signature.keyId = sealedWhole[1];
965
+ }
966
+ return { signature, segments: [], source: "", remainder: "", errors: [] };
967
+ }
950
968
  const slash = path.indexOf("/");
951
969
  if (slash === -1) {
952
970
  return { signature: null, segments: [], source: "", remainder: "", errors: ["no signature segment: the path is a single segment"] };
@@ -955,6 +973,22 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
955
973
  const remainder = path.slice(slash + 1);
956
974
  let signature;
957
975
  const keyed = signatureRaw.match(/^([A-Za-z0-9_-]{1,32}):([0-9a-f]{64})$/);
976
+ const sealedTok = signatureRaw.match(/^(?:([A-Za-z0-9_-]{1,32}):)?s1\.([A-Za-z0-9_-]+)$/);
977
+ if (sealedTok) {
978
+ // A sealed token with a trailing path (issue #388):
979
+ // the server opens the token and serves the URL it
980
+ // carries; the visible remainder behind it is not
981
+ // part of that URL, so there is nothing to itemize.
982
+ signature = {
983
+ kind: "sealed",
984
+ value: `s1.${sealedTok[2]}`,
985
+ detail: "sealed token (AES-256-GCM): the operations and source are encrypted inside it; the server opens it and serves the URL it carries, and anything visible after the token is not part of that URL",
986
+ };
987
+ if (sealedTok[1]) {
988
+ signature.keyId = sealedTok[1];
989
+ }
990
+ return { signature, segments: [], source: "", remainder, errors };
991
+ }
958
992
  if (signatureRaw === "unsafe") {
959
993
  signature = { kind: "unsafe", value: signatureRaw, detail: "unsigned; the server honors it only with CRABCROP_ALLOW_UNSAFE or in presets-only deployments" };
960
994
  }
@@ -968,6 +1002,10 @@ export function explainUrl(input, nowSeconds = Math.floor(Date.now() / 1000)) {
968
1002
  signature = { kind: "invalid", value: signatureRaw, detail: "lowercase hex, but not a full 64-character HMAC tag: the server will answer 403" };
969
1003
  errors.push("malformed signature segment");
970
1004
  }
1005
+ else if (/^(?:[A-Za-z0-9_-]{1,32}:)?s\d+\./.test(signatureRaw)) {
1006
+ signature = { kind: "invalid", value: signatureRaw, detail: "a sealed-token shape this scheme does not open (an unknown version marker, or a malformed token body): the server answers the uniform 403 refusal, indistinguishable from a token that fails to open" };
1007
+ errors.push("malformed signature segment");
1008
+ }
971
1009
  else {
972
1010
  signature = { kind: "invalid", value: signatureRaw, detail: "not `unsafe` and not lowercase hex: the server rejects it as malformed (400)" };
973
1011
  errors.push("malformed signature segment");
@@ -95,7 +95,9 @@ export declare const ops: {
95
95
  * none of the classes is detected the render falls back
96
96
  * to the plain center crop of the same size. Duplicate
97
97
  * classes (case-insensitive) are spelled once, as the
98
- * server's canonical form spells them.
98
+ * server's canonical form spells them; at most 32
99
+ * distinct classes, and a 33rd throws, since the server
100
+ * rejects it with `invalid_crop`.
99
101
  */
100
102
  cropObjects(width: number, height: number, classes: string[], scale?: number): string;
101
103
  /** `crop-rect:10x20:110x220` (left x top : right x bottom). */
@@ -164,18 +164,25 @@ export const ops = {
164
164
  * none of the classes is detected the render falls back
165
165
  * to the plain center crop of the same size. Duplicate
166
166
  * classes (case-insensitive) are spelled once, as the
167
- * server's canonical form spells them.
167
+ * server's canonical form spells them; at most 32
168
+ * distinct classes, and a 33rd throws, since the server
169
+ * rejects it with `invalid_crop`.
168
170
  */
169
171
  cropObjects(width, height, classes, scale = 1) {
170
- if (!Array.isArray(classes) || classes.length < 1 || classes.length > 32) {
171
- fail("crop objects classes", classes, "an array of 1 to 32 class names");
172
+ if (!Array.isArray(classes) || classes.length < 1) {
173
+ fail("crop objects classes", classes, "an array of 1 to 32 distinct class names");
172
174
  }
175
+ // The cap counts distinct names, as the server's parser
176
+ // does (issue #408): repeats fold first and cost nothing.
173
177
  const seen = new Set();
174
178
  const names = [];
175
179
  for (const name of classes) {
176
180
  const encoded = className("crop objects class", name);
177
181
  const key = typeof name === "string" ? name.trim().toLowerCase() : "";
178
182
  if (!seen.has(key)) {
183
+ if (names.length === 32) {
184
+ fail("crop objects classes", classes, "an array of 1 to 32 distinct class names");
185
+ }
179
186
  seen.add(key);
180
187
  names.push(encoded);
181
188
  }
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "test": "node --test"
22
22
  },
23
23
  "type": "module",
24
- "version": "0.9.10",
24
+ "version": "0.9.11",
25
25
  "devDependencies": {
26
26
  "@types/node": "^26.6.5",
27
27
  "typescript": "^7.0.2"