Skip to content

Commit ef24ff2

Browse files
authored
fix(workers-shared): guard against undefined user_worker static_routing rules (#13668)
1 parent 175ec8b commit ef24ff2

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@cloudflare/workers-shared": patch
3+
"miniflare": patch
4+
---
5+
6+
Fix `TypeError: rules is not iterable` in the router-worker when `static_routing` is configured without `user_worker` rules
7+
8+
The router-worker's static-routing include-rule evaluation passed `config.static_routing.user_worker` directly to the matcher, which iterates with `for...of`. When `static_routing` was set but `user_worker` was omitted, the matcher threw `TypeError: rules is not iterable` and failed the request. The adjacent `asset_worker` branch already falls back to `[]` in this case; the `user_worker` branch now does the same.

packages/workers-shared/router-worker/src/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export default {
219219
}
220220
// evaluate "include" rules
221221
const includeRulesMatcher = generateStaticRoutingRuleMatcher(
222-
config.static_routing.user_worker
222+
config.static_routing.user_worker ?? []
223223
);
224224
if (
225225
includeRulesMatcher({

packages/workers-shared/router-worker/tests/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,41 @@ describe("unit tests", () => {
201201
expect(await response.text()).toEqual("hello from asset worker");
202202
});
203203

204+
// Regression test for WC-5014 / Sentry #31445454: previously threw
205+
// `TypeError: rules is not iterable` when static_routing was defined but
206+
// user_worker was undefined.
207+
it("does not throw when static_routing is defined without user_worker rules", async ({
208+
expect,
209+
}) => {
210+
const request = new Request("https://example.com/unmatched-path");
211+
const ctx = createExecutionContext();
212+
213+
const env = {
214+
CONFIG: {
215+
has_user_worker: true,
216+
static_routing: {
217+
asset_worker: ["/assets/*"],
218+
},
219+
},
220+
USER_WORKER: {
221+
async fetch(_request: Request): Promise<Response> {
222+
return new Response("hello from user worker");
223+
},
224+
},
225+
ASSET_WORKER: {
226+
async fetch(_request: Request): Promise<Response> {
227+
return new Response("hello from asset worker");
228+
},
229+
async unstable_canFetch(_request: Request): Promise<boolean> {
230+
return true;
231+
},
232+
},
233+
} as Env;
234+
235+
const response = await worker.fetch(request, env, ctx);
236+
expect(await response.text()).toEqual("hello from asset worker");
237+
});
238+
204239
it("returns fetch from asset worker when no static_routing rule matches but asset exists", async ({
205240
expect,
206241
}) => {

0 commit comments

Comments
 (0)