-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add CORS for Commerce Manager endpoints #106
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
3 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
Some comments aren't visible on the classic Files Changed page.
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,87 @@ | ||
| import { isCmOriginAllowed } from "@/wab/server/cm-cors"; | ||
|
|
||
| describe("isCmOriginAllowed", () => { | ||
| describe("valid origins", () => { | ||
| it("should allow *.cm.elasticpath.com origins", () => { | ||
| expect(isCmOriginAllowed("https://integration.cm.elasticpath.com")).toBe( | ||
| true | ||
| ); | ||
| expect(isCmOriginAllowed("https://staging.cm.elasticpath.com")).toBe(true); | ||
| expect(isCmOriginAllowed("https://useast.cm.elasticpath.com")).toBe(true); | ||
| expect(isCmOriginAllowed("https://euwest.cm.elasticpath.com")).toBe(true); | ||
| expect(isCmOriginAllowed("https://new-env.cm.elasticpath.com")).toBe(true); | ||
| }); | ||
|
|
||
| it("should allow localhost:3000", () => { | ||
| expect(isCmOriginAllowed("http://localhost:3000")).toBe(true); | ||
| }); | ||
|
|
||
| it("should allow Vercel preview deployments", () => { | ||
| expect( | ||
| isCmOriginAllowed( | ||
| "https://3492--integration-commerce-manager.vercel.app" | ||
| ) | ||
| ).toBe(true); | ||
| expect( | ||
| isCmOriginAllowed("https://123--staging-commerce-manager.vercel.app") | ||
| ).toBe(true); | ||
| expect( | ||
| isCmOriginAllowed("https://99999--prod-commerce-manager.vercel.app") | ||
| ).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("invalid origins", () => { | ||
| it("should reject undefined/null origins", () => { | ||
| expect(isCmOriginAllowed(undefined)).toBe(false); | ||
| }); | ||
|
|
||
| it("should reject malicious domains", () => { | ||
| expect(isCmOriginAllowed("https://malicious.com")).toBe(false); | ||
| expect(isCmOriginAllowed("https://evil.cm.elasticpath.com.attacker.com")).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it("should reject domains trying to bypass with similar names", () => { | ||
| expect(isCmOriginAllowed("https://cm.elasticpath.com")).toBe(false); | ||
| expect(isCmOriginAllowed("https://fakecm.elasticpath.com")).toBe(false); | ||
| expect(isCmOriginAllowed("https://test.cm.elasticpath.com.evil.com")).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it("should reject origins with underscores (invalid DNS)", () => { | ||
| expect(isCmOriginAllowed("https://test_env.cm.elasticpath.com")).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it("should reject http instead of https for production domains", () => { | ||
| expect(isCmOriginAllowed("http://integration.cm.elasticpath.com")).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| it("should reject localhost on other ports", () => { | ||
| expect(isCmOriginAllowed("http://localhost:3001")).toBe(false); | ||
| expect(isCmOriginAllowed("http://localhost:8080")).toBe(false); | ||
| }); | ||
|
|
||
| it("should reject invalid Vercel preview URLs", () => { | ||
| expect(isCmOriginAllowed("https://random.vercel.app")).toBe(false); | ||
| expect( | ||
| isCmOriginAllowed("https://not-a-number--integration-commerce-manager.vercel.app") | ||
| ).toBe(false); | ||
| expect( | ||
| isCmOriginAllowed("https://123--other-app.vercel.app") | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("should reject origins with ports for production domains", () => { | ||
| expect(isCmOriginAllowed("https://integration.cm.elasticpath.com:8080")).toBe( | ||
| false | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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,57 @@ | ||
| import cors from "cors"; | ||
| import express, { RequestHandler } from "express"; | ||
| import { safeCast } from "@/wab/shared/common"; | ||
| import { logger } from "@/wab/server/observability"; | ||
|
|
||
| // CORS configuration restricted to Commerce Manager origins | ||
|
|
||
| // Regex for *.cm.elasticpath.com (any subdomain, alphanumerics and hyphens only) | ||
| const cmOriginPattern = /^https:\/\/[a-zA-Z0-9-]+\.cm\.elasticpath\.com$/; | ||
|
|
||
| // Regex for Vercel preview deployments: {MR_NUMBER}--{env}-commerce-manager.vercel.app | ||
| const cmPreviewOriginPattern = | ||
| /^https:\/\/\d+--[a-zA-Z0-9-]+-commerce-manager\.vercel\.app$/; | ||
|
|
||
| // localhost for development | ||
| const cmLocalOrigin = "http://localhost:3000"; | ||
|
|
||
| export function isCmOriginAllowed(origin: string | undefined): boolean { | ||
| if (!origin) return false; | ||
| if (origin === cmLocalOrigin) return true; | ||
| if (cmOriginPattern.test(origin)) return true; | ||
| if (cmPreviewOriginPattern.test(origin)) return true; | ||
| logger().warn(`CM CORS rejected origin: ${origin}`); | ||
| return false; | ||
| } | ||
|
|
||
| const cmCorsOptions: cors.CorsOptions = { | ||
| origin: (origin, callback) => { | ||
| if (isCmOriginAllowed(origin)) { | ||
| callback(null, true); | ||
| } else { | ||
| callback(null, false); | ||
| } | ||
| }, | ||
| credentials: true, | ||
| }; | ||
|
|
||
| export const cmCors = cors(cmCorsOptions); | ||
|
|
||
| export function cmCorsPreflight() { | ||
| const corsHandler = cors({ | ||
| ...cmCorsOptions, | ||
| maxAge: 30 * 24 * 60 * 60, | ||
| allowedHeaders: "*", | ||
| }); | ||
|
|
||
| const handler: express.RequestHandler = safeCast<RequestHandler>( | ||
| async (req, res, next) => { | ||
| res.set( | ||
| "Cache-Control", | ||
| `max-age=${30 * 24 * 60 * 60}, s-maxage=${30 * 24 * 60 * 60}` | ||
| ); | ||
| corsHandler(req, res, next); | ||
| } | ||
| ); | ||
| return handler; | ||
| } | ||
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.