| 1 |
import pytest |
| 2 |
from tests.bidi import recursive_compare |
| 3 |
from tests.support.helpers import get_origin_from_url |
| 4 |
from webdriver.bidi.modules.network import NetworkStringValue |
| 5 |
from webdriver.bidi.modules.storage import ( |
| 6 |
BrowsingContextPartitionDescriptor, |
| 7 |
PartialCookie, |
| 8 |
) |
| 9 |
|
| 10 |
pytestmark = pytest.mark.asyncio |
| 11 |
|
| 12 |
|
| 13 |
# This is a gecko-specific test because Firefox and Chrome define partitioning |
| 14 |
# on a browsing context differently. Firefox as a combination of an origin loaded |
| 15 |
# in this browsing context and a related user context, where Chrome defines it |
| 16 |
# as only a user context, which leads to different returned partition keys. |
| 17 |
async def test_partition_context( |
| 18 |
bidi_session, |
| 19 |
set_cookie, |
| 20 |
top_context, |
| 21 |
new_tab, |
| 22 |
test_page, |
| 23 |
test_page_cross_origin, |
| 24 |
domain_value, |
| 25 |
): |
| 26 |
await bidi_session.browsing_context.navigate( |
| 27 |
context=new_tab["context"], url=test_page, wait="complete" |
| 28 |
) |
| 29 |
source_origin_1 = get_origin_from_url(test_page) |
| 30 |
await bidi_session.browsing_context.navigate( |
| 31 |
context=top_context["context"], url=test_page_cross_origin, wait="complete" |
| 32 |
) |
| 33 |
source_origin_2 = get_origin_from_url(test_page_cross_origin) |
| 34 |
|
| 35 |
cookie_name = "foo" |
| 36 |
cookie_value = "bar" |
| 37 |
cookie_domain = domain_value() |
| 38 |
new_tab_partition = BrowsingContextPartitionDescriptor(new_tab["context"]) |
| 39 |
|
| 40 |
set_cookie_result = await set_cookie( |
| 41 |
cookie=PartialCookie( |
| 42 |
domain=cookie_domain, |
| 43 |
name=cookie_name, |
| 44 |
value=NetworkStringValue(cookie_value), |
| 45 |
), |
| 46 |
partition=new_tab_partition, |
| 47 |
) |
| 48 |
|
| 49 |
assert set_cookie_result == { |
| 50 |
"partitionKey": {"sourceOrigin": source_origin_1, "userContext": "default"} |
| 51 |
} |
| 52 |
|
| 53 |
# Check that added cookies are present on the right context. |
| 54 |
cookies = await bidi_session.storage.get_cookies(partition=new_tab_partition) |
| 55 |
|
| 56 |
recursive_compare( |
| 57 |
{ |
| 58 |
"cookies": [ |
| 59 |
{ |
| 60 |
"domain": cookie_domain, |
| 61 |
"httpOnly": False, |
| 62 |
"name": cookie_name, |
| 63 |
"path": "/", |
| 64 |
"sameSite": "none", |
| 65 |
"secure": False, |
| 66 |
"size": 6, |
| 67 |
"value": {"type": "string", "value": cookie_value}, |
| 68 |
} |
| 69 |
], |
| 70 |
"partitionKey": {"sourceOrigin": source_origin_1}, |
| 71 |
}, |
| 72 |
cookies, |
| 73 |
) |
| 74 |
|
| 75 |
# Check that added cookies are not present on the other context. |
| 76 |
cookies = await bidi_session.storage.get_cookies( |
| 77 |
partition=BrowsingContextPartitionDescriptor(top_context["context"]) |
| 78 |
) |
| 79 |
|
| 80 |
recursive_compare( |
| 81 |
{ |
| 82 |
"cookies": [], |
| 83 |
"partitionKey": {"sourceOrigin": source_origin_2, "userContext": "default"}, |
| 84 |
}, |
| 85 |
cookies, |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) |
| 90 |
async def test_partition_context_iframe( |
| 91 |
bidi_session, new_tab, inline, domain_value, domain, set_cookie |
| 92 |
): |
| 93 |
iframe_url = inline("<div id='in-iframe'>foo</div>", domain=domain) |
| 94 |
source_origin_for_iframe = get_origin_from_url(iframe_url) |
| 95 |
page_url = inline(f"<iframe src='{iframe_url}'></iframe>") |
| 96 |
source_origin_for_page = get_origin_from_url(page_url) |
| 97 |
await bidi_session.browsing_context.navigate( |
| 98 |
context=new_tab["context"], url=page_url, wait="complete" |
| 99 |
) |
| 100 |
|
| 101 |
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"]) |
| 102 |
iframe_context = contexts[0]["children"][0] |
| 103 |
iframe_partition = BrowsingContextPartitionDescriptor(iframe_context["context"]) |
| 104 |
|
| 105 |
cookie_name = "foo" |
| 106 |
cookie_value = "bar" |
| 107 |
await set_cookie( |
| 108 |
cookie=PartialCookie( |
| 109 |
domain=domain_value(domain), |
| 110 |
name=cookie_name, |
| 111 |
value=NetworkStringValue(cookie_value), |
| 112 |
), |
| 113 |
partition=iframe_partition, |
| 114 |
) |
| 115 |
|
| 116 |
# Check that added cookies are present on the right context |
| 117 |
cookies = await bidi_session.storage.get_cookies(partition=iframe_partition) |
| 118 |
|
| 119 |
expected_cookies = [ |
| 120 |
{ |
| 121 |
"domain": domain_value(domain=domain), |
| 122 |
"httpOnly": False, |
| 123 |
"name": cookie_name, |
| 124 |
"path": "/", |
| 125 |
"sameSite": "none", |
| 126 |
"secure": False, |
| 127 |
"size": 6, |
| 128 |
"value": {"type": "string", "value": cookie_value}, |
| 129 |
} |
| 130 |
] |
| 131 |
recursive_compare( |
| 132 |
{ |
| 133 |
"cookies": expected_cookies, |
| 134 |
"partitionKey": { |
| 135 |
"sourceOrigin": source_origin_for_iframe, |
| 136 |
"userContext": "default", |
| 137 |
}, |
| 138 |
}, |
| 139 |
cookies, |
| 140 |
) |
| 141 |
|
| 142 |
cookies = await bidi_session.storage.get_cookies( |
| 143 |
partition=BrowsingContextPartitionDescriptor(new_tab["context"]) |
| 144 |
) |
| 145 |
# When the iframe is on the different domain we can verify that top context has no iframe cookie. |
| 146 |
if domain == "alt": |
| 147 |
recursive_compare( |
| 148 |
{ |
| 149 |
"cookies": [], |
| 150 |
"partitionKey": { |
| 151 |
"sourceOrigin": source_origin_for_page, |
| 152 |
"userContext": "default", |
| 153 |
}, |
| 154 |
}, |
| 155 |
cookies, |
| 156 |
) |
| 157 |
else: |
| 158 |
# When the iframe is on the same domain, since the browsing context partition is defined by user context and origin, |
| 159 |
# which will be the same for the page, we get the same cookies as for the iframe |
| 160 |
recursive_compare( |
| 161 |
{ |
| 162 |
"cookies": expected_cookies, |
| 163 |
"partitionKey": { |
| 164 |
"sourceOrigin": source_origin_for_page, |
| 165 |
"userContext": "default", |
| 166 |
}, |
| 167 |
}, |
| 168 |
cookies, |
| 169 |
) |