Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,33 @@ export function ensureOrigin(req: express.Request, _?: express.Response, next?:
}
}

/**
* Return true if the origin matches any trusted origin. Entries are matched
* as exact strings, the special wildcard `"*"`, or regex literals in the form
* `/pattern/flags` (e.g. `/^.*\.example\.com$/i`).
*/
export function isTrustedOrigin(origin: string, trustedOrigins: string[]): boolean {
return trustedOrigins.some((trusted) => {
if (trusted === "*" || trusted === origin) {
return true
}
// Regex literal: /pattern/ or /pattern/flags
if (trusted.startsWith("/")) {
const closingSlash = trusted.lastIndexOf("/")
if (closingSlash > 0) {
const pattern = trusted.slice(1, closingSlash)
const flags = trusted.slice(closingSlash + 1)
try {
return new RegExp(pattern, flags).test(origin)
} catch {
return false
}
}
}
return false
})
}

/**
* Authenticate the request origin against the host. Throw if invalid.
*/
Expand All @@ -370,7 +397,7 @@ export function authenticateOrigin(req: express.Request): void {
}

const trustedOrigins = req.args["trusted-origins"] || []
if (trustedOrigins.includes(origin) || trustedOrigins.includes("*")) {
if (isTrustedOrigin(origin, trustedOrigins)) {
return
}

Expand Down
50 changes: 49 additions & 1 deletion test/unit/node/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ describe("http", () => {
expect(http.relativeRoot("/foo/bar/")).toStrictEqual("./../..")
})

describe("isTrustedOrigin", () => {
it("should match exact origins", () => {
expect(http.isTrustedOrigin("localhost:8080", ["localhost:8080"])).toBe(true)
expect(http.isTrustedOrigin("example.com", ["example.com"])).toBe(true)
expect(http.isTrustedOrigin("example.com", ["other.com"])).toBe(false)
})

it("should match the wildcard *", () => {
expect(http.isTrustedOrigin("anything.example.com", ["*"])).toBe(true)
expect(http.isTrustedOrigin("localhost:8080", ["*"])).toBe(true)
})

it("should match regex patterns", () => {
expect(http.isTrustedOrigin("sub.example.com", ["/\\.example\\.com$/"])).toBe(true)
expect(http.isTrustedOrigin("evil.com", ["/\\.example\\.com$/"])).toBe(false)
})

it("should support regex flags", () => {
expect(http.isTrustedOrigin("SUB.EXAMPLE.COM", ["/\\.example\\.com$/i"])).toBe(true)
})

it("should return false for invalid regex patterns", () => {
expect(http.isTrustedOrigin("example.com", ["/[invalid/"])).toBe(false)
})

it("should return false for an empty trusted origins list", () => {
expect(http.isTrustedOrigin("example.com", [])).toBe(false)
})
})

describe("origin", () => {
;[
{
Expand Down Expand Up @@ -54,6 +84,22 @@ describe("http", () => {
host: "localhost:8080",
expected: "malformed", // Parsing fails completely.
},
{
origin: "http://sub.example.com",
host: "other.com",
trustedOrigins: ["/\\.example\\.com$/"],
},
{
origin: "http://evil.com",
host: "other.com",
trustedOrigins: ["/\\.example\\.com$/"],
expected: "does not match",
},
{
origin: "http://sub.example.com",
host: "other.com",
trustedOrigins: ["*"],
},
].forEach((test) => {
;[
["host", test.host],
Expand All @@ -70,7 +116,9 @@ describe("http", () => {
origin: test.origin,
[key]: value,
},
args: {},
args: {
"trusted-origins": (test as { trustedOrigins?: string[] }).trustedOrigins,
},
})
if (typeof test.expected === "string") {
expect(() => http.authenticateOrigin(req)).toThrow(test.expected)
Expand Down
Loading