-
-
Notifications
You must be signed in to change notification settings - Fork 169
fix(s3): trim whitespace in bucket name + creds so uploads can't break on a stray space #1338
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
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,45 @@ | ||
| import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
|
|
||
| // Regression guard: a trailing space in S3_BUCKET_NAME (or the keys) once made | ||
| // every presigned PUT fail with 400 InvalidBucketName. These values must be | ||
| // trimmed before they reach the bucket name / SigV4 credential. | ||
| describe("getPresignedUrl whitespace hardening", () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let getPresignedUrl: any; | ||
|
|
||
| beforeAll(async () => { | ||
| // Whitespace-padded credentials, set before import so the s3 client (built | ||
| // at module load) sees them. | ||
| process.env.ACCESS_KEY = "AKIATESTKEY1234567890 "; | ||
| process.env.SECRET_KEY = "secretsecretsecretsecretsecretsecret1234\n"; | ||
| ({ getPresignedUrl } = await import("@/server/common/getPresignedUrl")); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| delete process.env.ACCESS_KEY; | ||
| delete process.env.SECRET_KEY; | ||
| delete process.env.S3_BUCKET_NAME; | ||
| }); | ||
|
|
||
| it("trims a trailing space in S3_BUCKET_NAME so the PUT targets the real bucket", async () => { | ||
| process.env.S3_BUCKET_NAME = "codu.uploads "; // <- the prod footgun | ||
| const url = await getPresignedUrl("image/png", 123, { | ||
| kind: "uploads", | ||
| userId: "u1", | ||
| }); | ||
| expect(url).toContain("/codu.uploads/"); | ||
| expect(url).not.toContain("codu.uploads%20"); | ||
| expect(url).not.toContain("codu.uploads "); | ||
| }); | ||
|
|
||
| it("trims whitespace in the access key used to sign the URL", async () => { | ||
| process.env.S3_BUCKET_NAME = "codu.uploads"; | ||
| const url = await getPresignedUrl("image/png", 123, { | ||
| kind: "uploads", | ||
| userId: "u1", | ||
| }); | ||
| const cred = new URL(url).searchParams.get("X-Amz-Credential") ?? ""; | ||
| expect(cred.startsWith("AKIATESTKEY1234567890/")).toBe(true); | ||
| expect(cred).not.toContain("%20"); | ||
| }); | ||
| }); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Add cleanup to restore environment variables.
The
beforeAllhook modifiesprocess.env.ACCESS_KEY,process.env.SECRET_KEY, and the tests modifyprocess.env.S3_BUCKET_NAME, but these changes are never reverted. This can pollute other tests if Vitest runs them in the same process or if other tests depend on these variables.🧹 Add cleanup with afterAll
describe("getPresignedUrl whitespace hardening", () => { // eslint-disable-next-line `@typescript-eslint/no-explicit-any` let getPresignedUrl: any; + const originalEnv = { + ACCESS_KEY: process.env.ACCESS_KEY, + SECRET_KEY: process.env.SECRET_KEY, + S3_BUCKET_NAME: process.env.S3_BUCKET_NAME, + }; beforeAll(async () => { // Whitespace-padded credentials, set before import so the s3 client (built // at module load) sees them. process.env.ACCESS_KEY = "AKIATESTKEY1234567890 "; process.env.SECRET_KEY = "secretsecretsecretsecretsecretsecret1234\n"; ({ getPresignedUrl } = await import("`@/server/common/getPresignedUrl`")); }); + + afterAll(() => { + process.env.ACCESS_KEY = originalEnv.ACCESS_KEY; + process.env.SECRET_KEY = originalEnv.SECRET_KEY; + process.env.S3_BUCKET_NAME = originalEnv.S3_BUCKET_NAME; + });🤖 Prompt for AI Agents