-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: add options to restrict ssr lambda functions to cloudfront-only access using OAC #5581
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
+455
−8
Merged
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
9797838
feat: add option to protect ssr server with origin access control
MrHertal 19d8aec
Merge branch 'dev' into feat/ssr-site-enable-oac
MrHertal 698d7c3
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 15a4fe3
feat: use metadata in key value store
MrHertal 9b87de7
Merge remote-tracking branch 'origin/dev' into feat/ssr-site-enable-oac
MrHertal f3dad04
Merge branch 'dev' into feat/ssr-site-enable-oac
MrHertal 5a37305
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 11a175d
fix: parent self
MrHertal 2d8e82e
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal e46f460
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal f82b440
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal b329512
docs: add an example of usage with tanstack start
MrHertal 82f5513
fix: add a better example
MrHertal 5c608fd
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal dd90849
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 47d0c58
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 90a7bb6
chore: remove outdated example
MrHertal 463bdb6
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 2dfcd97
feat: protect nextjs image optimiser
MrHertal 7220546
feat: lambda at edge
MrHertal cf6d2ae
chore: trim comments
MrHertal 9c182a2
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 6e8d8f0
Merge branch 'sst:dev' into feat/ssr-site-enable-oac
MrHertal 61f63e2
chore(review): move function to right folder
MrHertal 0791c64
chore(review): handle file upload
MrHertal e675073
chore(review): add note about lambda@edge long deletion
MrHertal 4d2d842
chore(review): update platform/functions/oac-edge-signer/index.ts
MrHertal 3210246
chore(review): prettier
MrHertal d11b8f2
chore(review): rename arg
MrHertal 4cedfbc
Merge branch 'anomalyco:dev' into feat/ssr-site-enable-oac
MrHertal 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,86 @@ | ||
| // Lambda@Edge function for automatic SHA256 header signing | ||
| // This function adds the required x-amz-content-sha256 header for POST/PUT/PATCH requests | ||
| // going to Lambda function URLs with Origin Access Control enabled. | ||
|
|
||
| import crypto from "crypto"; | ||
|
|
||
| interface CloudFrontRequest { | ||
| method: string; | ||
| uri: string; | ||
| body?: { | ||
| data: string; | ||
| encoding?: "base64" | "text"; | ||
| inputTruncated?: boolean; | ||
| }; | ||
| headers: Record<string, Array<{ key: string; value: string }>>; | ||
| } | ||
|
|
||
| interface CloudFrontResponse { | ||
| status: string; | ||
| statusDescription: string; | ||
| headers: Record<string, Array<{ key: string; value: string }>>; | ||
| body: string; | ||
| } | ||
|
|
||
| interface CloudFrontEvent { | ||
| Records: Array<{ | ||
| cf: { | ||
| request: CloudFrontRequest; | ||
| }; | ||
| }>; | ||
| } | ||
|
|
||
| export async function handler( | ||
| event: CloudFrontEvent, | ||
| ): Promise<CloudFrontRequest | CloudFrontResponse> { | ||
| const request = event.Records[0].cf.request; | ||
|
|
||
| // Only process requests that need SHA256 signing (methods with body) | ||
| if (!["POST", "PUT", "PATCH", "DELETE"].includes(request.method)) { | ||
| return request; | ||
| } | ||
|
|
||
| // Check if body was truncated (exceeds 1MB Lambda@Edge limit) | ||
| if (request.body?.inputTruncated) { | ||
| return { | ||
| status: "413", | ||
| statusDescription: "Payload Too Large", | ||
| headers: { | ||
| "content-type": [{ key: "Content-Type", value: "application/json" }], | ||
| }, | ||
| body: JSON.stringify({ | ||
| error: | ||
| "Request body exceeds 1MB Lambda@Edge limit. Use presigned S3 URLs for large uploads.", | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| // Get the request body as raw bytes (never convert to UTF-8 string) | ||
| const bodyBuffer = request.body?.data | ||
| ? request.body.encoding === "base64" | ||
| ? Buffer.from(request.body.data, "base64") | ||
| : Buffer.from(request.body.data) | ||
| : Buffer.alloc(0); | ||
|
|
||
| // Compute SHA256 hash of the raw bytes | ||
| const hash = crypto.createHash("sha256").update(bodyBuffer).digest("hex"); | ||
|
|
||
| // Add the x-amz-content-sha256 header in CloudFront format | ||
| request.headers["x-amz-content-sha256"] = [ | ||
| { | ||
| key: "x-amz-content-sha256", | ||
| value: hash, | ||
| }, | ||
| ]; | ||
|
|
||
| console.log( | ||
| `Added SHA256 header for ${request.method} request to ${request.uri}: ${hash}`, | ||
| ); | ||
| } catch (error) { | ||
| console.error("Error computing SHA256 hash:", error); | ||
| // Continue without the header rather than failing the request | ||
| } | ||
|
|
||
| return request; | ||
| } | ||
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
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.