fix(remote): preserve pairing on failed forget and respect asset cache policy
This commit is contained in:
1 parent
49eb3a27f9
commit
fd9681ef8b
4 files changed
+56
-8
No files matched your search
@@ -400,15 +400,19 @@ class RemoteInstances:
|
||||
self._unknown_hosts.discard(key)
|
||||
return {"trusted": True}
|
||||
if action in {"disconnect", "remove"}:
|
||||
pair_id = profiles[key].pair_id
|
||||
if action == "remove":
|
||||
self._remember_origins(profiles)
|
||||
del profiles[key]
|
||||
# Commit the directory before destroying its credentials.
|
||||
# A failed save must leave the listed connection usable.
|
||||
self._write(profiles)
|
||||
connection = self.connections.pop(key, None)
|
||||
if connection:
|
||||
await connection.proxy.pause()
|
||||
if action == "remove":
|
||||
self._remember_origins(profiles)
|
||||
if profiles[key].pair_id:
|
||||
self.pairing.forget(profiles[key].pair_id)
|
||||
del profiles[key]
|
||||
self._write(profiles)
|
||||
if pair_id:
|
||||
self.pairing.forget(pair_id)
|
||||
self._known_hosts(key).unlink(missing_ok=True)
|
||||
self._unknown_hosts.discard(key)
|
||||
self._unverified.pop(key, None)
|
||||
|
||||
@@ -313,10 +313,11 @@ class RemoteProxy:
|
||||
and not request.rel_url.query and "set-cookie" not in upstream.headers
|
||||
and "public" in upstream_cache and "immutable" in upstream_cache
|
||||
and "no-store" not in upstream_cache and "private" not in upstream_cache
|
||||
and "no-cache" not in upstream_cache and "must-revalidate" not in upstream_cache
|
||||
and "application/json" not in upstream.headers.get("content-type", "")):
|
||||
# Only versioned public build assets, never HTML, API data,
|
||||
# credentials or media capabilities. Keep caches browser-local.
|
||||
outgoing["Cache-Control"] = "private, max-age=31536000, immutable"
|
||||
outgoing["Cache-Control"] = re.sub(r"\bpublic\b", "private", upstream_cache)
|
||||
if "application/json" in upstream.headers.get("content-type", ""):
|
||||
body = bytearray()
|
||||
async for chunk in upstream.aiter_bytes():
|
||||
|
||||
@@ -256,6 +256,34 @@ async def test_api_pair_finalization_pins_host_and_hides_credentials(tmp_path):
|
||||
assert manager.snapshot()["profiles"] == []
|
||||
|
||||
|
||||
async def test_failed_forget_preserves_saved_pair_for_restart_and_retry(tmp_path, monkeypatch):
|
||||
manager = RemoteInstances(tmp_path)
|
||||
request = read_request((await manager.action("pair_start", {}))["command"].split()[-1])
|
||||
value = receipt(request)
|
||||
await manager.action("pair_finish", {"id": request.id, "code": seal(request, value)})
|
||||
directory_before = manager.path.read_bytes()
|
||||
identity_path = manager.pairing.path(request.id) / "identity"
|
||||
identity_before = identity_path.read_bytes()
|
||||
|
||||
def fail_save(_profiles):
|
||||
raise OSError("disk full")
|
||||
|
||||
monkeypatch.setattr(manager, "_write", fail_save)
|
||||
with pytest.raises(OSError, match="disk full"):
|
||||
await manager.action("remove", {"id": request.id})
|
||||
|
||||
restarted = RemoteInstances(tmp_path)
|
||||
assert restarted.path.read_bytes() == directory_before
|
||||
assert restarted.snapshot()["profiles"][0]["id"] == request.id
|
||||
assert identity_path.read_bytes() == identity_before
|
||||
assert restarted.pairing.connection(request.id)["secret"] == value.secret
|
||||
assert restarted._known_hosts(request.id).exists()
|
||||
await restarted.action("rename", {"id": request.id, "name": "Still usable"})
|
||||
await restarted.action("remove", {"id": request.id})
|
||||
assert restarted.snapshot()["profiles"] == []
|
||||
assert not identity_path.exists()
|
||||
|
||||
|
||||
async def test_repeated_pairing_groups_one_instance_without_deleting_authorizations(tmp_path):
|
||||
manager = RemoteInstances(tmp_path)
|
||||
first = read_request((await manager.action("pair_start", {}))["command"].split()[-1])
|
||||
|
||||
@@ -39,7 +39,8 @@ async def remote(tmp_path, monkeypatch):
|
||||
runtime_surface="browser", runtime_capabilities_overrides=None,
|
||||
)
|
||||
seen, children = [], []
|
||||
state = SimpleNamespace(block=asyncio.Event(), entered=asyncio.Event(), ws_redirect=False)
|
||||
state = SimpleNamespace(block=asyncio.Event(), entered=asyncio.Event(), ws_redirect=False,
|
||||
asset_cache="public, max-age=31536000, immutable")
|
||||
|
||||
async def process(connection, request):
|
||||
seen.append(request)
|
||||
@@ -47,7 +48,7 @@ async def remote(tmp_path, monkeypatch):
|
||||
if path.startswith("/assets/"):
|
||||
response = connection.respond(200, "fixture asset")
|
||||
response.headers["Content-Type"] = "application/javascript"
|
||||
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
|
||||
response.headers["Cache-Control"] = state.asset_cache
|
||||
if "private" in request.path:
|
||||
response.headers["Cache-Control"] = "private, no-store"
|
||||
if "cookie" in request.path:
|
||||
@@ -140,6 +141,20 @@ async def test_cache_only_versioned_public_build_assets(remote, path, cacheable)
|
||||
assert "set-cookie" not in response.headers
|
||||
|
||||
|
||||
@pytest.mark.parametrize("directive", ["no-cache", 'no-cache="Content-Type"', "must-revalidate"])
|
||||
async def test_proxy_preserves_upstream_asset_revalidation(remote, directive):
|
||||
remote.state.asset_cache = f"public, max-age=0, immutable, {directive}"
|
||||
response = await remote.client.get(remote.proxy.origin + "/assets/index-AbcD1234.js")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["cache-control"] == "no-store"
|
||||
|
||||
|
||||
async def test_proxy_does_not_extend_upstream_asset_freshness(remote):
|
||||
remote.state.asset_cache = "public, max-age=60, immutable"
|
||||
response = await remote.client.get(remote.proxy.origin + "/assets/index-AbcD1234.js")
|
||||
assert response.headers["cache-control"] == "private, max-age=60, immutable"
|
||||
|
||||
|
||||
async def test_remote_credentials_never_leave_the_backend(remote):
|
||||
first, second = await asyncio.gather(bootstrap(remote), bootstrap(remote))
|
||||
assert first["token"] != second["token"]
|
||||
|
||||
Reference in new issue
Block a user