Skip to content

Commit 8bcd08d

Browse files
committed
fix(start-static-server-functions): treat a wrongly shaped payload as a miss
Decoding can succeed for a payload that is not a cached result, for instance a seroval encoded plain string left at the cache path by another build step. Such a value is truthy, so the caller took it for a hit, skipped the server function and handed back an undefined result. Require the decoded value to be an object carrying both `result` and `context` before accepting it. `addItemToCache` always writes both keys and seroval keeps them even when the context is undefined, so a genuine hit is unaffected. Plain JSON that is not seroval encoded already threw inside `fromJSON` and was caught, so only the valid-seroval-wrong-shape case needed the check.
1 parent e5c9d7d commit 8bcd08d

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎packages/start-static-server-functions/src/staticFunctionMiddleware.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ const fetchItem = async ({
160160
return undefined
161161
}
162162

163+
// Decoding can succeed for a payload that is not a cached result, for
164+
// example a JSON asset that another build step left at this path. Such a
165+
// value is truthy, so without a shape check the caller would treat it as a
166+
// hit and hand back an undefined result instead of calling the server
167+
// function. `addItemToCache` always writes both keys, and seroval keeps them
168+
// even when the context is undefined.
169+
if (
170+
result === null ||
171+
typeof result !== 'object' ||
172+
!Object.prototype.hasOwnProperty.call(result, 'result') ||
173+
!Object.prototype.hasOwnProperty.call(result, 'context')
174+
) {
175+
return undefined
176+
}
177+
163178
staticClientCache?.set(url, result)
164179

165180
return result

‎packages/start-static-server-functions/tests/staticFunctionMiddleware.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ describe('staticFunctionMiddleware client on a cache miss', () => {
9797
expect(next).toHaveBeenCalledTimes(1)
9898
})
9999

100+
test('falls back to the server function when the payload is not a cached result', async () => {
101+
// Valid seroval, but for a plain string rather than a StaticCachedResult.
102+
// It decodes to a truthy value, so only a shape check can tell it apart
103+
// from a real hit.
104+
const payload = JSON.stringify(await toJSONAsync('not a cached result'))
105+
vi.stubGlobal(
106+
'fetch',
107+
vi.fn(async () => jsonResponse(payload)),
108+
)
109+
110+
const { promise, next } = callClientMiddleware({ case: 'wrong-shape' })
111+
112+
await expect(promise).resolves.toBe(LIVE_RESULT)
113+
expect(next).toHaveBeenCalledTimes(1)
114+
})
115+
116+
test('falls back to the server function when the payload is missing context', async () => {
117+
const payload = JSON.stringify(await toJSONAsync({ result: 'partial' }))
118+
vi.stubGlobal(
119+
'fetch',
120+
vi.fn(async () => jsonResponse(payload)),
121+
)
122+
123+
const { promise, next } = callClientMiddleware({ case: 'partial-shape' })
124+
125+
await expect(promise).resolves.toBe(LIVE_RESULT)
126+
expect(next).toHaveBeenCalledTimes(1)
127+
})
128+
100129
test('falls back to the server function when the request fails outright', async () => {
101130
vi.stubGlobal(
102131
'fetch',

0 commit comments

Comments
 (0)