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

fix: always provide userContext in cookie's partitionKey #1938

Merged
merged 4 commits into from
Mar 5, 2024
Merged
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
36 changes: 21 additions & 15 deletions src/bidiMapper/domains/storage/StorageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class StorageProcessor {
const cdpResponse = await this.#browserCdpClient.sendCommand(
'Storage.getCookies',
{
browserContextId: partitionKey.userContext,
browserContextId: this.#getCdpBrowserContextId(partitionKey),
}
);

Expand All @@ -79,7 +79,7 @@ export class StorageProcessor {

await this.#browserCdpClient.sendCommand('Storage.setCookies', {
cookies: cdpCookiesToDelete,
browserContextId: partitionKey.userContext,
browserContextId: this.#getCdpBrowserContextId(partitionKey),
});
return {
partitionKey,
Expand All @@ -94,7 +94,7 @@ export class StorageProcessor {
const cdpResponse = await this.#browserCdpClient.sendCommand(
'Storage.getCookies',
{
browserContextId: partitionKey.userContext,
browserContextId: this.#getCdpBrowserContextId(partitionKey),
}
);

Expand Down Expand Up @@ -125,7 +125,7 @@ export class StorageProcessor {
try {
await this.#browserCdpClient.sendCommand('Storage.setCookies', {
cookies: [cdpCookie],
browserContextId: partitionKey.userContext,
browserContextId: this.#getCdpBrowserContextId(partitionKey),
});
} catch (e: any) {
this.#logger?.(LogType.debugError, e);
Expand All @@ -136,6 +136,14 @@ export class StorageProcessor {
};
}

#getCdpBrowserContextId(
partitionKey: Storage.PartitionKey
): string | undefined {
return partitionKey.userContext === 'default'
? undefined
: partitionKey.userContext;
}

#expandStoragePartitionSpecByBrowsingContext(
descriptor: Storage.BrowsingContextPartitionDescriptor
): Storage.PartitionKey {
Expand All @@ -147,10 +155,7 @@ export class StorageProcessor {
// storage partition it uses to persist data. In Chromium it's a `BrowserContext`
// which maps to BiDi `UserContext`.
return {
userContext:
browsingContext.userContext === 'default'
? undefined
: browsingContext.userContext,
userContext: browsingContext.userContext,
};
}

Expand All @@ -171,11 +176,6 @@ export class StorageProcessor {
}
}

const userContext =
descriptor.userContext === 'default' ? undefined : descriptor.userContext;

// Partition spec is a storage partition.
// Let partition key be partition spec.
for (const [key, value] of Object.entries(descriptor)) {
if (
key !== undefined &&
Expand All @@ -195,22 +195,28 @@ export class StorageProcessor {
);
}

// Set `userContext` to `default` if not provided, as it's required in Chromium.
const userContext = descriptor.userContext ?? 'default';

return {
userContext,
...(sourceOrigin === undefined ? {} : {sourceOrigin}),
...(userContext === undefined ? {} : {userContext}),
};
}

#expandStoragePartitionSpec(
partitionSpec: Storage.PartitionDescriptor | undefined
): Storage.PartitionKey {
if (partitionSpec === undefined) {
return {};
// `userContext` is required in Chromium.
return {userContext: 'default'};
}
if (partitionSpec.type === 'context') {
return this.#expandStoragePartitionSpecByBrowsingContext(partitionSpec);
}
assert(partitionSpec.type === 'storageKey', 'Unknown partition type');
// Partition spec is a storage partition.
// Let partition key be partition spec.
return this.#expandStoragePartitionSpecByStorageKey(partitionSpec);
}

Expand Down
24 changes: 8 additions & 16 deletions tests/storage/test_delete_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,19 @@ async def test_cookies_delete_params_empty(websocket, context_id):
partition=source_origin_partition)

# Delete all cookies.
res = await execute_command(websocket, {
resp = await execute_command(websocket, {
'method': 'storage.deleteCookies',
'params': {}
})
assert res == {
'partitionKey': {},
}
assert resp == {'partitionKey': {'userContext': 'default'}}

# Expect no cookies to be presented.
await assert_only_cookies_present(websocket, [])


@pytest.mark.asyncio
async def test_cookies_delete_partition_source_origin(websocket, context_id):
source_origin_partition = {
'type': 'storageKey',
'sourceOrigin': SOME_URL,
}
source_origin_partition = {'type': 'storageKey', 'sourceOrigin': SOME_URL}

not_partitioned_cookie = get_bidi_cookie(SOME_COOKIE_NAME,
SOME_COOKIE_VALUE, SOME_DOMAIN)
Expand All @@ -92,6 +87,7 @@ async def test_cookies_delete_partition_source_origin(websocket, context_id):
'partitionKey': {
# CDP's `partitionKey` does not support port.
'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT,
'userContext': 'default'
},
}

Expand Down Expand Up @@ -144,7 +140,7 @@ async def test_cookies_delete_partition_unsupported_key(websocket, context_id):
unknown_partition_key = 'UNKNOWN_PARTITION_KEY'
unknown_partition_value = 'UNKNOWN_PARTITION_VALUE'

res = await execute_command(
resp = await execute_command(
websocket, {
'method': 'storage.deleteCookies',
'params': {
Expand All @@ -154,9 +150,7 @@ async def test_cookies_delete_partition_unsupported_key(websocket, context_id):
},
}
})
assert res == {
'partitionKey': {},
}
assert resp == {'partitionKey': {'userContext': 'default'}}

# Expect no cookies to be presented.
await assert_only_cookies_present(websocket, [])
Expand All @@ -179,9 +173,7 @@ async def test_cookies_delete_partition_browsing_context(
}
})

assert resp == {
'partitionKey': {},
}
assert resp == {'partitionKey': {'userContext': 'default'}}

# Expect no cookies to be presented.
await assert_only_cookies_present(websocket, [])
Expand Down Expand Up @@ -252,7 +244,7 @@ async def test_cookies_delete_params_filter(websocket, context_id,
'filter': cookie_filter
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

await assert_only_cookies_present(websocket, [another_cookie])

Expand Down
17 changes: 13 additions & 4 deletions tests/storage/test_get_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ async def test_cookies_get_params_empty(websocket, context_id):

assert res == {
'cookies': [AnyExtending(cookie)],
'partitionKey': {},
'partitionKey': {
'userContext': 'default'
},
}


Expand All @@ -71,7 +73,9 @@ async def test_cookies_get_result_cdp_specific_fields(websocket, context_id):
'goog:sourceScheme': 'Secure',
}
],
'partitionKey': {},
'partitionKey': {
'userContext': 'default'
},
}


Expand Down Expand Up @@ -106,6 +110,7 @@ async def test_cookies_get_partition_source_origin(websocket, context_id):
'partitionKey': {
# CDP's `partitionKey` does not support port.
'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT,
'userContext': 'default'
},
}

Expand Down Expand Up @@ -165,7 +170,9 @@ async def test_cookies_get_partition_unsupported_key(websocket, context_id):

assert res == {
'cookies': [AnyExtending(cookie)],
'partitionKey': {},
'partitionKey': {
'userContext': 'default'
},
}


Expand All @@ -187,7 +194,9 @@ async def test_cookies_get_partition_browsing_context(websocket, context_id):

assert resp == {
'cookies': [AnyExtending(cookie)],
'partitionKey': {},
'partitionKey': {
'userContext': 'default'
},
}


Expand Down
56 changes: 40 additions & 16 deletions tests/storage/test_set_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def test_cookie_set_required_fields(websocket, context_id):
}
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -62,7 +62,9 @@ async def test_cookie_set_required_fields(websocket, context_id):
SOME_DOMAIN,
secure=False))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand All @@ -83,7 +85,7 @@ async def test_cookie_set_base64_value(websocket, context_id):
}
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -97,7 +99,9 @@ async def test_cookie_set_base64_value(websocket, context_id):
SOME_DOMAIN,
secure=False))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand All @@ -122,7 +126,7 @@ async def test_cookie_set_partition_browsing_context(websocket, context_id):
}
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -136,7 +140,9 @@ async def test_cookie_set_partition_browsing_context(websocket, context_id):
SOME_DOMAIN,
secure=True))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand All @@ -161,7 +167,7 @@ async def test_cookie_set_partition_user_context(websocket, context_id):
}
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -175,7 +181,9 @@ async def test_cookie_set_partition_user_context(websocket, context_id):
SOME_DOMAIN,
secure=True))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand Down Expand Up @@ -249,7 +257,12 @@ async def test_cookie_set_partition_source_origin(websocket, context_id):
}
}
})
assert resp == {'partitionKey': {'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT}}
assert resp == {
'partitionKey': {
'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT,
'userContext': 'default'
}
}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -263,7 +276,9 @@ async def test_cookie_set_partition_source_origin(websocket, context_id):
SOME_DOMAIN,
secure=True))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand Down Expand Up @@ -294,7 +309,7 @@ async def test_cookie_set_params_cookie_all_fields(websocket, context_id):
},
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -307,7 +322,9 @@ async def test_cookie_set_params_cookie_all_fields(websocket, context_id):
SOME_DOMAIN, some_path, http_only, secure,
same_site, expiry))
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}


Expand All @@ -324,13 +341,13 @@ async def test_cookie_set_params_cookie_expired(websocket, context_id):
expiry=expiry),
}
})
assert resp == {'partitionKey': {}}
assert resp == {'partitionKey': {'userContext': 'default'}}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
'params': {}
})
assert resp == {'cookies': [], 'partitionKey': {}}
assert resp == {'cookies': [], 'partitionKey': {'userContext': 'default'}}


@pytest.mark.asyncio
Expand Down Expand Up @@ -358,7 +375,12 @@ async def test_cookies_set_params_cookie_cdp_specific_fields(
}
}
})
assert resp == {'partitionKey': {'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT}}
assert resp == {
'partitionKey': {
'sourceOrigin': SOME_ORIGIN_WITHOUT_PORT,
'userContext': 'default',
}
}

resp = await execute_command(websocket, {
'method': 'storage.getCookies',
Expand All @@ -380,5 +402,7 @@ async def test_cookies_set_params_cookie_cdp_specific_fields(
'goog:sourceScheme': 'Secure',
}
],
'partitionKey': {}
'partitionKey': {
'userContext': 'default'
}
}
Loading
Loading