Skip to content
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
15 changes: 10 additions & 5 deletions apps/auth-service/src/security/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,15 +1000,20 @@ impl CacheClass {
}
}

/// The `Cache-Control` value of this class.
/// The `Cache-Control` value of this class. Every value forbids transforming the
/// body: Cloudflare's edge would otherwise rewrite pages, hiding email addresses
/// behind a decoding script the Content-Security-Policy blocks.
fn cache_control(self) -> HeaderValue {
HeaderValue::from_static(match self {
Self::NoStore => "no-store",
Self::Immutable => "public, max-age=31536000, immutable",
Self::NoStore => "no-store, no-transform",
Self::Immutable => "public, max-age=31536000, immutable, no-transform",
Self::Public { max_age } => {
// Digits and fixed text always form a valid header value.
return HeaderValue::try_from(format!("public, max-age={}", max_age.as_secs()))
.unwrap_or(HeaderValue::from_static("no-store"));
return HeaderValue::try_from(format!(
"public, max-age={}, no-transform",
max_age.as_secs()
))
.unwrap_or(HeaderValue::from_static("no-store, no-transform"));
}
})
}
Expand Down
17 changes: 13 additions & 4 deletions apps/auth-service/src/security/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ impl SecurityFixture {
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.is_none()
);
assert_eq!(response.headers()[header::CACHE_CONTROL], "no-store");
assert_eq!(
response.headers()[header::CACHE_CONTROL],
"no-store, no-transform"
);
assert_eq!(
response.headers()[header::X_CONTENT_TYPE_OPTIONS],
"nosniff"
Expand Down Expand Up @@ -977,12 +980,15 @@ fn client() -> CorsGrant {
#[test]
fn cached_responses_carry_their_class() {
for (class, cache_control) in [
(CacheClass::Immutable, "public, max-age=31536000, immutable"),
(
CacheClass::Immutable,
"public, max-age=31536000, immutable, no-transform",
),
(
CacheClass::Public {
max_age: std::time::Duration::from_secs(3600),
},
"public, max-age=3600",
"public, max-age=3600, no-transform",
),
] {
let response = decorated(Cached(class, StatusCode::OK), None);
Expand All @@ -1008,7 +1014,10 @@ fn unclassed_errors_and_cookie_responses_are_not_stored() {
decorated(Cached(class, StatusCode::TOO_MANY_REQUESTS), None),
decorated([(header::CACHE_CONTROL, "public, max-age=60")], None),
] {
assert_eq!(response.headers()[header::CACHE_CONTROL], "no-store");
assert_eq!(
response.headers()[header::CACHE_CONTROL],
"no-store, no-transform"
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion apps/auth-service/src/web/developers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ async fn assert_missing(response: Response) {
/// redirect, no cookie, and no copy in any cache.
fn assert_unstored(response: &Response) {
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers()[header::CACHE_CONTROL], "no-store");
assert_eq!(
response.headers()[header::CACHE_CONTROL],
"no-store, no-transform"
);
assert!(response.headers().get(header::LOCATION).is_none());
assert!(response.headers().get(header::SET_COOKIE).is_none());
}
Expand Down
10 changes: 2 additions & 8 deletions apps/auth-service/src/web/settings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ async fn confirmed_changes_ask_for_a_passkey_first() {
let added = confirmed.post(&replicas, settings::EMAILS, &add).await;
assert_eq!(cookie(&added, BrowserCookie::Notice).0, "email_added");
let page = html(confirmed.visit(&replicas.a, settings::EMAILS).await).await;
assert!(
page.contains(&format!("<strong>{address}</strong>")),
"{page}"
);
assert!(page.contains(&format!("<span>{address}</span>")), "{page}");
assert!(page.contains("Waiting for confirmation"), "{page}");

let id = value_after(&page, r#"name="remove" value=""#)
Expand Down Expand Up @@ -235,10 +232,7 @@ async fn addresses_change_their_primary_and_are_removed() {
assert_eq!(cookie(&removed, BrowserCookie::Notice).0, "email_removed");
let page = html(browser.visit(&replicas.a, settings::EMAILS).await).await;
assert!(!page.contains(&first), "{page}");
assert!(
page.contains(&format!("<strong>{second}</strong>")),
"{page}"
);
assert!(page.contains(&format!("<span>{second}</span>")), "{page}");
assert!(
page.contains("Primary: receives security notices"),
"{page}"
Expand Down
26 changes: 26 additions & 0 deletions crates/ui/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ tbody tr:last-child td {
margin-top: 2rem;
}
}
/* An email address, blurred until its visually hidden checkbox is checked. */
.private-address {
display: inline;
position: relative;
font: inherit;
cursor: pointer;
}
.private-address input {
position: absolute;
width: 1px;
height: 1px;
min-height: 0;
padding: 0;
opacity: 0;
}
.private-address span {
filter: blur(5px);
transition: filter 0.15s;
}
.private-address input:checked + span {
filter: none;
}
.private-address:has(input:focus-visible) span {
outline: 3px solid #ab7900;
outline-offset: 4px;
}
/* Author display rules would otherwise show what a page or its script hides. */
[hidden] {
display: none !important;
Expand Down
7 changes: 5 additions & 2 deletions crates/ui/src/admin/users.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Account search and one account's page.

use super::{EventRow, account_status, event_rows, heading, moment, refused_action};
use crate::layout::private_address;
use piquel_common::{
AccountStatus, AuthenticationMethod,
admin::{Session, User, UserDetail},
Expand Down Expand Up @@ -237,7 +238,7 @@ async fn credentials<'a>(detail: &'a UserDetail) -> Result<impl View> {
<ul class="list">
for email in &detail.emails {
<li class="row between">
<span>(email.address.as_str())</span>
private_address(address: email.address.as_str())
<span class="row">
if email.primary { <span class="badge">"Primary"</span> }
match email.verified_at {
Expand Down Expand Up @@ -272,7 +273,9 @@ async fn credentials<'a>(detail: &'a UserDetail) -> Result<impl View> {
for identity in &detail.identities {
<li class="row between">
<span>(identity.provider.to_string())</span>
<small>(identity.linked_email.clone().unwrap_or_default())</small>
if let Some(email) = &identity.linked_email {
<small>private_address(address: email.as_str())</small>
}
</li>
}
</ul>
Expand Down
13 changes: 13 additions & 0 deletions crates/ui/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ pub async fn intro<'a>(
})
}

/// An email `address`, blurred until clicked so a shared screen does not show it;
/// clicking again blurs it. A checkbox, visually hidden, holds whether it shows:
/// the admin dashboard allows no script.
#[component]
pub(crate) async fn private_address<'a>(address: &'a str) -> Result<impl View> {
Ok(view! {
<label class="private-address">
<input type="checkbox" aria-label="Show address">
<span>(address)</span>
</label>
})
}

/// The navigation named `label`, of `class`, linking each of `links` to its URL,
/// `current` among them, whose link is marked [`Label::CURRENT`].
#[component]
Expand Down
4 changes: 2 additions & 2 deletions crates/ui/src/settings/emails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Audience;
use crate::{
Section, Title,
form::{Query, change_form, fragment_form, open},
layout::frame,
layout::{frame, private_address},
};
use piquel_common::EmailId;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -153,7 +153,7 @@ async fn address_row<'a>(
Ok(view! {
<div class="row between">
<div>
<strong>(address)</strong>
<strong>private_address(address: address)</strong>
<p class="help">
(if blocked {
"Primary: receives security notices. Make another address primary to remove it."
Expand Down
10 changes: 6 additions & 4 deletions infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ the gateway ranges in `PIQUELD_INGRESS_PROXIES` to trust ingress's overwritten
Auth decides every response's `Cache-Control` itself; the edge needs no rule.
Handlers mark a response cacheable with `Cached` (`apps/auth-service/src/security`),
and anything else, including any error and any response that sets a cookie, is
`no-store`:
`no-store`. Every value also carries `no-transform`, so Cloudflare never rewrites
a body: its Email Address Obfuscation would replace addresses with a link to a
decoding script the pages' Content-Security-Policy blocks.

| Class | `Cache-Control` | Responses |
| --- | --- | --- |
| `NoStore` | `no-store` | APIs, OAuth, server-rendered pages, errors, everything not below |
| `Immutable` | `public, max-age=31536000, immutable` | the pages' content-hashed stylesheet and script, under `/assets/` |
| `Public` | `public, max-age=N` | discovery and JWKS, for an hour |
| `NoStore` | `no-store, no-transform` | APIs, OAuth, server-rendered pages, errors, everything not below |
| `Immutable` | `public, max-age=31536000, immutable, no-transform` | the pages' content-hashed stylesheet and script, under `/assets/` |
| `Public` | `public, max-age=N, no-transform` | discovery and JWKS, for an hour |

Every page is server-rendered and `no-store`, so a deploy never leaves a cached page
pointing at a stylesheet or script the new binary no longer serves. The Cloudflare Tunnel
Expand Down
2 changes: 1 addition & 1 deletion tests/browser/developers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('the developer console registers an app, shows its secret once, and the app
await page.getByRole('button', { name: 'Register app', exact: true }).click();
const answer = await registered;
expect(answer.status()).toBe(200);
expect(answer.headers()['cache-control']).toBe('no-store');
expect(answer.headers()['cache-control']).toBe('no-store, no-transform');
await expect(page.getByRole('heading', { name: 'Your client secret' })).toBeVisible();
const secret = (await page.locator('#client-secret').textContent()) ?? '';
expect(secret).toMatch(/^pqcs_/);
Expand Down
Loading