-
Notifications
You must be signed in to change notification settings - Fork 74
fix: correctly handle __Host and __Secure cookies when restoring state #1255
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
Open
shadowusr
wants to merge
1
commit into
users/shadowusr/TESTPLANE-995.repl-instrumentation
Choose a base branch
from
users/shadowusr/TESTPLANE-996.save-state-fixes
base: users/shadowusr/TESTPLANE-995.repl-instrumentation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,221 @@ | ||
| "use strict"; | ||
|
|
||
| const restoreStateCommand = require("src/browser/commands/restoreState").default; | ||
| const { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } = require("src/constants/config"); | ||
| const { mkSessionStub_ } = require("../utils"); | ||
|
|
||
| describe('"restoreState" command', () => { | ||
| const sandbox = sinon.createSandbox(); | ||
|
|
||
| const mkBrowser_ = ({ session, automationProtocol = WEBDRIVER_PROTOCOL } = {}) => ({ | ||
| publicAPI: session, | ||
| config: { | ||
| automationProtocol, | ||
| isolation: false, | ||
| stateOpts: { | ||
| cookies: true, | ||
| localStorage: true, | ||
| sessionStorage: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const initWebdriverSession_ = ({ isBidi = false } = {}) => { | ||
| const session = mkSessionStub_(); | ||
| session.getUrl.resolves("https://example.com/page"); | ||
| session.isBidi = isBidi; | ||
| session.setCookies = sandbox.stub().named("setCookies").resolves(); | ||
| session.refresh = sandbox.stub().named("refresh").resolves(); | ||
|
|
||
| restoreStateCommand(mkBrowser_({ session })); | ||
|
|
||
| return session; | ||
| }; | ||
|
|
||
| const initDevtoolsSession_ = () => { | ||
| const session = mkSessionStub_(); | ||
| const page = { | ||
| setCookie: sandbox.stub().named("setCookie").resolves(), | ||
| frames: sandbox.stub().named("frames").returns([]), | ||
| reload: sandbox.stub().named("reload").resolves(), | ||
| target: sandbox.stub().named("target").returns({ _targetId: "active-target" }), | ||
| }; | ||
|
|
||
| session.getUrl.resolves("https://example.com/page"); | ||
| session.getWindowHandle = sandbox.stub().named("getWindowHandle").resolves("active-target"); | ||
| session.getPuppeteer.resolves({ | ||
| pages: sandbox.stub().named("pages").resolves([page]), | ||
| }); | ||
|
|
||
| restoreStateCommand(mkBrowser_({ session, automationProtocol: DEVTOOLS_PROTOCOL })); | ||
|
|
||
| return { page, session }; | ||
| }; | ||
|
|
||
| afterEach(() => sandbox.restore()); | ||
|
|
||
| it("should normalize cookie prefix constraints before webdriver restore", async () => { | ||
| const session = initWebdriverSession_(); | ||
| const hostCookie = { | ||
| name: "__Host-csrf-token", | ||
| value: "host-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/custom", | ||
| secure: false, | ||
| httpOnly: true, | ||
| sameSite: "Lax", | ||
| expires: 12345, | ||
| }; | ||
| const secureCookie = { | ||
| name: "__Secure-session", | ||
| value: "secure-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/auth", | ||
| secure: false, | ||
| sameSite: "Strict", | ||
| }; | ||
| const ordinaryCookie = { | ||
| name: "regular", | ||
| value: "regular-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/custom", | ||
| secure: false, | ||
| httpOnly: true, | ||
| sameSite: "Lax", | ||
| }; | ||
|
|
||
| await session.restoreState({ | ||
| data: { cookies: [hostCookie, secureCookie, ordinaryCookie] }, | ||
| cookies: true, | ||
| refresh: false, | ||
| }); | ||
|
|
||
| assert.calledOnce(session.setCookies); | ||
| assert.deepEqual(session.setCookies.firstCall.args[0], [ | ||
| { | ||
| name: "__Host-csrf-token", | ||
| value: "host-value", | ||
| path: "/", | ||
| secure: true, | ||
| httpOnly: true, | ||
| sameSite: "Lax", | ||
| expires: 12345, | ||
| }, | ||
| { | ||
| name: "__Secure-session", | ||
| value: "secure-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/auth", | ||
| secure: true, | ||
| sameSite: "Strict", | ||
| }, | ||
| ordinaryCookie, | ||
| ]); | ||
| assert.property(hostCookie, "domain"); | ||
| }); | ||
|
|
||
| it("should keep webdriver sameSite/BiDi normalization", async () => { | ||
| const session = initWebdriverSession_({ isBidi: true }); | ||
|
|
||
| await session.restoreState({ | ||
| data: { | ||
| cookies: [ | ||
| { | ||
| name: "same-site-none", | ||
| value: "value", | ||
| secure: false, | ||
| sameSite: "None", | ||
| }, | ||
| ], | ||
| }, | ||
| cookies: true, | ||
| refresh: false, | ||
| }); | ||
|
|
||
| assert.calledOnceWith(session.setCookies, [ | ||
| { | ||
| name: "same-site-none", | ||
| value: "value", | ||
| secure: true, | ||
| sameSite: "none", | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should normalize cookie prefix constraints before devtools restore", async () => { | ||
| const { page, session } = initDevtoolsSession_(); | ||
| const ordinaryCookie = { | ||
| name: "regular", | ||
| value: "regular-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/custom", | ||
| secure: false, | ||
| sameSite: "Lax", | ||
| }; | ||
|
|
||
| await session.restoreState({ | ||
| data: { | ||
| cookies: [ | ||
| { | ||
| name: "__Host-csrf-token", | ||
| value: "host-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/custom", | ||
| secure: false, | ||
| }, | ||
| { | ||
| name: "__Secure-session", | ||
| value: "secure-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/auth", | ||
| secure: false, | ||
| }, | ||
| ordinaryCookie, | ||
| ], | ||
| }, | ||
| cookies: true, | ||
| refresh: false, | ||
| }); | ||
|
|
||
| assert.calledOnce(page.setCookie); | ||
| assert.deepEqual(page.setCookie.firstCall.args, [ | ||
| { | ||
| name: "__Host-csrf-token", | ||
| value: "host-value", | ||
| path: "/", | ||
| secure: true, | ||
| }, | ||
| { | ||
| name: "__Secure-session", | ||
| value: "secure-value", | ||
| domain: "www.chromatic.com", | ||
| path: "/auth", | ||
| secure: true, | ||
| }, | ||
| ordinaryCookie, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should not ignore unrelated cookie restore errors", async () => { | ||
| const session = initWebdriverSession_(); | ||
| const error = new Error("unable to set cookie"); | ||
|
|
||
| session.setCookies.rejects(error); | ||
|
|
||
| await assert.isRejected( | ||
| session.restoreState({ | ||
| data: { | ||
| cookies: [ | ||
| { | ||
| name: "invalid-cookie", | ||
| value: "value", | ||
| }, | ||
| ], | ||
| }, | ||
| cookies: true, | ||
| refresh: false, | ||
| }), | ||
| /unable to set cookie/, | ||
| ); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a state file or
dataobject contains a__Host-cookie for a host other than the currently open page, deletingdomainleaves the cookie with neitherdomainnorurl. The DevTools path then lets Puppeteer fill the missing URL from the current page, and WebDriver defaults a missing domain to the current document, so the restore writes the host-only cookie to the wrong host instead of the saved one. This corrupts restored auth state for flows that restore saved cookies after navigating to a different origin; use the saved domain to build a URL or require navigating to that host instead of dropping it completely.Useful? React with 👍 / 👎.