-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(core): cap idempotencyKey length at the API boundary #3560
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
Open
d-cs
wants to merge
2
commits into
main
Choose a base branch
from
idempotency-key-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@trigger.dev/core": patch | ||
| --- | ||
|
|
||
| Reject overlong `idempotencyKey` values at the API boundary so they no longer trip an internal size limit on the underlying unique index and surface as a generic 500. Inputs are capped at 2048 characters — well above what `idempotencyKeys.create()` produces (a 64-character hash) and above any realistic raw key. Applies to `tasks.trigger`, `tasks.batchTrigger`, `batch.create` (Phase 1 streaming batches), `wait.createToken`, `wait.forDuration`, and the input/session stream waitpoint endpoints. Over-limit requests now return a structured 400 instead. |
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
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,173 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { | ||
| BatchTriggerTaskItem, | ||
| CreateBatchRequestBody, | ||
| CreateInputStreamWaitpointRequestBody, | ||
| CreateSessionStreamWaitpointRequestBody, | ||
| CreateWaitpointTokenRequestBody, | ||
| TriggerTaskRequestBody, | ||
| WaitForDurationRequestBody, | ||
| } from "./api.js"; | ||
|
|
||
| // These tests verify the zod-level character cap (.max(2048)) on schemas whose | ||
| // idempotencyKey lands against a unique composite index downstream. The cap | ||
| // itself is a JS-string-length check, so the constants below are chosen to | ||
| // exercise the boundary cleanly — high entropy isn't required for this layer. | ||
| const TOO_LONG = "x".repeat(3000); | ||
| const AT_LIMIT = "x".repeat(2048); | ||
| const SDK_HASH = "a".repeat(64); // shape of idempotencyKeys.create() output | ||
|
|
||
| describe("idempotencyKey length validation", () => { | ||
| describe("TriggerTaskRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = TriggerTaskRequestBody.safeParse({ | ||
| payload: {}, | ||
| options: { idempotencyKey: TOO_LONG }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["options", "idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts an idempotencyKey at the 2048-character limit", () => { | ||
| const result = TriggerTaskRequestBody.safeParse({ | ||
| payload: {}, | ||
| options: { idempotencyKey: AT_LIMIT }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it("accepts the SDK-generated 64-character hash", () => { | ||
| const result = TriggerTaskRequestBody.safeParse({ | ||
| payload: {}, | ||
| options: { idempotencyKey: SDK_HASH }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("BatchTriggerTaskItem", () => { | ||
| it("rejects an idempotencyKey over 2048 characters", () => { | ||
| const result = BatchTriggerTaskItem.safeParse({ | ||
| task: "my-task", | ||
| payload: {}, | ||
| options: { idempotencyKey: TOO_LONG }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("accepts an idempotencyKey at the 2048-character limit", () => { | ||
| const result = BatchTriggerTaskItem.safeParse({ | ||
| task: "my-task", | ||
| payload: {}, | ||
| options: { idempotencyKey: AT_LIMIT }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CreateBatchRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = CreateBatchRequestBody.safeParse({ | ||
| runCount: 1, | ||
| idempotencyKey: TOO_LONG, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts an idempotencyKey at the 2048-character limit", () => { | ||
| const result = CreateBatchRequestBody.safeParse({ | ||
| runCount: 1, | ||
| idempotencyKey: AT_LIMIT, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CreateWaitpointTokenRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = CreateWaitpointTokenRequestBody.safeParse({ | ||
| idempotencyKey: TOO_LONG, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts an idempotencyKey at the 2048-character limit", () => { | ||
| const result = CreateWaitpointTokenRequestBody.safeParse({ | ||
| idempotencyKey: AT_LIMIT, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CreateInputStreamWaitpointRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = CreateInputStreamWaitpointRequestBody.safeParse({ | ||
| streamId: "stream_1", | ||
| idempotencyKey: TOO_LONG, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("CreateSessionStreamWaitpointRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = CreateSessionStreamWaitpointRequestBody.safeParse({ | ||
| session: "session_1", | ||
| io: "out", | ||
| idempotencyKey: TOO_LONG, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("WaitForDurationRequestBody", () => { | ||
| it("rejects an idempotencyKey over 2048 characters with a clear message", () => { | ||
| const result = WaitForDurationRequestBody.safeParse({ | ||
| date: new Date(), | ||
| idempotencyKey: TOO_LONG, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| const issue = result.error.issues[0]!; | ||
| expect(issue.path).toEqual(["idempotencyKey"]); | ||
| expect(issue.message).toBe("idempotencyKey must be 2048 characters or less"); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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.