-
Notifications
You must be signed in to change notification settings - Fork 0
fix(reliability): harden limiter JSON protocol #269
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab2d6ab
test(reliability): define exact JSON media type boundary
seonghobae 54e6331
fix(reliability): enforce exact JSON media type
seonghobae 98a169b
test(reliability): cover JSON content type cases
seonghobae 0d84452
fix(reliability): remove impossible media-type coverage branch
seonghobae 6e51ec7
test(rate-limit): require exact limiter response protocol
seonghobae 62d1cb9
fix(rate-limit): authenticate limiter response contract
seonghobae bedd48c
merge(main): synchronize current protected main into #269
seonghobae a25a24a
Merge main after exchange JSON boundary hardening
seonghobae 0374ff6
Merge protected main after OIDC replay media-type hardening
seonghobae 591a898
test(reliability): strengthen limiter media-type regressions
seonghobae 3f594e8
Merge branch 'main' into fix/rate-limit-content-type-boundary
opencode-agent[bot] 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); | ||
| expect(isJsonMediaType("text/plain; profile=application/json")).toBe(false); | ||
| expect(isJsonMediaType(null)).toBe(false); | ||
| }); | ||
|
|
||
| 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.