Agent Wranglers

APS-19Required

Clear cached pages and data when someone signs out

On sign-out, delete everything the browser stored for the account. That covers the service worker's caches, local storage and cookies.

How to do it

A service worker that cached a signed-in page shows it to the next person on a shared device, even after sign-out. Send Clear-Site-Data: "cache", "cookies", "storage" on the sign-out answer. Browsers that support it then drop caches, storage and service worker registrations for the site. Also delete the caches from the page with caches.delete before the redirect, for browsers that ignore the header. End the session on the server too, as Let people sign out of one device or all of them says. Better still, never cache private answers in the worker, as Keep working when the network drops says.

Examples

JavaScript example

app.post('/sign-out', (req, res) => {
  req.session.destroy(() => {
    res.set('Clear-Site-Data', '"cache", "cookies", "storage"').redirect(303, '/');
  });
});

Python example

@app.post("/sign-out")
def sign_out(request: Request):
    request.session.clear()
    response = RedirectResponse("/", status_code=303)
    response.headers["Clear-Site-Data"] = '"cache", "cookies", "storage"'
    return response

How to check

A browser test signs in, opens the account page, and signs out. Afterwards caches.keys() returns an empty list, local storage is empty, and the account page is not served while offline.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/application-security/authentication-and-sessions/clear-site-data-on-sign-out">Agent Wranglers application security playbook</a>. In code you write, add a comment next to it with the same link.