Skip to content

Commit

Permalink
fix(core): createActionURL set detectedProtocol correctly (#10421)
Browse files Browse the repository at this point in the history
* fix: possible invalid url in createActionURL

* fix: x-forwarded-proto goes without colon
  • Loading branch information
Carlos-err406 committed Mar 31, 2024
1 parent 11cfb0a commit 246a838
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/lib/utils/env.ts
Expand Up @@ -76,8 +76,9 @@ export function createActionURL(
const detectedHost = headers.get("x-forwarded-host") ?? headers.get("host")
const detectedProtocol =
headers.get("x-forwarded-proto") ?? protocol ?? "https"

url = new URL(`${detectedProtocol}://${detectedHost}`)
const _protocol = detectedProtocol.endsWith(":") ? detectedProtocol : detectedProtocol + ':'

url = new URL(`${_protocol}//${detectedHost}`)
}

// remove trailing slash
Expand Down
52 changes: 51 additions & 1 deletion packages/core/test/env.test.ts
Expand Up @@ -108,7 +108,7 @@ describe("config is inferred from environment variables", () => {
})

describe("createActionURL", () => {
const consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => { })
const consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})

afterEach(() => {
consoleWarnSpy.mockClear()
Expand Down Expand Up @@ -161,6 +161,56 @@ describe("createActionURL", () => {
},
expected: "https://example.com/auth/signin",
},
{
args: {
action: "signin",
protocol: "http:",
headers: new Headers({
"x-forwarded-host": "example.com",
}),
env: {},
basePath: "/auth",
},
expected: "http://example.com/auth/signin",
},
{
args: {
action: "signin",
protocol: "https:",
headers: new Headers({
"x-forwarded-host": "example.com",
}),
env: {},
basePath: "/auth",
},
expected: "https://example.com/auth/signin",
},
{
args: {
action: "signin",
protocol: undefined,
headers: new Headers({
"x-forwarded-host": "example.com",
"x-forwarded-proto": "https",
}),
env: {},
basePath: "/auth",
},
expected: "https://example.com/auth/signin",
},
{
args: {
action: "signin",
protocol: undefined,
headers: new Headers({
"x-forwarded-host": "example.com",
"x-forwarded-proto": "http",
}),
env: {},
basePath: "/auth",
},
expected: "http://example.com/auth/signin",
},
{
args: {
action: "signout",
Expand Down

0 comments on commit 246a838

Please sign in to comment.