@snowcone-app/mockup-resolver 1.0.0 → 1.0.2
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 +15 -0
- package/README.md +21 -0
- package/package.json +2 -2
- package/src/asset-allowlist.ts +70 -0
- package/src/design-ref.ts +104 -0
- package/src/index.ts +255 -65
- package/src/public-params.ts +36 -0
- package/src/resolve.ts +121 -1
- package/test/asset-allowlist.test.ts +87 -0
- package/test/design-ref.test.ts +139 -0
- package/test/integration/mockup-resolver.test.ts +22 -0
- package/test/integration/worker.test.ts +15 -0
- package/test/public-params.test.ts +132 -0
- package/test/resolve.test.ts +194 -1
- package/wrangler.jsonc +7 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { checkAssetUrls, isHostAllowed } from "../src/asset-allowlist";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* L2 asset-origin allowlist. No origin is ever exempt (ADR-0086): catalog
|
|
6
|
+
* designs render by reference (`design=`) with resolver-injected components,
|
|
7
|
+
* so the old EXEMPT_ASSET_ORIGINS bypass is gone — every developer-supplied
|
|
8
|
+
* URL is checked against the shop's own allowlist, per-URL.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const ALLOWLIST = ["allowed.example.com", "*.cdn.example.com"];
|
|
12
|
+
|
|
13
|
+
describe("checkAssetUrls (L2)", () => {
|
|
14
|
+
it("passes everything when the allowlist is unset or empty", () => {
|
|
15
|
+
expect(
|
|
16
|
+
checkAssetUrls(["https://evil.example.com/a.png"], undefined).ok
|
|
17
|
+
).toBe(true);
|
|
18
|
+
expect(checkAssetUrls(["https://evil.example.com/a.png"], []).ok).toBe(
|
|
19
|
+
true
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("403s an off-list origin", () => {
|
|
24
|
+
const verdict = checkAssetUrls(
|
|
25
|
+
["https://evil.example.com/a.png"],
|
|
26
|
+
ALLOWLIST
|
|
27
|
+
);
|
|
28
|
+
expect(verdict).toEqual({
|
|
29
|
+
ok: false,
|
|
30
|
+
status: 403,
|
|
31
|
+
message: "Asset source not allowed for this shop",
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("passes an allowlisted origin (including glob patterns)", () => {
|
|
36
|
+
expect(
|
|
37
|
+
checkAssetUrls(["https://allowed.example.com/a.png"], ALLOWLIST).ok
|
|
38
|
+
).toBe(true);
|
|
39
|
+
expect(
|
|
40
|
+
checkAssetUrls(["https://img.cdn.example.com/a.png"], ALLOWLIST).ok
|
|
41
|
+
).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("no origin is exempt — other shops' storage origins are off-list (ADR-0086)", () => {
|
|
45
|
+
const verdict = checkAssetUrls(
|
|
46
|
+
["https://zzz999.storage.snowcone.app/exports/full.png"],
|
|
47
|
+
ALLOWLIST
|
|
48
|
+
);
|
|
49
|
+
expect(verdict.ok).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("multi-placement mixed allowed + off-list still fails per-URL", () => {
|
|
53
|
+
const verdict = checkAssetUrls(
|
|
54
|
+
[
|
|
55
|
+
"https://allowed.example.com/front.png",
|
|
56
|
+
"https://evil.example.com/back.png",
|
|
57
|
+
],
|
|
58
|
+
ALLOWLIST
|
|
59
|
+
);
|
|
60
|
+
expect(verdict).toEqual({
|
|
61
|
+
ok: false,
|
|
62
|
+
status: 403,
|
|
63
|
+
message: "Asset source not allowed for this shop",
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("400s an unparsable asset URL", () => {
|
|
68
|
+
const verdict = checkAssetUrls(["not a url"], ALLOWLIST);
|
|
69
|
+
expect(verdict).toEqual({
|
|
70
|
+
ok: false,
|
|
71
|
+
status: 400,
|
|
72
|
+
message: "Invalid asset URL",
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("isHostAllowed", () => {
|
|
78
|
+
it("treats an empty allowlist as allow-any", () => {
|
|
79
|
+
expect(isHostAllowed("anything.example.com", [])).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("matches case-insensitively and via globs", () => {
|
|
83
|
+
expect(isHostAllowed("Allowed.Example.COM", ALLOWLIST)).toBe(true);
|
|
84
|
+
expect(isHostAllowed("a.b.cdn.example.com", ALLOWLIST)).toBe(true);
|
|
85
|
+
expect(isHostAllowed("evil.example.com", ALLOWLIST)).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { fetchDesign } from "../src/design-ref";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `design=<pairingId|slug>` catalog lookup (ADR-0086): document-id fetch
|
|
6
|
+
* first, slug-filter search fallback, short-TTL KV cache, and a null (→ loud
|
|
7
|
+
* 404 at the caller) for refs that are not in the catalog.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const MEILI = {
|
|
11
|
+
host: "https://search.example",
|
|
12
|
+
apiKey: "public-key",
|
|
13
|
+
index: "snowcone-designs-dev",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const DOC = {
|
|
17
|
+
pairingId: "pairing-1",
|
|
18
|
+
slug: "watercolor-christmas-card",
|
|
19
|
+
productId: "KMYKUK",
|
|
20
|
+
asset: "https://abc.storage.snowcone.app/exports/full.png",
|
|
21
|
+
assets: { front: "https://abc.storage.snowcone.app/exports/front.png" },
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function kvStub(seed: Record<string, string> = {}) {
|
|
25
|
+
const store = new Map(Object.entries(seed));
|
|
26
|
+
return {
|
|
27
|
+
get: vi.fn(async (k: string, _type?: string) => {
|
|
28
|
+
const v = store.get(k);
|
|
29
|
+
return v ? JSON.parse(v) : null;
|
|
30
|
+
}),
|
|
31
|
+
put: vi.fn(async (k: string, v: string) => {
|
|
32
|
+
store.set(k, v);
|
|
33
|
+
}),
|
|
34
|
+
store,
|
|
35
|
+
} as any;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
39
|
+
|
|
40
|
+
describe("fetchDesign", () => {
|
|
41
|
+
it("resolves by document id (pairingId) and caches the slice", async () => {
|
|
42
|
+
const fetchMock = vi.fn(async (url: string) => {
|
|
43
|
+
expect(url).toBe(
|
|
44
|
+
`${MEILI.host}/indexes/${MEILI.index}/documents/pairing-1`
|
|
45
|
+
);
|
|
46
|
+
return new Response(JSON.stringify(DOC), { status: 200 });
|
|
47
|
+
});
|
|
48
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
49
|
+
const kv = kvStub();
|
|
50
|
+
|
|
51
|
+
const design = await fetchDesign("pairing-1", MEILI, kv);
|
|
52
|
+
expect(design).toEqual({
|
|
53
|
+
pairingId: "pairing-1",
|
|
54
|
+
slug: "watercolor-christmas-card",
|
|
55
|
+
productId: "KMYKUK",
|
|
56
|
+
asset: DOC.asset,
|
|
57
|
+
assets: DOC.assets,
|
|
58
|
+
});
|
|
59
|
+
expect(kv.put).toHaveBeenCalledOnce();
|
|
60
|
+
|
|
61
|
+
// Second call is served from KV — no further fetches.
|
|
62
|
+
await fetchDesign("pairing-1", MEILI, kv);
|
|
63
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("falls back to a slug-filter search when the id lookup 404s", async () => {
|
|
67
|
+
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
68
|
+
if (String(url).includes("/documents/")) {
|
|
69
|
+
return new Response("{}", { status: 404 });
|
|
70
|
+
}
|
|
71
|
+
expect(String(url)).toBe(`${MEILI.host}/indexes/${MEILI.index}/search`);
|
|
72
|
+
const body = JSON.parse(String(init?.body));
|
|
73
|
+
expect(body.filter).toBe('slug = "watercolor-christmas-card"');
|
|
74
|
+
return new Response(JSON.stringify({ hits: [DOC] }), { status: 200 });
|
|
75
|
+
});
|
|
76
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
77
|
+
|
|
78
|
+
const design = await fetchDesign(
|
|
79
|
+
"watercolor-christmas-card",
|
|
80
|
+
MEILI,
|
|
81
|
+
kvStub()
|
|
82
|
+
);
|
|
83
|
+
expect(design?.pairingId).toBe("pairing-1");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("strips quote/backslash from the ref before building the slug filter", async () => {
|
|
87
|
+
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
88
|
+
if (String(url).includes("/documents/")) {
|
|
89
|
+
return new Response("{}", { status: 404 });
|
|
90
|
+
}
|
|
91
|
+
const body = JSON.parse(String(init?.body));
|
|
92
|
+
expect(body.filter).toBe('slug = "x OR pairingId != "');
|
|
93
|
+
return new Response(JSON.stringify({ hits: [] }), { status: 200 });
|
|
94
|
+
});
|
|
95
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
96
|
+
|
|
97
|
+
const design = await fetchDesign(
|
|
98
|
+
'x" OR pairingId != "',
|
|
99
|
+
MEILI,
|
|
100
|
+
kvStub()
|
|
101
|
+
);
|
|
102
|
+
expect(design).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("returns null (caller 404s loudly) for a ref not in the catalog", async () => {
|
|
106
|
+
vi.stubGlobal(
|
|
107
|
+
"fetch",
|
|
108
|
+
vi.fn(async (url: string) =>
|
|
109
|
+
String(url).includes("/documents/")
|
|
110
|
+
? new Response("{}", { status: 404 })
|
|
111
|
+
: new Response(JSON.stringify({ hits: [] }), { status: 200 })
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
const kv = kvStub();
|
|
115
|
+
expect(await fetchDesign("nope", MEILI, kv)).toBeNull();
|
|
116
|
+
// Misses are NOT cached — a just-synced design becomes renderable
|
|
117
|
+
// immediately.
|
|
118
|
+
expect(kv.put).not.toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("returns null for a doc with nothing renderable (no asset, no assets)", async () => {
|
|
122
|
+
vi.stubGlobal(
|
|
123
|
+
"fetch",
|
|
124
|
+
vi.fn(
|
|
125
|
+
async () =>
|
|
126
|
+
new Response(
|
|
127
|
+
JSON.stringify({
|
|
128
|
+
pairingId: "p2",
|
|
129
|
+
productId: "KMYKUK",
|
|
130
|
+
asset: null,
|
|
131
|
+
assets: {},
|
|
132
|
+
}),
|
|
133
|
+
{ status: 200 }
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
);
|
|
137
|
+
expect(await fetchDesign("p2", MEILI, kvStub())).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -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,132 @@
|
|
|
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', 'design', 'shop', 'placement', 'page', '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', 'design', 'mockup', 'page', '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('returns [] for a catalog design reference (ADR-0086)', () => {
|
|
63
|
+
const url = new URL('https://img.snowcone.app/tee?design=watercolor-christmas-card&shop=abc123')
|
|
64
|
+
expect(unknownPublicParams(url.searchParams)).toEqual([])
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('returns [] for an album page selector (ADR-0086 §5)', () => {
|
|
68
|
+
const url = new URL('https://img.snowcone.app/album?asset=https%3A%2F%2Fx%2Fa.png&shop=abc123&page=3')
|
|
69
|
+
expect(unknownPublicParams(url.searchParams)).toEqual([])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('names unknown keys, deduped, in first-seen order', () => {
|
|
73
|
+
const url = new URL(
|
|
74
|
+
'https://img.snowcone.app/tee?asset=x&view=a&shop=abc123&view=b&foo=1',
|
|
75
|
+
)
|
|
76
|
+
expect(unknownPublicParams(url)).toEqual(['view', 'foo'])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('accepts a string URL input', () => {
|
|
80
|
+
expect(
|
|
81
|
+
unknownPublicParams('https://img.snowcone.app/tee?asset=x&shop=abc123&bogus=1'),
|
|
82
|
+
).toEqual(['bogus'])
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('unknownPublicParamsMessage', () => {
|
|
87
|
+
it('names the key and lists the allowed grammar', () => {
|
|
88
|
+
const msg = unknownPublicParamsMessage(['frobnicate'])
|
|
89
|
+
expect(msg).toContain('Unknown query parameter "frobnicate"')
|
|
90
|
+
expect(msg).toContain(`Allowed: ${PUBLIC_PARAM_GRAMMAR}`)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('tells `view=` users to use mockup=<sceneId>', () => {
|
|
94
|
+
const msg = unknownPublicParamsMessage(['view'])
|
|
95
|
+
expect(msg).toContain('"view"')
|
|
96
|
+
expect(msg).toContain('mockup=<sceneId>')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('tells `key=` users to use shop=', () => {
|
|
100
|
+
expect(unknownPublicParamsMessage(['key'])).toContain('shop=<shopId>')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('pluralizes and lists every offending key', () => {
|
|
104
|
+
const msg = unknownPublicParamsMessage(['view', 'foo'])
|
|
105
|
+
expect(msg).toContain('Unknown query parameters "view", "foo"')
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe('rejectUnknownPublicParams (the resolver 400 seam)', () => {
|
|
110
|
+
it('returns null for a clean multi-placement URL (must NOT 400)', () => {
|
|
111
|
+
const url = new URL(
|
|
112
|
+
'https://img.snowcone.app/hoodie-black' +
|
|
113
|
+
'?asset.front=https%3A%2F%2Fx%2Fa.png&color.back=%23000&opt.size=M' +
|
|
114
|
+
'&shop=abc123&width=600&aspect=2:3&mockup=scene-1&signature=deadbeefdeadbeef',
|
|
115
|
+
)
|
|
116
|
+
expect(rejectUnknownPublicParams(url)).toBeNull()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('400s an unknown param with a JSON error naming the key', async () => {
|
|
120
|
+
const url = new URL('https://img.snowcone.app/tee?asset=x&shop=abc123&view=scene-2')
|
|
121
|
+
const resp = rejectUnknownPublicParams(url)
|
|
122
|
+
expect(resp).not.toBeNull()
|
|
123
|
+
expect(resp!.status).toBe(400)
|
|
124
|
+
expect(resp!.headers.get('Content-Type')).toBe('application/json')
|
|
125
|
+
// 4xx must never be cached as the image (ADR-0076 §7).
|
|
126
|
+
expect(resp!.headers.get('Cache-Control')).toBe('no-store')
|
|
127
|
+
const body = (await resp!.json()) as { error: string }
|
|
128
|
+
expect(body.error).toContain('"view"')
|
|
129
|
+
expect(body.error).toContain('mockup=<sceneId>')
|
|
130
|
+
expect(body.error).toContain('Allowed:')
|
|
131
|
+
})
|
|
132
|
+
})
|
package/test/resolve.test.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
looksLikeResolveRequest,
|
|
4
|
+
normalizeAlbum,
|
|
5
|
+
resolveAlbumPagePlacement,
|
|
6
|
+
resolveVariant,
|
|
7
|
+
slugify,
|
|
8
|
+
type ResolvedProduct,
|
|
9
|
+
} from "../src/resolve";
|
|
3
10
|
|
|
4
11
|
// A size×color-style product WITH combinations (picks select the SKU).
|
|
5
12
|
const tee: ResolvedProduct = {
|
|
@@ -13,6 +20,7 @@ const tee: ResolvedProduct = {
|
|
|
13
20
|
{ variantId: "qS", options: { Size: "S" } },
|
|
14
21
|
{ variantId: "qM", options: { Size: "M" } },
|
|
15
22
|
],
|
|
23
|
+
album: null,
|
|
16
24
|
};
|
|
17
25
|
|
|
18
26
|
// The cap: NO combinations; render-only attributes (resolve via defaultGvid).
|
|
@@ -27,6 +35,7 @@ const cap: ResolvedProduct = {
|
|
|
27
35
|
Pocket: { affectsCombinations: false, choices: ["None", "Left chest"] },
|
|
28
36
|
},
|
|
29
37
|
combinations: [],
|
|
38
|
+
album: null,
|
|
30
39
|
};
|
|
31
40
|
|
|
32
41
|
describe("resolveVariant", () => {
|
|
@@ -62,11 +71,195 @@ describe("resolveVariant", () => {
|
|
|
62
71
|
defaultGvid: null,
|
|
63
72
|
attributes: {},
|
|
64
73
|
combinations: [],
|
|
74
|
+
album: null,
|
|
65
75
|
};
|
|
66
76
|
expect(resolveVariant(empty, {})).toBeNull();
|
|
67
77
|
});
|
|
68
78
|
});
|
|
69
79
|
|
|
80
|
+
describe("looksLikeResolveRequest", () => {
|
|
81
|
+
const u = (path: string, host = "img.snowcone.app") =>
|
|
82
|
+
new URL(`https://${host}${path}`);
|
|
83
|
+
|
|
84
|
+
it("resolves on the prod img host (leading label)", () => {
|
|
85
|
+
expect(
|
|
86
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/JFBH0Q"))
|
|
87
|
+
).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("resolves on an interior img label (workspace-prefixed dev host)", () => {
|
|
91
|
+
expect(
|
|
92
|
+
looksLikeResolveRequest(
|
|
93
|
+
"GET",
|
|
94
|
+
"lima-v2.img.lvh.me",
|
|
95
|
+
u("/JFBH0Q", "lima-v2.img.lvh.me")
|
|
96
|
+
)
|
|
97
|
+
).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("resolves a multi-placement asset.<key> via the single-host fallback", () => {
|
|
101
|
+
// Non-img host: must fall back on asset.<key>= + shop= (the bug — the old
|
|
102
|
+
// gate only checked bare `asset`, so `asset.front` hit the signer index).
|
|
103
|
+
expect(
|
|
104
|
+
looksLikeResolveRequest(
|
|
105
|
+
"GET",
|
|
106
|
+
"lima-v2.s.lvh.me",
|
|
107
|
+
u("/JFBH0Q?shop=abc&asset.front=https%3A%2F%2Fx%2Fa.png", "lima-v2.s.lvh.me")
|
|
108
|
+
)
|
|
109
|
+
).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("resolves bare asset= via the single-host fallback", () => {
|
|
113
|
+
expect(
|
|
114
|
+
looksLikeResolveRequest(
|
|
115
|
+
"GET",
|
|
116
|
+
"host.lvh.me",
|
|
117
|
+
u("/JFBH0Q?shop=abc&asset=https%3A%2F%2Fx%2Fa.png", "host.lvh.me")
|
|
118
|
+
)
|
|
119
|
+
).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("does NOT resolve the signer/verify/reserved paths", () => {
|
|
123
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/sign"))).toBe(
|
|
124
|
+
false
|
|
125
|
+
);
|
|
126
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/verify"))).toBe(
|
|
127
|
+
false
|
|
128
|
+
);
|
|
129
|
+
expect(looksLikeResolveRequest("GET", "img.snowcone.app", u("/mockups"))).toBe(
|
|
130
|
+
false
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("does NOT treat the /_blank.png house placeholder as a productCode", () => {
|
|
135
|
+
// The asset=default token resolves to this worker-served PNG; it must be
|
|
136
|
+
// served, never routed through the resolver as a product.
|
|
137
|
+
expect(
|
|
138
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/_blank.png"))
|
|
139
|
+
).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("does NOT resolve on a non-img host without artwork params", () => {
|
|
143
|
+
expect(
|
|
144
|
+
looksLikeResolveRequest("GET", "s.lvh.me", u("/JFBH0Q", "s.lvh.me"))
|
|
145
|
+
).toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("does NOT resolve non-GET or multi-segment paths", () => {
|
|
149
|
+
expect(
|
|
150
|
+
looksLikeResolveRequest("POST", "img.snowcone.app", u("/JFBH0Q"))
|
|
151
|
+
).toBe(false);
|
|
152
|
+
expect(
|
|
153
|
+
looksLikeResolveRequest("GET", "img.snowcone.app", u("/a/b"))
|
|
154
|
+
).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// An album (ADR-0086): covers enumerated, ONE page placement that repeats.
|
|
159
|
+
const album: ResolvedProduct = {
|
|
160
|
+
id: "ALBUM1",
|
|
161
|
+
variants: [{ gvid: "gA" }],
|
|
162
|
+
mockups: [],
|
|
163
|
+
placements: [
|
|
164
|
+
{ key: "front_cover", label: "Front cover", type: "image", width: 3000, height: 3000 },
|
|
165
|
+
{ key: "page", label: "Page", type: "image", width: 2400, height: 2400 },
|
|
166
|
+
{ key: "spine", label: "Spine", type: "image", width: 200, height: 3000 },
|
|
167
|
+
],
|
|
168
|
+
defaultGvid: "gA",
|
|
169
|
+
attributes: { Pages: { affectsCombinations: true, choices: ["20", "24"] } },
|
|
170
|
+
combinations: [],
|
|
171
|
+
album: {
|
|
172
|
+
pagePlacement: "page",
|
|
173
|
+
pageAxis: "Pages",
|
|
174
|
+
pageExtent: { min: 20, max: 100, step: 4 },
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
describe("resolveAlbumPagePlacement", () => {
|
|
179
|
+
it("expands page_N to the repeating placement's geometry (catalog never enumerates pages)", () => {
|
|
180
|
+
const pl = resolveAlbumPagePlacement(album, "page_3");
|
|
181
|
+
expect(pl).toEqual({
|
|
182
|
+
key: "page_3", // the public wire address is kept…
|
|
183
|
+
label: "Page", // …but the label is the base placement's (render wire unchanged)
|
|
184
|
+
type: "image",
|
|
185
|
+
width: 2400,
|
|
186
|
+
height: 2400,
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("accepts the full extent (1..pageExtent.max)", () => {
|
|
191
|
+
expect(resolveAlbumPagePlacement(album, "page_1")?.key).toBe("page_1");
|
|
192
|
+
expect(resolveAlbumPagePlacement(album, "page_100")?.key).toBe("page_100");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("rejects out-of-range and non-canonical page numbers", () => {
|
|
196
|
+
expect(resolveAlbumPagePlacement(album, "page_0")).toBeNull();
|
|
197
|
+
expect(resolveAlbumPagePlacement(album, "page_101")).toBeNull();
|
|
198
|
+
expect(resolveAlbumPagePlacement(album, "page_07")).toBeNull(); // would alias page_7
|
|
199
|
+
expect(resolveAlbumPagePlacement(album, "page_x")).toBeNull();
|
|
200
|
+
expect(resolveAlbumPagePlacement(album, "page_")).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("only expands the album's pagePlacement, never other placements", () => {
|
|
204
|
+
expect(resolveAlbumPagePlacement(album, "front_cover_2")).toBeNull();
|
|
205
|
+
expect(resolveAlbumPagePlacement(album, "spine_1")).toBeNull();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("returns null for non-album products (byte-identical behavior today)", () => {
|
|
209
|
+
expect(resolveAlbumPagePlacement(tee, "page_3")).toBeNull();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("returns null when the album names a placement the doc no longer has", () => {
|
|
213
|
+
const drifted: ResolvedProduct = {
|
|
214
|
+
...album,
|
|
215
|
+
placements: album.placements.filter((p) => p.key !== "page"),
|
|
216
|
+
};
|
|
217
|
+
expect(resolveAlbumPagePlacement(drifted, "page_3")).toBeNull();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("normalizeAlbum", () => {
|
|
222
|
+
it("passes a well-formed block through", () => {
|
|
223
|
+
expect(
|
|
224
|
+
normalizeAlbum({
|
|
225
|
+
pagePlacement: "page",
|
|
226
|
+
pageAxis: "Pages",
|
|
227
|
+
pageExtent: { min: 20, max: 100, step: 4 },
|
|
228
|
+
})
|
|
229
|
+
).toEqual({
|
|
230
|
+
pagePlacement: "page",
|
|
231
|
+
pageAxis: "Pages",
|
|
232
|
+
pageExtent: { min: 20, max: 100, step: 4 },
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("rejects missing or malformed blocks (the product behaves as non-album)", () => {
|
|
237
|
+
expect(normalizeAlbum(undefined)).toBeNull();
|
|
238
|
+
expect(normalizeAlbum({ pagePlacement: "page" })).toBeNull();
|
|
239
|
+
expect(
|
|
240
|
+
normalizeAlbum({
|
|
241
|
+
pagePlacement: "page",
|
|
242
|
+
pageAxis: "Pages",
|
|
243
|
+
pageExtent: { min: 100, max: 20, step: 4 }, // max < min
|
|
244
|
+
})
|
|
245
|
+
).toBeNull();
|
|
246
|
+
expect(
|
|
247
|
+
normalizeAlbum({
|
|
248
|
+
pagePlacement: "",
|
|
249
|
+
pageAxis: "Pages",
|
|
250
|
+
pageExtent: { min: 20, max: 100, step: 4 },
|
|
251
|
+
})
|
|
252
|
+
).toBeNull();
|
|
253
|
+
expect(
|
|
254
|
+
normalizeAlbum({
|
|
255
|
+
pagePlacement: "page",
|
|
256
|
+
pageAxis: "Pages",
|
|
257
|
+
pageExtent: { min: 20, max: 100, step: 0 },
|
|
258
|
+
})
|
|
259
|
+
).toBeNull();
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
70
263
|
describe("slugify", () => {
|
|
71
264
|
it("derives URL-safe placement keys from labels", () => {
|
|
72
265
|
expect(slugify("Front")).toBe("front");
|
package/wrangler.jsonc
CHANGED
|
@@ -38,7 +38,10 @@
|
|
|
38
38
|
"MEILISEARCH_API_KEY": "eee819b849798ad9091228c486ec05d0931e5292",
|
|
39
39
|
"MEILISEARCH_INDEX": "snowcone",
|
|
40
40
|
"MOCKUP_BASE_URL": "https://cdn.snowcone.app",
|
|
41
|
-
"KV_CACHE_BASE_URL": "https://api.snowcone.app"
|
|
41
|
+
"KV_CACHE_BASE_URL": "https://api.snowcone.app",
|
|
42
|
+
// Designs catalog (ADR-0086): the index `design=<pairingId|slug>` resolves
|
|
43
|
+
// against, read with the same public search key.
|
|
44
|
+
"MEILISEARCH_DESIGNS_INDEX": "snowcone-designs"
|
|
42
45
|
},
|
|
43
46
|
"kv_namespaces": [
|
|
44
47
|
{
|
|
@@ -63,7 +66,9 @@
|
|
|
63
66
|
"MEILISEARCH_API_KEY": "eee819b849798ad9091228c486ec05d0931e5292",
|
|
64
67
|
"MEILISEARCH_INDEX": "snowcone-dev",
|
|
65
68
|
"MOCKUP_BASE_URL": "https://cdn.snowcone.app",
|
|
66
|
-
"KV_CACHE_BASE_URL": "http://localhost:4000"
|
|
69
|
+
"KV_CACHE_BASE_URL": "http://localhost:4000",
|
|
70
|
+
// See top-level MEILISEARCH_DESIGNS_INDEX comment.
|
|
71
|
+
"MEILISEARCH_DESIGNS_INDEX": "snowcone-designs-dev"
|
|
67
72
|
},
|
|
68
73
|
"kv_namespaces": [
|
|
69
74
|
{
|