Skip to content

Proxy 10% of Worker traffic to Wasmer - #232

Closed
jaredwray wants to merge 3 commits into
mainfrom
cursor/wasmer-traffic-split-3694
Closed

jaredwray wants to merge 3 commits into
mainfrom
cursor/wasmer-traffic-split-3694

Conversation

@jaredwray

Copy link
Copy Markdown
Owner

Please check if the PR fulfills these requirements

  • Followed the Contributing and Code of Conduct guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce?

Feature. mockhttp.org stays on the Cloudflare Worker. Dynamic requests are still rate-limited there, and 10% of them are proxied to the Wasmer Node app.

What is the current behavior?

Every dynamic mockhttp.org request is handled by the Worker. https://mockhttp.wasmer.app is a separate test app.

What is the new behavior?

WASMER_TRAFFIC_PERCENT is 10. The Worker hashes the client IP into a bucket from 0 to 99 and proxies buckets under 10 to https://mockhttp.wasmer.app. The same client stays on one backend. Documentation from Workers Assets is unchanged. Redirect Location values that point at the Wasmer host are rewritten back to the incoming host. Allowlisted X-Forwarded-Host values (mockhttp.org, www.mockhttp.org) are what echoed URLs use.

Wasmer rate-limits by connecting IP, which would group every proxied client behind Cloudflare. Set the Wrangler secret WASMER_EDGE_TOKEN and the Wasmer secret EDGE_PROXY_TOKEN to the same value before deploying this Worker so those clients are limited by x-mockhttp-client-ip.

Other information

pnpm test: 47 files, 559 tests passed. Line coverage stayed at 100%.

Open in Web Open in Cursor 

Keep mockhttp.org on the Cloudflare Worker and send a sticky 10% of
dynamic requests to mockhttp.wasmer.app after the edge rate limit.

Co-authored-by: Jared Wray <me@jaredwray.com>
@jaredwray
jaredwray marked this pull request as ready for review October 6, 2026 08:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-06T08:55:34.275358Z 4d747fd Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread worker/app.ts
Resolve the conflict in site/docs/hosted.md: keep this branch's
description of the 10% Wasmer traffic split and add main's link to the
Wasmer Edge deploy guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014wUSAto7jX3BKVDSm5YaJE

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d747fdb54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread worker/app.ts
Comment on lines +245 to +246
if (env.WASMER_EDGE_TOKEN) {
headers.set(EDGE_TOKEN_HEADER, env.WASMER_EDGE_TOKEN);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the edge token out of echoed request headers

When WASMER_EDGE_TOKEN is configured as instructed, this adds the shared secret to the ordinary Fastify request headers. Endpoints such as /get, /headers, and /anything return request.headers, so any client routed to Wasmer receives the secret in its response. The client can then call the public Wasmer app directly with that token and arbitrary x-mockhttp-client-ip values, defeating the per-client rate limit by rotating the claimed IP. The authentication value must be removed before route handlers can observe the headers, or conveyed through a mechanism that echo endpoints cannot expose.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
return true;
}

return trafficBucket(clientIp(request)) < percent;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Route each request bin independently of the caller IP

Hashing only the caller IP breaks request bins whenever the client creating or inspecting a bin and the webhook sender fall into different buckets. For example, a POST /bins routed to Wasmer stores the bin in that process's InMemoryBinStore, but a later /b/:id request from a Worker-bucket IP is dispatched to the separate Worker store and returns bin_not_found; the reverse direction fails similarly. This is a normal webhook-debugging flow involving two clients, so bin management and capture routes need backend affinity based on the bin itself, or they must remain on a single backend.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
Comment on lines +204 to +211
if (redirected.hostname !== origin.hostname) {
return response;
}

redirected.protocol = incoming.protocol;
redirected.host = incoming.host;
const headers = new Headers(response.headers);
headers.set("location", redirected.toString());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve explicit redirect destinations

For a proxied request such as /redirect-to?url=https://mockhttp.wasmer.app/target, the route intentionally returns that exact user-supplied destination, but this hostname check treats it as an internal self-redirect and rewrites it to https://mockhttp.org/target. The same mutation affects user-generated Location headers from response-inspection endpoints and also matches a configured origin on the same hostname but a different port. Restrict rewriting to redirects known to have been generated as internal links rather than rewriting every response whose Location happens to share the upstream hostname.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
Comment on lines +208 to +211
redirected.protocol = incoming.protocol;
redirected.host = incoming.host;
const headers = new Headers(response.headers);
headers.set("location", redirected.toString());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Rewrite absolute-redirect bodies with their Location header

When /absolute-redirect/:value is handled by Wasmer, that route constructs both its Location header and its HTML link from the Wasmer request host. This function rewrites only Location, so the returned HTML still links to mockhttp.wasmer.app even though the header points to mockhttp.org; this violates the endpoint's existing contract that the body link and redirect target match for every client in the Wasmer cohort. The public origin needs to reach the route itself, or the matching link in the body must be rewritten too.

Useful? React with 👍 / 👎.

Comment thread src/public-origin.ts
Comment on lines +27 to +29
const forwarded = firstHeader(request.headers["x-forwarded-host"]);
if (forwarded && ALLOWED_FORWARDED_HOSTS.has(hostnameOf(forwarded))) {
return hostnameOf(forwarded);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve an allowed forwarded host's port

When the public service is reached on a non-default port, such as https://mockhttp.org:8443, the Worker forwards mockhttp.org:8443, but this returns only the parsed hostname. Consequently /bins, /delay, and /stream generate URLs without :8443; a created bin URL may therefore point at a different or unavailable service. The hostname should be used for allowlist validation while the validated host-and-port value is retained for the generated origin.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
Comment on lines +224 to +226
const incoming = new URL(request.url);
const origin = wasmerOrigin(env);
const url = new URL(`${incoming.pathname}${incoming.search}`, origin);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the configured upstream base path

When WASMER_ORIGIN is configured with a path, such as the explicitly accepted https://edge.example.test/base, constructing the target from an absolute /${incoming.pathname} discards /base and sends every request to the origin root instead. Deployments that expose MockHTTP below a path prefix therefore receive requests at the wrong application or return 404s. Join the incoming path beneath the configured base path, or reject path-bearing origin values instead of silently accepting and then ignoring them.

Useful? React with 👍 / 👎.

The How to Deploy guides from main still said the Worker forwards every
request into the Fastify app and that Wasmer only rate-limits by the
connecting address. Note the WASMER_TRAFFIC_PERCENT split, how a fork
turns it off or points it at its own app, and how EDGE_PROXY_TOKEN keys
proxied clients on Wasmer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014wUSAto7jX3BKVDSm5YaJE
@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (44a95cd) to head (8be7f95).

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #232    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           42        46     +4     
  Lines         1419      1554   +135     
  Branches       289       320    +31     
==========================================
+ Hits          1419      1554   +135     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jaredwray jaredwray closed this Oct 6, 2026
@jaredwray
jaredwray deleted the cursor/wasmer-traffic-split-3694 branch October 6, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants