@snowcone-app/mockup-resolver 1.0.0 → 1.0.1
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/CHANGELOG.md +8 -0
- package/README.md +21 -0
- package/package.json +2 -2
- package/src/index.ts +101 -18
- package/src/public-params.ts +36 -0
- package/src/resolve.ts +42 -0
- package/test/integration/mockup-resolver.test.ts +22 -0
- package/test/integration/worker.test.ts +15 -0
- package/test/public-params.test.ts +122 -0
- package/test/resolve.test.ts +84 -1
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -96,6 +96,27 @@ curl "https://api.snowcone.app/v1/url-signer/sign?url=https://example.com/resour
|
|
|
96
96
|
curl "https://api.snowcone.app/v1/url-signer/verify?url=https://example.com/resource.jpg?signature=xxx&expires=123456789"
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
+
### Blank items (`asset=default`)
|
|
100
|
+
|
|
101
|
+
A reserved asset **value** renders a blank (un-printed) item, for every shop with
|
|
102
|
+
zero setup. Pass `default` in place of an artwork URL:
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
https://img.snowcone.app/{productCode}?asset=default&shop=...
|
|
106
|
+
https://img.snowcone.app/{productCode}?asset.front=default&shop=... # per-placement
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The resolver substitutes the transparent `/_blank.png` this worker serves itself
|
|
110
|
+
(1×1, composites to nothing → a bare mockup). No hosting, no allowlist entry, no
|
|
111
|
+
per-shop setup.
|
|
112
|
+
|
|
113
|
+
Because the resolver injects this asset, it is **exempt from the L2 asset-origin
|
|
114
|
+
allowlist by provenance** (L2 only polices developer-supplied hosts). Omitting
|
|
115
|
+
artwork entirely still returns `400` — `default` is an explicit opt-in.
|
|
116
|
+
|
|
117
|
+
Future: a per-shop brand override could resolve `default` to the shop's own mark
|
|
118
|
+
instead of the transparent blank — the same URL, no caller change. Not built yet.
|
|
119
|
+
|
|
99
120
|
## Configuration
|
|
100
121
|
|
|
101
122
|
The service is configured using `wrangler.toml`:
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/mockup-resolver",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "URL signing and verification Cloudflare Worker",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@snowcone-app/mockup-url": "0.1.
|
|
9
|
+
"@snowcone-app/mockup-url": "0.1.1"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@cloudflare/workers-types": "^4.20250303.0",
|
package/src/index.ts
CHANGED
|
@@ -69,6 +69,16 @@ interface AccountData {
|
|
|
69
69
|
requireSig?: boolean;
|
|
70
70
|
/** Explicit kill switch — a revoked key 403s regardless of any other config. */
|
|
71
71
|
revoked?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Set when a sandbox shop has been RELEASED (ADR-0078). The backend leaves a
|
|
74
|
+
* render-only tombstone in KV so already-produced mockup URLs keep resolving
|
|
75
|
+
* after release (a produced mockup is public marketing imagery and must outlive
|
|
76
|
+
* the shop). A retained shop has no apiKeys/l3Secret and `revoked:false`, so the
|
|
77
|
+
* existing resolve path already serves it render-only. The flag is informational
|
|
78
|
+
* here (it lets the resolver/observability distinguish a live shop from a
|
|
79
|
+
* retained-artifact stub) and intentionally does NOT change render behaviour.
|
|
80
|
+
*/
|
|
81
|
+
retained?: boolean;
|
|
72
82
|
}
|
|
73
83
|
|
|
74
84
|
interface ApiKeyData {
|
|
@@ -783,9 +793,11 @@ import {
|
|
|
783
793
|
} from "@snowcone-app/mockup-url";
|
|
784
794
|
import {
|
|
785
795
|
fetchProduct,
|
|
796
|
+
looksLikeResolveRequest,
|
|
786
797
|
resolveDefaults,
|
|
787
798
|
resolveVariant,
|
|
788
799
|
} from "./resolve";
|
|
800
|
+
import { rejectUnknownPublicParams } from "./public-params";
|
|
789
801
|
|
|
790
802
|
// ADR-0073 §10 — resolve + build + sign for `getMockupUrl(assetUrl, productCode)`.
|
|
791
803
|
// The BFF POSTs { accountId, productCode, assetUrl, variant?, mockupId?, placement?, width? }
|
|
@@ -1016,6 +1028,56 @@ async function loadAccountByKey(
|
|
|
1016
1028
|
}
|
|
1017
1029
|
}
|
|
1018
1030
|
|
|
1031
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
1032
|
+
// "Blank item" affordance (the homepage's bottom-of-page blanks, but available
|
|
1033
|
+
// to every shop out of the box). A reserved asset VALUE — `asset=default` (or
|
|
1034
|
+
// `asset.<key>=default`) — that the resolver substitutes server-side for the
|
|
1035
|
+
// transparent /_blank.png this worker serves itself (below). So a blank item
|
|
1036
|
+
// needs no hosting, no allowlist entry, and no per-shop setup.
|
|
1037
|
+
//
|
|
1038
|
+
// Because the resolver INJECTS this asset, it is exempt from the L2 asset-origin
|
|
1039
|
+
// allowlist by provenance — L2 polices developer-supplied hosts (anti-theft),
|
|
1040
|
+
// and the house placeholder is not developer-supplied. The "no artwork" contract
|
|
1041
|
+
// is unchanged: omitting artwork entirely still 400s; `default` is an explicit
|
|
1042
|
+
// opt-in. (Asset-origin allowlist context: ADR-0076.)
|
|
1043
|
+
//
|
|
1044
|
+
// Per-shop branding (resolving `default` to the shop's own mark instead of the
|
|
1045
|
+
// transparent blank) is a future addition — the same URL would then render
|
|
1046
|
+
// branding with no caller change. Not built yet.
|
|
1047
|
+
const DEFAULT_ASSET_TOKEN = "default";
|
|
1048
|
+
const isDefaultToken = (v: string | null | undefined): boolean =>
|
|
1049
|
+
v === DEFAULT_ASSET_TOKEN;
|
|
1050
|
+
|
|
1051
|
+
// A 1×1 fully-transparent PNG (68 bytes), served at /_blank.png. Generated as an
|
|
1052
|
+
// 8-bit RGBA PNG with a single transparent pixel; rendercenter composites it to
|
|
1053
|
+
// nothing, so the placement contributes zero pixels → a bare (blank) mockup.
|
|
1054
|
+
const BLANK_PNG_BASE64 =
|
|
1055
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNgAAIAAAUAAen63NgAAAAASUVORK5CYII=";
|
|
1056
|
+
const BLANK_PNG_PATH = "/_blank.png";
|
|
1057
|
+
|
|
1058
|
+
/** Decode a base64 string to bytes (Workers-safe; no Buffer). */
|
|
1059
|
+
function base64ToBytes(b64: string): Uint8Array {
|
|
1060
|
+
const bin = atob(b64);
|
|
1061
|
+
const out = new Uint8Array(bin.length);
|
|
1062
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
1063
|
+
return out;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* The URL the `default` token resolves to: this worker's own transparent
|
|
1068
|
+
* `/_blank.png` on the request's host (img.snowcone.app in prod, the workspace
|
|
1069
|
+
* host in dev) — no separate hosting to keep in sync. (A per-shop brand override
|
|
1070
|
+
* would branch here in future.)
|
|
1071
|
+
*
|
|
1072
|
+
* Scheme is forced to https: both prod and the local dev proxy (caddy/traefik on
|
|
1073
|
+
* *.img.lvh.me) serve the resolver over https ONLY. Behind that TLS-terminating
|
|
1074
|
+
* proxy the worker sees the request as http, so deriving the scheme from
|
|
1075
|
+
* request.url would inject an http asset URL the renderer can't fetch.
|
|
1076
|
+
*/
|
|
1077
|
+
function houseAssetUrl(request: Request): string {
|
|
1078
|
+
return `https://${new URL(request.url).host}${BLANK_PNG_PATH}`;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1019
1081
|
/** Check a hostname against a glob allowlist (reuses globToRegExp). */
|
|
1020
1082
|
function isHostAllowed(host: string, allowlist: string[]): boolean {
|
|
1021
1083
|
if (allowlist.length === 0) return true; // unset = any
|
|
@@ -1047,6 +1109,14 @@ async function handlePublicResolve(
|
|
|
1047
1109
|
|
|
1048
1110
|
const reqUrl = new URL(request.url);
|
|
1049
1111
|
const q = reqUrl.searchParams;
|
|
1112
|
+
|
|
1113
|
+
// Strict params FIRST: any query key outside the public grammar 400s loudly
|
|
1114
|
+
// (naming the key + the allowed grammar) instead of being silently ignored —
|
|
1115
|
+
// a hand-edited `view=<sceneId>` used to render the default scene with zero
|
|
1116
|
+
// feedback. Predicate lives in @snowcone-app/mockup-url (grammar SSOT).
|
|
1117
|
+
const badParams = rejectUnknownPublicParams(reqUrl);
|
|
1118
|
+
if (badParams) return badParams;
|
|
1119
|
+
|
|
1050
1120
|
// The full grammar: bare `asset=` (default placement) OR a multi-placement
|
|
1051
1121
|
// design map (`asset.<key>`/`color.<key>`/`tile.<key>`/`align.<key>`) + variant
|
|
1052
1122
|
// option picks (`opt.<attr>`).
|
|
@@ -1082,12 +1152,18 @@ async function handlePublicResolve(
|
|
|
1082
1152
|
// never be enforced. Anti-theft is the asset-source allowlist (L2, below) plus
|
|
1083
1153
|
// signed URLs (L3). See ADR-0075/0076.
|
|
1084
1154
|
|
|
1085
|
-
//
|
|
1155
|
+
// The asset the reserved `default` token resolves to (the transparent
|
|
1156
|
+
// /_blank.png this worker serves). Computed once; injected below.
|
|
1157
|
+
const house = houseAssetUrl(request);
|
|
1158
|
+
|
|
1159
|
+
// Every DEVELOPER-supplied image URL the design references (bare asset +
|
|
1160
|
+
// per-placement srcs). Reserved `default` tokens are EXCLUDED — they are
|
|
1161
|
+
// platform-injected (resolved to `house`), so they bypass L2 by provenance.
|
|
1086
1162
|
const imageUrls: string[] = [];
|
|
1087
|
-
if (asset) imageUrls.push(asset);
|
|
1163
|
+
if (asset && !isDefaultToken(asset)) imageUrls.push(asset);
|
|
1088
1164
|
for (const k of designKeys) {
|
|
1089
1165
|
const src = design[k].src;
|
|
1090
|
-
if (src) imageUrls.push(src);
|
|
1166
|
+
if (src && !isDefaultToken(src)) imageUrls.push(src);
|
|
1091
1167
|
}
|
|
1092
1168
|
|
|
1093
1169
|
// 2b. L2 — asset-source allowlist (checked for EVERY image URL's hostname).
|
|
@@ -1241,7 +1317,7 @@ async function handlePublicResolve(
|
|
|
1241
1317
|
} else if (fill.src) {
|
|
1242
1318
|
const el: DesignElement = {
|
|
1243
1319
|
type: "image",
|
|
1244
|
-
imageUrl: fill.src,
|
|
1320
|
+
imageUrl: isDefaultToken(fill.src) ? house : fill.src,
|
|
1245
1321
|
placement: pl.label,
|
|
1246
1322
|
width: pl.width,
|
|
1247
1323
|
height: pl.height,
|
|
@@ -1259,7 +1335,7 @@ async function handlePublicResolve(
|
|
|
1259
1335
|
} else if (asset) {
|
|
1260
1336
|
designElements.push({
|
|
1261
1337
|
type: "image",
|
|
1262
|
-
imageUrl: asset,
|
|
1338
|
+
imageUrl: isDefaultToken(asset) ? house : asset,
|
|
1263
1339
|
placement: scene.placement.label,
|
|
1264
1340
|
width: scene.placement.width,
|
|
1265
1341
|
height: scene.placement.height,
|
|
@@ -1388,6 +1464,22 @@ export default {
|
|
|
1388
1464
|
);
|
|
1389
1465
|
}
|
|
1390
1466
|
|
|
1467
|
+
// The transparent house placeholder the `asset=default` token resolves to
|
|
1468
|
+
// (see DEFAULT_ASSET_TOKEN). Served by this worker so the "blank item"
|
|
1469
|
+
// affordance deploys atomically with no external hosting to keep in sync.
|
|
1470
|
+
// Immutable → long cache.
|
|
1471
|
+
if (request.method === "GET" && url.pathname === BLANK_PNG_PATH) {
|
|
1472
|
+
return addCorsHeaders(
|
|
1473
|
+
new Response(base64ToBytes(BLANK_PNG_BASE64), {
|
|
1474
|
+
headers: {
|
|
1475
|
+
"Content-Type": "image/png",
|
|
1476
|
+
"Cache-Control": "public, max-age=31536000, immutable",
|
|
1477
|
+
},
|
|
1478
|
+
}),
|
|
1479
|
+
request
|
|
1480
|
+
);
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1391
1483
|
// ADR-0076: the public image-CDN resolver on img.snowcone.app.
|
|
1392
1484
|
// GET https://img.snowcone.app/{productCode}?asset=…&shop=…
|
|
1393
1485
|
// Routed by Host (img.*) in production; in local dev (single host) we also
|
|
@@ -1395,19 +1487,10 @@ export default {
|
|
|
1395
1487
|
// (or the legacy `key` alias), so the resolver is reachable without the
|
|
1396
1488
|
// custom domain binding.
|
|
1397
1489
|
const host = (request.headers.get("Host") || url.host || "").toLowerCase();
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
const looksLikeResolve =
|
|
1403
|
-
request.method === "GET" &&
|
|
1404
|
-
isSingleSegmentPath &&
|
|
1405
|
-
!RESERVED_PATHS.has(pathSegments[0]) &&
|
|
1406
|
-
(isImgHost ||
|
|
1407
|
-
(url.searchParams.has(PARAMS.asset) &&
|
|
1408
|
-
url.searchParams.has(PARAMS.shop)));
|
|
1409
|
-
if (looksLikeResolve) {
|
|
1410
|
-
const productCode = decodeURIComponent(pathSegments[0]);
|
|
1490
|
+
if (looksLikeResolveRequest(request.method, host, url)) {
|
|
1491
|
+
const productCode = decodeURIComponent(
|
|
1492
|
+
url.pathname.split("/").filter(Boolean)[0]
|
|
1493
|
+
);
|
|
1411
1494
|
return addCorsHeaders(
|
|
1412
1495
|
await handlePublicResolve(request, env, productCode),
|
|
1413
1496
|
request
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Strict query-param validation for the ADR-0076 public resolver
|
|
2
|
+
// (img.snowcone.app). The grammar predicate lives in @snowcone-app/mockup-url
|
|
3
|
+
// (the wire-grammar SSOT); this module just turns "unknown keys" into the 400
|
|
4
|
+
// Response. Kept free of `cloudflare:workers` imports so unit tests can load
|
|
5
|
+
// it in plain node vitest (same pattern as src/signing.ts).
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
unknownPublicParams,
|
|
9
|
+
unknownPublicParamsMessage,
|
|
10
|
+
} from "@snowcone-app/mockup-url";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* If the public resolve URL carries any query key outside the public grammar,
|
|
14
|
+
* return a 400 JSON Response naming the offending key(s) and listing the
|
|
15
|
+
* allowed grammar (with `view=` → `mockup=` style hints). Returns null when
|
|
16
|
+
* every key is known.
|
|
17
|
+
*
|
|
18
|
+
* Rationale: the resolver used to silently ignore unknown params — a
|
|
19
|
+
* hand-edited `view=<sceneId>` returned the DEFAULT scene with zero feedback
|
|
20
|
+
* (4 different `view=` values → 4 identical images). Pre-launch, fail loudly.
|
|
21
|
+
*/
|
|
22
|
+
export function rejectUnknownPublicParams(url: URL): Response | null {
|
|
23
|
+
const unknown = unknownPublicParams(url.searchParams);
|
|
24
|
+
if (unknown.length === 0) return null;
|
|
25
|
+
return new Response(
|
|
26
|
+
JSON.stringify({ error: unknownPublicParamsMessage(unknown) }),
|
|
27
|
+
{
|
|
28
|
+
status: 400,
|
|
29
|
+
headers: {
|
|
30
|
+
"Content-Type": "application/json",
|
|
31
|
+
// 4xx must never be cached as the image (ADR-0076 §7).
|
|
32
|
+
"Cache-Control": "no-store",
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
}
|
package/src/resolve.ts
CHANGED
|
@@ -4,6 +4,48 @@
|
|
|
4
4
|
// logic in `apps/www/app/api/harness/mockup/route.ts` (the prototype) and the
|
|
5
5
|
// SDK's `getProduct`. Edge-safe: plain fetch + JSON. (ADR-0073 §10.)
|
|
6
6
|
|
|
7
|
+
import { PARAMS } from "@snowcone-app/mockup-url";
|
|
8
|
+
|
|
9
|
+
/** Paths reserved for the signer/utility surface — never treated as a productCode. */
|
|
10
|
+
const RESERVED_RESOLVE_PATHS = new Set([
|
|
11
|
+
"test-kv",
|
|
12
|
+
"mockups",
|
|
13
|
+
"sign",
|
|
14
|
+
"verify",
|
|
15
|
+
"_blank.png", // the house placeholder the `asset=default` token resolves to
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Decide whether an incoming request is an ADR-0076 public resolve
|
|
20
|
+
* (`GET /{productCode}?asset[.key]=…&shop=…`) versus the signer surface.
|
|
21
|
+
*
|
|
22
|
+
* Routed by Host in prod (`img.snowcone.app`); the `img` label may also be
|
|
23
|
+
* interior behind the workspace-prefixed dev proxy (`lima-v2.img.lvh.me`).
|
|
24
|
+
* The single-host dev fallback accepts any artwork param — bare `asset=` or a
|
|
25
|
+
* multi-placement `asset.<key>=` — plus `shop=`, so a resolve is reachable
|
|
26
|
+
* without the custom domain binding.
|
|
27
|
+
*/
|
|
28
|
+
export function looksLikeResolveRequest(
|
|
29
|
+
method: string,
|
|
30
|
+
host: string,
|
|
31
|
+
url: URL
|
|
32
|
+
): boolean {
|
|
33
|
+
if (method !== "GET") return false;
|
|
34
|
+
|
|
35
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
36
|
+
if (segments.length !== 1) return false;
|
|
37
|
+
if (RESERVED_RESOLVE_PATHS.has(segments[0])) return false;
|
|
38
|
+
|
|
39
|
+
const h = host.toLowerCase();
|
|
40
|
+
const isImgHost = h.startsWith("img.") || h.includes(".img.");
|
|
41
|
+
if (isImgHost) return true;
|
|
42
|
+
|
|
43
|
+
const hasAssetParam = [...url.searchParams.keys()].some(
|
|
44
|
+
(k) => k === PARAMS.asset || k.startsWith(`${PARAMS.asset}.`)
|
|
45
|
+
);
|
|
46
|
+
return hasAssetParam && url.searchParams.has(PARAMS.shop);
|
|
47
|
+
}
|
|
48
|
+
|
|
7
49
|
export interface MeiliConfig {
|
|
8
50
|
host: string;
|
|
9
51
|
apiKey: string;
|
|
@@ -178,6 +178,28 @@ describe('Mockup Resolver Integration Tests', () => {
|
|
|
178
178
|
expect(valid).toBe(false)
|
|
179
179
|
})
|
|
180
180
|
|
|
181
|
+
it('should 400 a public resolve carrying an unknown query param', async () => {
|
|
182
|
+
// `view=` is the classic mistake (the MCP tool option name; the wire param
|
|
183
|
+
// is `mockup=`). It used to be silently ignored — same image, zero feedback.
|
|
184
|
+
const response = await fetch(
|
|
185
|
+
`${BASE_URL}/some-product?asset=${encodeURIComponent(TEST_URL)}&shop=abc123&view=scene-2`,
|
|
186
|
+
)
|
|
187
|
+
expect(response.status).toBe(400)
|
|
188
|
+
const body = (await response.json()) as { error: string }
|
|
189
|
+
expect(body.error).toContain('"view"')
|
|
190
|
+
expect(body.error).toContain('mockup=<sceneId>')
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('should not reject a public resolve whose params are all known grammar', async () => {
|
|
194
|
+
const response = await fetch(
|
|
195
|
+
`${BASE_URL}/some-product?asset.front=${encodeURIComponent(TEST_URL)}&color.back=%23000&opt.size=M&shop=abc123&width=600&aspect=2:3&mockup=scene-1&signature=deadbeefdeadbeef`,
|
|
196
|
+
)
|
|
197
|
+
// Local dev may lack Meilisearch/catalog config, so this can be 404/500 —
|
|
198
|
+
// but it must never be the unknown-param 400.
|
|
199
|
+
const body = (await response.clone().json().catch(() => ({}))) as { error?: string }
|
|
200
|
+
expect(body.error ?? '').not.toContain('Unknown query')
|
|
201
|
+
})
|
|
202
|
+
|
|
181
203
|
it('should handle CORS preflight requests', async () => {
|
|
182
204
|
const urlToSign = addAccountIdToSignedUrl(TEST_URL)
|
|
183
205
|
const response = await fetch(`${BASE_URL}/sign?url=${encodeURIComponent(urlToSign)}`, {
|
|
@@ -139,6 +139,21 @@ describe('Mockup Resolver Worker', () => {
|
|
|
139
139
|
expect(response.status).toBe(401)
|
|
140
140
|
})
|
|
141
141
|
|
|
142
|
+
it('serves the transparent house placeholder at /_blank.png', async () => {
|
|
143
|
+
// The asset=default token (blank-item affordance) resolves to this; the
|
|
144
|
+
// worker serves it itself so it deploys atomically with no external hosting.
|
|
145
|
+
const worker = await getWorker()
|
|
146
|
+
const request = new Request('https://img.snowcone.app/_blank.png')
|
|
147
|
+
|
|
148
|
+
const response = await worker.fetch(request, mockEnv, {})
|
|
149
|
+
expect(response.status).toBe(200)
|
|
150
|
+
expect(response.headers.get('Content-Type')).toBe('image/png')
|
|
151
|
+
expect(response.headers.get('Cache-Control')).toContain('immutable')
|
|
152
|
+
const bytes = new Uint8Array(await response.arrayBuffer())
|
|
153
|
+
// PNG magic number.
|
|
154
|
+
expect(Array.from(bytes.slice(0, 4))).toEqual([0x89, 0x50, 0x4e, 0x47])
|
|
155
|
+
})
|
|
156
|
+
|
|
142
157
|
it('should handle CORS preflight requests', async () => {
|
|
143
158
|
const worker = await getWorker()
|
|
144
159
|
const request = new Request(
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
// Helpers live in @snowcone-app/mockup-url (the wire-grammar SSOT); the worker
|
|
4
|
+
// seam is src/public-params.ts (pure — no `cloudflare:workers` import, same
|
|
5
|
+
// unit-test pattern as src/signing.ts).
|
|
6
|
+
import {
|
|
7
|
+
PUBLIC_PARAM_GRAMMAR,
|
|
8
|
+
PUBLIC_PARAM_NAMES,
|
|
9
|
+
isKnownPublicParam,
|
|
10
|
+
unknownPublicParams,
|
|
11
|
+
unknownPublicParamsMessage,
|
|
12
|
+
} from '@snowcone-app/mockup-url'
|
|
13
|
+
import { rejectUnknownPublicParams } from '../src/public-params'
|
|
14
|
+
|
|
15
|
+
describe('isKnownPublicParam', () => {
|
|
16
|
+
it('accepts every exact public param name', () => {
|
|
17
|
+
for (const name of ['asset', 'shop', 'placement', 'variant', 'mockup', 'width', 'aspect', 'signature']) {
|
|
18
|
+
expect(isKnownPublicParam(name), name).toBe(true)
|
|
19
|
+
}
|
|
20
|
+
// The exported list matches the grammar exactly (no extras, no omissions).
|
|
21
|
+
expect([...PUBLIC_PARAM_NAMES].sort()).toEqual(
|
|
22
|
+
['aspect', 'asset', 'mockup', 'placement', 'shop', 'signature', 'variant', 'width'],
|
|
23
|
+
)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('accepts the dotted families with a non-empty suffix', () => {
|
|
27
|
+
for (const name of ['asset.front', 'color.back', 'tile.sleeve', 'align.front', 'opt.Size']) {
|
|
28
|
+
expect(isKnownPublicParam(name), name).toBe(true)
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('rejects a bare dotted prefix (empty placement key)', () => {
|
|
33
|
+
for (const name of ['asset.', 'color.', 'tile.', 'align.', 'opt.']) {
|
|
34
|
+
expect(isKnownPublicParam(name), name).toBe(false)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('rejects unknown params, including near-misses', () => {
|
|
39
|
+
for (const name of ['view', 'key', 'accountId', 'w', 's', 'utm_source', 'assets', 'seal', 'Shop', 'expires']) {
|
|
40
|
+
expect(isKnownPublicParam(name), name).toBe(false)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('unknownPublicParams', () => {
|
|
46
|
+
it('returns [] for the full known grammar in one URL (multi-placement, CRITICAL)', () => {
|
|
47
|
+
const url = new URL(
|
|
48
|
+
'https://img.snowcone.app/hoodie-black' +
|
|
49
|
+
'?asset.front=https%3A%2F%2Fx.storage.snowcone.app%2Fa.png' +
|
|
50
|
+
'&color.back=%23ff0000&tile.front=2&align.front=top&opt.Size=M' +
|
|
51
|
+
'&shop=2e2c853b83&width=600&aspect=2:3&mockup=scene-1&variant=gv1' +
|
|
52
|
+
'&placement=Front&signature=abcdef0123456789',
|
|
53
|
+
)
|
|
54
|
+
expect(unknownPublicParams(url)).toEqual([])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('returns [] for the bare-asset shorthand', () => {
|
|
58
|
+
const url = new URL('https://img.snowcone.app/tee?asset=default&shop=abc123')
|
|
59
|
+
expect(unknownPublicParams(url.searchParams)).toEqual([])
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('names unknown keys, deduped, in first-seen order', () => {
|
|
63
|
+
const url = new URL(
|
|
64
|
+
'https://img.snowcone.app/tee?asset=x&view=a&shop=abc123&view=b&foo=1',
|
|
65
|
+
)
|
|
66
|
+
expect(unknownPublicParams(url)).toEqual(['view', 'foo'])
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('accepts a string URL input', () => {
|
|
70
|
+
expect(
|
|
71
|
+
unknownPublicParams('https://img.snowcone.app/tee?asset=x&shop=abc123&bogus=1'),
|
|
72
|
+
).toEqual(['bogus'])
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('unknownPublicParamsMessage', () => {
|
|
77
|
+
it('names the key and lists the allowed grammar', () => {
|
|
78
|
+
const msg = unknownPublicParamsMessage(['frobnicate'])
|
|
79
|
+
expect(msg).toContain('Unknown query parameter "frobnicate"')
|
|
80
|
+
expect(msg).toContain(`Allowed: ${PUBLIC_PARAM_GRAMMAR}`)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('tells `view=` users to use mockup=<sceneId>', () => {
|
|
84
|
+
const msg = unknownPublicParamsMessage(['view'])
|
|
85
|
+
expect(msg).toContain('"view"')
|
|
86
|
+
expect(msg).toContain('mockup=<sceneId>')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('tells `key=` users to use shop=', () => {
|
|
90
|
+
expect(unknownPublicParamsMessage(['key'])).toContain('shop=<shopId>')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('pluralizes and lists every offending key', () => {
|
|
94
|
+
const msg = unknownPublicParamsMessage(['view', 'foo'])
|
|
95
|
+
expect(msg).toContain('Unknown query parameters "view", "foo"')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('rejectUnknownPublicParams (the resolver 400 seam)', () => {
|
|
100
|
+
it('returns null for a clean multi-placement URL (must NOT 400)', () => {
|
|
101
|
+
const url = new URL(
|
|
102
|
+
'https://img.snowcone.app/hoodie-black' +
|
|
103
|
+
'?asset.front=https%3A%2F%2Fx%2Fa.png&color.back=%23000&opt.size=M' +
|
|
104
|
+
'&shop=abc123&width=600&aspect=2:3&mockup=scene-1&signature=deadbeefdeadbeef',
|
|
105
|
+
)
|
|
106
|
+
expect(rejectUnknownPublicParams(url)).toBeNull()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('400s an unknown param with a JSON error naming the key', async () => {
|
|
110
|
+
const url = new URL('https://img.snowcone.app/tee?asset=x&shop=abc123&view=scene-2')
|
|
111
|
+
const resp = rejectUnknownPublicParams(url)
|
|
112
|
+
expect(resp).not.toBeNull()
|
|
113
|
+
expect(resp!.status).toBe(400)
|
|
114
|
+
expect(resp!.headers.get('Content-Type')).toBe('application/json')
|
|
115
|
+
// 4xx must never be cached as the image (ADR-0076 §7).
|
|
116
|
+
expect(resp!.headers.get('Cache-Control')).toBe('no-store')
|
|
117
|
+
const body = (await resp!.json()) as { error: string }
|
|
118
|
+
expect(body.error).toContain('"view"')
|
|
119
|
+
expect(body.error).toContain('mockup=<sceneId>')
|
|
120
|
+
expect(body.error).toContain('Allowed:')
|
|
121
|
+
})
|
|
122
|
+
})
|
package/test/resolve.test.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
looksLikeResolveRequest,
|
|
4
|
+
resolveVariant,
|
|
5
|
+
slugify,
|
|
6
|
+
type ResolvedProduct,
|
|
7
|
+
} from "../src/resolve";
|
|
3
8
|
|
|
4
9
|
// A size×color-style product WITH combinations (picks select the SKU).
|
|
5
10
|
const tee: ResolvedProduct = {
|
|
@@ -67,6 +72,84 @@ describe("resolveVariant", () => {
|
|
|
67
72
|
});
|
|
68
73
|
});
|
|
69
74
|
|
|
75
|
+
describe("looksLikeResolveRequest", () => {
|
|
76
|
+
const u = (path: string, host = "img.snowcone.app") =>
|
|
77
|
+
new URL(`https://${host}${path}`);
|
|
78
|
+
|
|
79
|
+
it("resolves on the prod img host (leading label)", () => {
|
|
80
|
+
expect(
|
|
81
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/JFBH0Q"))
|
|
82
|
+
).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("resolves on an interior img label (workspace-prefixed dev host)", () => {
|
|
86
|
+
expect(
|
|
87
|
+
looksLikeResolveRequest(
|
|
88
|
+
"GET",
|
|
89
|
+
"lima-v2.img.lvh.me",
|
|
90
|
+
u("/JFBH0Q", "lima-v2.img.lvh.me")
|
|
91
|
+
)
|
|
92
|
+
).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("resolves a multi-placement asset.<key> via the single-host fallback", () => {
|
|
96
|
+
// Non-img host: must fall back on asset.<key>= + shop= (the bug — the old
|
|
97
|
+
// gate only checked bare `asset`, so `asset.front` hit the signer index).
|
|
98
|
+
expect(
|
|
99
|
+
looksLikeResolveRequest(
|
|
100
|
+
"GET",
|
|
101
|
+
"lima-v2.s.lvh.me",
|
|
102
|
+
u("/JFBH0Q?shop=abc&asset.front=https%3A%2F%2Fx%2Fa.png", "lima-v2.s.lvh.me")
|
|
103
|
+
)
|
|
104
|
+
).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("resolves bare asset= via the single-host fallback", () => {
|
|
108
|
+
expect(
|
|
109
|
+
looksLikeResolveRequest(
|
|
110
|
+
"GET",
|
|
111
|
+
"host.lvh.me",
|
|
112
|
+
u("/JFBH0Q?shop=abc&asset=https%3A%2F%2Fx%2Fa.png", "host.lvh.me")
|
|
113
|
+
)
|
|
114
|
+
).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("does NOT resolve the signer/verify/reserved paths", () => {
|
|
118
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/sign"))).toBe(
|
|
119
|
+
false
|
|
120
|
+
);
|
|
121
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/verify"))).toBe(
|
|
122
|
+
false
|
|
123
|
+
);
|
|
124
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/mockups"))).toBe(
|
|
125
|
+
false
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("does NOT treat the /_blank.png house placeholder as a productCode", () => {
|
|
130
|
+
// The asset=default token resolves to this worker-served PNG; it must be
|
|
131
|
+
// served, never routed through the resolver as a product.
|
|
132
|
+
expect(
|
|
133
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/_blank.png"))
|
|
134
|
+
).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("does NOT resolve on a non-img host without artwork params", () => {
|
|
138
|
+
expect(
|
|
139
|
+
looksLikeResolveRequest("GET", "s.lvh.me", u("/JFBH0Q", "s.lvh.me"))
|
|
140
|
+
).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("does NOT resolve non-GET or multi-segment paths", () => {
|
|
144
|
+
expect(
|
|
145
|
+
looksLikeResolveRequest("POST", "img.snowcone.app", u("/JFBH0Q"))
|
|
146
|
+
).toBe(false);
|
|
147
|
+
expect(
|
|
148
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/a/b"))
|
|
149
|
+
).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
70
153
|
describe("slugify", () => {
|
|
71
154
|
it("derives URL-safe placement keys from labels", () => {
|
|
72
155
|
expect(slugify("Front")).toBe("front");
|