Skip to content

Commit

Permalink
chore: improve types for server automation cookie client (#25836)
Browse files Browse the repository at this point in the history
* chore: improve types for automation cookies

* [run ci]
  • Loading branch information
AtofStryker authored Feb 20, 2023
1 parent 147c8f5 commit 59c1175
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 74 deletions.
6 changes: 3 additions & 3 deletions packages/driver/src/cross-origin/events/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AutomationCookie } from '@packages/server/lib/automation/cookies'
import type { SerializableAutomationCookie } from '@packages/server/lib/util/cookies'
import type { ICypress } from '../../cypress'

// cross-origin cookies collected by the the proxy are sent down to the driver
Expand All @@ -10,10 +10,10 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
// multiple requests could set cookies while the page is loading, so we
// collect all cookies and only send set them via automation once after
// the page has loaded
let cookiesToSend: AutomationCookie[] = []
let cookiesToSend: SerializableAutomationCookie[] = []
let waitingToSend = false

Cypress.on('cross:origin:cookies', (cookies: AutomationCookie[]) => {
Cypress.on('cross:origin:cookies', (cookies: SerializableAutomationCookie[]) => {
cookiesToSend = cookiesToSend.concat(cookies)

Cypress.backend('cross:origin:cookies:received')
Expand Down
4 changes: 2 additions & 2 deletions packages/driver/types/internal-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ declare namespace Cypress {
}

interface Actions {
(action: 'set:cookie', fn: (cookie: AutomationCookie) => void)
(action: 'set:cookie', fn: (cookie: SerializableAutomationCookie) => void)
(action: 'clear:cookie', fn: (name: string) => void)
(action: 'clear:cookies', fn: () => void)
(action: 'cross:origin:cookies', fn: (cookies: AutomationCookie[]) => void)
(action: 'cross:origin:cookies', fn: (cookies: SerializableAutomationCookie[]) => void)
(action: 'before:stability:release', fn: () => void)
(action: 'paused', fn: (nextCommandName: string) => void)
}
Expand Down
5 changes: 2 additions & 3 deletions packages/proxy/lib/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import RequestMiddleware from './request-middleware'
import ResponseMiddleware from './response-middleware'
import { DeferredSourceMapCache } from '@packages/rewriter'
import type { RemoteStates } from '@packages/server/lib/remote_states'
import type { CookieJar } from '@packages/server/lib/util/cookies'
import type { CookieJar, SerializableAutomationCookie } from '@packages/server/lib/util/cookies'
import type { RequestedWithAndCredentialManager } from '@packages/server/lib/util/requestedWithAndCredentialManager'
import type { AutomationCookie } from '@packages/server/lib/automation/cookies'
import { errorUtils } from '@packages/errors'

function getRandomColorFn () {
Expand Down Expand Up @@ -59,7 +58,7 @@ type HttpMiddlewareCtx<T> = {
getPreRequest: (cb: GetPreRequestCb) => void
getAUTUrl: Http['getAUTUrl']
setAUTUrl: Http['setAUTUrl']
simulatedCookies: AutomationCookie[]
simulatedCookies: SerializableAutomationCookie[]
} & T

export const defaultMiddleware = {
Expand Down
4 changes: 2 additions & 2 deletions packages/proxy/lib/http/util/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type Debug from 'debug'
import { URL } from 'url'
import { cors } from '@packages/network'
import { urlOriginsMatch, urlSameSiteMatch } from '@packages/network/lib/cors'
import { AutomationCookie, Cookie, CookieJar, toughCookieToAutomationCookie } from '@packages/server/lib/util/cookies'
import { SerializableAutomationCookie, Cookie, CookieJar, toughCookieToAutomationCookie } from '@packages/server/lib/util/cookies'
import type { RequestCredentialLevel, RequestedWithHeader } from '../../types'

type SiteContext = 'same-origin' | 'same-site' | 'cross-site'
Expand Down Expand Up @@ -197,7 +197,7 @@ export class CookiesHelper {

const afterCookies = this.cookieJar.getAllCookies()

return afterCookies.reduce<AutomationCookie[]>((memo, afterCookie) => {
return afterCookies.reduce<SerializableAutomationCookie[]>((memo, afterCookie) => {
if (matchesPreviousCookie(this.previousCookies, afterCookie)) return memo

return memo.concat(toughCookieToAutomationCookie(afterCookie, this.defaultDomain))
Expand Down
4 changes: 2 additions & 2 deletions packages/proxy/lib/http/util/inject.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { oneLine } from 'common-tags'
import { getRunnerInjectionContents, getRunnerCrossOriginInjectionContents } from '@packages/resolve-dist'
import type { AutomationCookie } from '@packages/server/lib/automation/cookies'
import type { SerializableAutomationCookie } from '@packages/server/lib/util/cookies'

interface InjectionOpts {
shouldInjectDocumentDomain: boolean
}
interface FullCrossOriginOpts {
modifyObstructiveThirdPartyCode: boolean
modifyObstructiveCode: boolean
simulatedCookies: AutomationCookie[]
simulatedCookies: SerializableAutomationCookie[]
}

export function partial (domain, options: InjectionOpts) {
Expand Down
4 changes: 2 additions & 2 deletions packages/proxy/lib/http/util/rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as inject from './inject'
import * as astRewriter from './ast-rewriter'
import * as regexRewriter from './regex-rewriter'
import type { CypressWantsInjection } from '../../types'
import type { AutomationCookie } from '@packages/server/lib/automation/cookies'
import type { SerializableAutomationCookie } from '@packages/server/lib/util/cookies'

export type SecurityOpts = {
isNotJavascript?: boolean
Expand All @@ -17,7 +17,7 @@ export type InjectionOpts = {
domainName: string
wantsInjection: CypressWantsInjection
wantsSecurityRemoved: any
simulatedCookies: AutomationCookie[]
simulatedCookies: SerializableAutomationCookie[]
shouldInjectDocumentDomain: boolean
}

Expand Down
12 changes: 6 additions & 6 deletions packages/runner/injection/patches/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import {
CookieJar,
toughCookieToAutomationCookie,
automationCookieToToughCookie,
SerializableAutomationCookie,
} from '@packages/server/lib/util/cookies'
import { Cookie as ToughCookie } from 'tough-cookie'
import type { AutomationCookie } from '@packages/server/lib/automation/cookies'

function isHostOnlyCookie (domain) {
return domain[0] !== '.'
}

const parseDocumentCookieString = (documentCookieString: string): AutomationCookie[] => {
const parseDocumentCookieString = (documentCookieString: string): SerializableAutomationCookie[] => {
if (!documentCookieString || !documentCookieString.trim().length) return []

return documentCookieString.split(';').map((cookieString) => {
Expand All @@ -33,7 +33,7 @@ const parseDocumentCookieString = (documentCookieString: string): AutomationCook
})
}

const sendCookieToServer = (cookie: AutomationCookie) => {
const sendCookieToServer = (cookie: SerializableAutomationCookie) => {
window.top!.postMessage({
event: 'cross:origin:aut:set:cookie',
data: {
Expand All @@ -52,7 +52,7 @@ const sendCookieToServer = (cookie: AutomationCookie) => {
// document.cookie runs into cross-origin restrictions when the AUT is on
// a different origin than top. The goal is to make it act like it would
// if the user's app was run in top.
export const patchDocumentCookie = (requestCookies: AutomationCookie[]) => {
export const patchDocumentCookie = (requestCookies: SerializableAutomationCookie[]) => {
const url = location.href
const domain = location.hostname
const cookieJar = new CookieJar()
Expand All @@ -64,7 +64,7 @@ export const patchDocumentCookie = (requestCookies: AutomationCookie[]) => {
}).join('; ')
}

const addCookies = (cookies: AutomationCookie[]) => {
const addCookies = (cookies: SerializableAutomationCookie[]) => {
cookies.forEach((cookie) => {
cookieJar.setCookie(automationCookieToToughCookie(cookie, domain), url, undefined)
})
Expand Down Expand Up @@ -154,7 +154,7 @@ export const patchDocumentCookie = (requestCookies: AutomationCookie[]) => {

// the following listeners are called from Cypress cookie commands, so that
// the document.cookie value is updated optimistically
Cypress.on('set:cookie', (cookie: AutomationCookie) => {
Cypress.on('set:cookie', (cookie: SerializableAutomationCookie) => {
setCookie(automationCookieToToughCookie(cookie, domain))
})

Expand Down
8 changes: 4 additions & 4 deletions packages/server/lib/automation/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class Automation {
this.middleware = this.initializeMiddleware()
}

automationValve (message, fn) {
return (msg, data) => {
automationValve (message: string, fn: (...args: any) => any) {
return (msg: string, data: any) => {
// enable us to omit message
// argument
if (!data) {
Expand All @@ -60,7 +60,7 @@ export class Automation {
}
}

requestAutomationResponse (message, data, fn) {
requestAutomationResponse (message: string, data: any, fn: (...args: any) => any) {
return new Bluebird((resolve, reject) => {
const id = uuidv4()

Expand Down Expand Up @@ -97,7 +97,7 @@ export class Automation {
})
}

normalize (message, data, automate?) {
normalize (message: string, data: any, automate?) {
return Bluebird.try(() => {
switch (message) {
case 'take:screenshot':
Expand Down
Loading

3 comments on commit 59c1175

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 59c1175 Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.7.0/linux-arm64/develop-59c117506588aa918037e584b439606ac969efa8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 59c1175 Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.7.0/linux-x64/develop-59c117506588aa918037e584b439606ac969efa8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 59c1175 Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.7.0/darwin-x64/develop-59c117506588aa918037e584b439606ac969efa8/cypress.tgz

Please sign in to comment.