Skip to content
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

Fixed Cookie Authentication #9186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions src/core/plugins/auth/wrap-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export const authorize = (oriAction, system) => (payload) => {
const isApiKeyInCookie = isApiKeyAuth && isInCookie

if (isApiKeyInCookie) {
document.cookie = `${schema.get("name")}=${value}; SameSite=None; Secure`
const secure = `${configs.url?.split("/")[0] === "https:" ? ";secure" : ""}`
const urlBasePath = configs.url?.split("/").splice(3).join("/")
const path = `${urlBasePath === undefined ? ";path=/" : ";path=/".concat(urlBasePath)}`
let cookieStr = `${schema.get("name")}=${value};samesite=None${secure}${path}`
document.cookie = cookieStr
}
} catch (error) {
console.error(
Expand All @@ -49,7 +53,9 @@ export const logout = (oriAction, system) => (payload) => {

if (isApiKeyInCookie) {
const cookieName = auth.getIn(["schema", "name"])
document.cookie = `${cookieName}=; Max-Age=-99999999`
const urlBasePath = configs.url?.split("/").splice(3).join("/")
const path = `${urlBasePath === undefined ? ";path=/" : ";path=/".concat(urlBasePath)}`
document.cookie = `${cookieName}=;max-age=-99999999${path}`
}
})
}
Expand Down
156 changes: 154 additions & 2 deletions test/unit/core/plugins/auth/wrap-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,107 @@ describe("Cookie based apiKey persistence in document.cookie", () => {
authorize(jest.fn(), system)(payload)

expect(document.cookie).toEqual(
"apiKeyCookie=test; SameSite=None; Secure"
"apiKeyCookie=test;samesite=None;path=/"
)
})

it("should persist secure cookie in document.cookie for non-SSL targets", () => {
const system = {
getConfigs: () => ({
persistAuthorization: true,
url: "http://example.org"
}),
}
const payload = {
api_key: {
schema: fromJS({
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
}),
value: "test",
},
}

authorize(jest.fn(), system)(payload)

expect(document.cookie).toEqual(
"apiKeyCookie=test;samesite=None;path=/"
)
})

it("should persist secure cookie in document.cookie for SSL targets", () => {
const system = {
getConfigs: () => ({
persistAuthorization: true,
url: "https://example.org"
}),
}
const payload = {
api_key: {
schema: fromJS({
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
}),
value: "test",
},
}

authorize(jest.fn(), system)(payload)

expect(document.cookie).toEqual(
"apiKeyCookie=test;samesite=None;secure;path=/"
)
})

it("should persist secure cookie in document.cookie for non-root SSL targets", () => {
const system = {
getConfigs: () => ({
persistAuthorization: true,
url: "https://example.org/api"
}),
}
const payload = {
api_key: {
schema: fromJS({
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
}),
value: "test",
},
}

authorize(jest.fn(), system)(payload)

expect(document.cookie).toEqual(
"apiKeyCookie=test;samesite=None;secure;path=/api"
)
})

it("should persist secure cookie in document.cookie for SSL targets with non-root multi-level base path", () => {
const system = {
getConfigs: () => ({
persistAuthorization: true,
url: "https://example.org/api/production"
}),
}
const payload = {
api_key: {
schema: fromJS({
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
}),
value: "test",
},
}

authorize(jest.fn(), system)(payload)

expect(document.cookie).toEqual(
"apiKeyCookie=test;samesite=None;secure;path=/api/production"
)
})

Expand All @@ -64,7 +164,33 @@ describe("Cookie based apiKey persistence in document.cookie", () => {

logout(jest.fn(), system)(["api_key"])

expect(document.cookie).toEqual("apiKeyCookie=; Max-Age=-99999999")
expect(document.cookie).toEqual("apiKeyCookie=;max-age=-99999999;path=/")
})

it("should delete cookie from document.cookie for targets with non-root multi-level base path", () => {
const payload = fromJS({
api_key: {
schema: {
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
},
value: "test",
},
})
const system = {
getConfigs: () => ({
persistAuthorization: true,
url: "https://example.org/api/production"
}),
authSelectors: {
authorized: () => payload,
},
}

logout(jest.fn(), system)(["api_key"])

expect(document.cookie).toEqual("apiKeyCookie=;max-age=-99999999;path=/api/production")
})
})

Expand Down Expand Up @@ -116,4 +242,30 @@ describe("Cookie based apiKey persistence in document.cookie", () => {
expect(document.cookie).toEqual("")
})
})

it("should delete cookie from document.cookie for targets with non-root multi-level base path", () => {
const payload = fromJS({
api_key: {
schema: {
type: "apiKey",
name: "apiKeyCookie",
in: "cookie",
},
value: "test",
},
})
const system = {
getConfigs: () => ({
persistAuthorization: false,
url: "https://example.org/api/production"
}),
authSelectors: {
authorized: () => payload,
},
}

logout(jest.fn(), system)(["api_key"])

expect(document.cookie).toEqual("")
})
})