-
Notifications
You must be signed in to change notification settings - Fork 0
fix(reliability): restack limiter JSON protocol on protected main #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9aedfb8
test(rate-limit): require exact JSON request media type
seonghobae 89b7cb6
chore(rate-limit): refresh RED on protected main
seonghobae 4bded02
chore(rate-limit): refresh RED after main advance
seonghobae de7ee01
fix(reliability): harden limiter JSON protocol
seonghobae f199fda
chore(reliability): merge protected main into limiter hardening
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { isJsonMediaType, NoemaRateLimiter } from "../src/rate-limit"; | ||
|
|
||
| describe("rate limit media type", () => { | ||
| it("accepts only application/json as the media type", () => { | ||
| expect(isJsonMediaType("application/json")).toBe(true); | ||
| expect(isJsonMediaType(" APPLICATION/JSON ; charset=utf-8")).toBe(true); | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| expect(isJsonMediaType("text/plain; profile=application/json")).toBe(false); | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| expect(isJsonMediaType(null)).toBe(false); | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| }); | ||
|
|
||
| it("rejects a misleading JSON profile at the Durable Object boundary", async () => { | ||
| const limiter = new NoemaRateLimiter({} as DurableObjectState); | ||
| const response = await limiter.fetch(new Request("https://noema-rate-limit.internal/check", { | ||
| method: "POST", | ||
| headers: { "content-type": "text/plain; profile=application/json" }, | ||
| body: JSON.stringify({ limit: 60 }), | ||
| })); | ||
|
|
||
| expect(response.status).toBe(415); | ||
| expect(response.headers.get("content-type")).toBe("application/json; charset=utf-8"); | ||
| await expect(response.json()).resolves.toEqual({ | ||
| ok: false, | ||
| error: "content_type_required", | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| checkDistributedRateLimit, | ||
| DistributedRateLimitUnavailable, | ||
| type DistributedRateLimitEnv, | ||
| } from "../src/rate-limit"; | ||
|
|
||
| const request = new Request("https://noema.example/exchange", { | ||
| headers: { "cf-connecting-ip": "203.0.113.90" }, | ||
| }); | ||
|
|
||
| const decision = { | ||
| allowed: true, | ||
| limit: 60, | ||
| remaining: 59, | ||
| retry_after_seconds: 0, | ||
| }; | ||
|
|
||
| function envReturning(response: Response): DistributedRateLimitEnv { | ||
| return { | ||
| NOEMA_RATE_LIMITER: { | ||
| idFromName(name: string) { | ||
| return { toString: () => name } as DurableObjectId; | ||
| }, | ||
| get() { | ||
| return { | ||
| fetch: async () => response, | ||
| } as unknown as DurableObjectStub; | ||
| }, | ||
| } as unknown as DurableObjectNamespace, | ||
| }; | ||
| } | ||
|
|
||
| describe("distributed rate-limit response protocol", () => { | ||
| it("accepts only the exact HTTP 200 JSON decision contract", async () => { | ||
| await expect( | ||
| checkDistributedRateLimit( | ||
| request, | ||
| envReturning(new Response(JSON.stringify(decision), { | ||
| status: 200, | ||
| headers: { "content-type": "application/json; charset=utf-8" }, | ||
| })), | ||
| ), | ||
| ).resolves.toEqual(decision); | ||
|
|
||
| await expect( | ||
| checkDistributedRateLimit(request, envReturning(Response.json(decision, { status: 201 }))), | ||
| ).rejects.toThrow(DistributedRateLimitUnavailable); | ||
|
|
||
| await expect( | ||
| checkDistributedRateLimit( | ||
| request, | ||
| envReturning(new Response(JSON.stringify(decision), { | ||
| status: 200, | ||
| headers: { "content-type": "text/plain; profile=application/json" }, | ||
| })), | ||
| ), | ||
| ).rejects.toThrow(DistributedRateLimitUnavailable); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.