happy path working with stub companion app

This commit is contained in:
2026-08-18 02:16:05 +02:00
parent 3c11a88a33
commit bc025d007c
41 changed files with 1512 additions and 102 deletions
@@ -15,10 +15,27 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* calls with a 403 — Spring's CORS check doesn't know or care about
* host_permissions, it just saw an Origin not in the allowlist. The
* extension needed *no* CORS config at all (that's what host_permissions is
* for); only a real webpage does. So this stays scoped to exactly the one
* endpoint a webpage calls directly in this slice — not the whole API —
* specifically so it can never again regress an extension-only endpoint by
* accident the way the blanket {@code /**} mapping just did.
* for); only a real webpage does. So this stays scoped to exactly the
* endpoints a webpage calls directly — not the whole API — specifically so
* it can never again regress an extension-only endpoint by accident the
* way the blanket {@code /**} mapping just did.
*
* Two of the mappings below ({@code /run/relay/*}/confirm} and
* {@code /security/report}) are *shared*: the companion calls them
* directly (browser CORS applies), and the extension also calls them
* directly (browser CORS doesn't apply to it, but Spring's check still
* runs against its {@code chrome-extension://} Origin header regardless —
* same mechanism that caused the incident above). application.yml's
* {@code allowed-origins} default includes the dev extension's origin for
* exactly this reason; omitting it here would 403 the extension's own
* confirm/report calls the instant these two paths got a CORS mapping.
*
* {@code /run/relay/{runId}} (bare, no {@code /confirm} suffix) is
* deliberately not mapped here — it's handled by
* {@link dev.locqr.server.relay.RelayHttpRequestHandler} via a raw
* {@code SimpleUrlHandlerMapping} (see {@code RelayEndpointConfig}), which
* this class's {@code WebMvcConfigurer} hook does not reach. Its CORS
* config is set directly on that mapping instead.
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@@ -31,9 +48,31 @@ public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
String[] origins = properties.allowedOrigins().toArray(new String[0]);
registry.addMapping("/domain/registration")
.allowedOrigins(properties.allowedOrigins().toArray(new String[0]))
.allowedOrigins(origins)
.allowedMethods("GET")
.allowedHeaders("*");
// Companion only (mobile/claude.md's "Backend API surface") — the
// extension never calls this path, so no extension origin needed.
registry.addMapping("/run/bundle/*")
.allowedOrigins(origins)
.allowedMethods("GET")
.allowedHeaders("*");
// Shared: companion GETs to poll (interfaces.md, "PIN
// confirmation"), extension POSTs to signal (PinConfirmationController).
registry.addMapping("/run/relay/*/confirm")
.allowedOrigins(origins)
.allowedMethods("GET", "POST")
.allowedHeaders("*");
// Shared: both extension and companion report security-class errors.
registry.addMapping("/security/report")
.allowedOrigins(origins)
.allowedMethods("POST")
.allowedHeaders("*");
}
}
@@ -17,7 +17,7 @@ public record LocqrProperties(
Run run,
Relay relay
) {
public record Run(int qrTtlSeconds, int maxAutoRefresh, int pinTtlSeconds) {}
public record Run(int qrTtlSeconds, int maxAutoRefresh, int pinTtlSeconds, int relayWaitTtlSeconds) {}
public record Relay(int bufferTtlSeconds) {}
}
@@ -4,24 +4,60 @@ import dev.locqr.server.relay.RelayHttpRequestHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import java.util.List;
import java.util.Map;
/**
* Registers {@link RelayHttpRequestHandler} as the sole owner of
* {@code /run/relay/*} — see that class's Javadoc for why it isn't split
* across {@code @RestController} + {@code WebSocketConfigurer} instead.
*
* This bean sits outside the {@code RequestMappingHandlerMapping} that
* {@code CorsConfig}'s {@code WebMvcConfigurer.addCorsMappings()} wires
* into, so it gets its own CORS config directly rather than inheriting
* one.
*
* This path is shared after all, just not the way {@code CorsConfig}'s
* shared paths are: the companion POSTs here (kem_ciphertext, credential),
* and the extension's WebSocket connection to this same path (interfaces.md's
* "Relay messages") starts life as a plain HTTP GET with an {@code Upgrade:
* websocket} header — which goes through this exact
* {@code SimpleUrlHandlerMapping} and is subject to the CORS config below
* like any other request, before the upgrade ever happens. Missing this
* once already broke the extension outright: scoping {@code allowedMethods}
* to POST-only 403'd the extension's own GET/upgrade request (confirmed by
* hand — the extension never reconnects after that, silently stuck showing
* the QR forever since {@code relay.ts}'s {@code openRelay()} has no
* error handler). Both GET and POST need to be allowed, and both the
* companion's and the extension's origins need to be in the allow-list —
* the same lesson {@code CorsConfig}'s own history docs, just for a path
* outside that class's reach.
*/
@Configuration
public class RelayEndpointConfig {
private final LocqrProperties properties;
public RelayEndpointConfig(LocqrProperties properties) {
this.properties = properties;
}
@Bean
public HandlerMapping relayHandlerMapping(RelayHttpRequestHandler handler) {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
mapping.setUrlMap(Map.of("/run/relay/*", handler));
CorsConfiguration cors = new CorsConfiguration();
cors.setAllowedOrigins(properties.allowedOrigins());
cors.setAllowedMethods(List.of("GET", "POST"));
cors.setAllowedHeaders(List.of("*"));
mapping.setCorsConfigurations(Map.of("/run/relay/*", cors));
return mapping;
}
}
@@ -11,11 +11,10 @@ import org.springframework.web.bind.annotation.RestController;
/**
* PIN-confirmation handshake (interfaces.md, "PIN confirmation") — the
* extension posts here when its user clicks Confirm; the companion polls
* here after its own local pin_display gate before it's allowed to proceed
* to credential_select. Neither side trusts the other's timing without
* this: the companion no longer just proceeds on its own schedule
* (mobile/claude.md, updated).
* extension posts here the moment its user clicks Confirm; the companion
* polls here automatically from the instant it enters pin_display. The
* extension's click is the only human action gating advancement — the
* companion has no local confirmation step of its own (mobile/claude.md).
*/
@RestController
public class PinConfirmationController {
@@ -47,7 +47,8 @@ public class RunController {
request.runId(), request.origin(), request.x25519Pubkey(), request.kemPubkey(), expiresAt));
return new RunBundleUploadResponse(
signedToken, runParams.qrTtlSeconds(), runParams.maxAutoRefresh(), runParams.pinTtlSeconds());
signedToken, runParams.qrTtlSeconds(), runParams.maxAutoRefresh(), runParams.pinTtlSeconds(),
runParams.relayWaitTtlSeconds());
}
@GetMapping("/run/bundle/{runId}")
@@ -6,5 +6,6 @@ public record RunBundleUploadResponse(
@JsonProperty("signed_token") String signedToken,
@JsonProperty("qr_ttl") int qrTtl,
@JsonProperty("max_auto_refresh") int maxAutoRefresh,
@JsonProperty("pin_ttl") int pinTtl
@JsonProperty("pin_ttl") int pinTtl,
@JsonProperty("relay_wait_ttl") int relayWaitTtl
) {}
@@ -28,11 +28,12 @@ import java.util.concurrent.TimeUnit;
* discarded and the run must be restarted."
*
* <p>Also tracks the PIN-confirmation handshake (interfaces.md, "PIN
* confirmation"): the extension posts a confirmation here when its user
* clicks Confirm, and the companion polls for it before it's allowed to
* proceed past its own local pin_display gate. Same ephemeral-state
* lifecycle and TTL as the message buffer above, so it's tracked alongside
* it rather than in a separate component.
* confirmation"): the extension posts a confirmation here the moment its
* user clicks Confirm — the only human action that gates the run, the
* companion has no local confirmation step of its own — and the companion
* polls for it automatically from the moment it displays the PIN. Same
* ephemeral-state lifecycle and TTL as the message buffer above, so it's
* tracked alongside it rather than in a separate component.
*/
@Component
public class RelaySessionRegistry {
+10 -4
View File
@@ -16,13 +16,19 @@ locqr:
test-domain: ${TEST_DOMAIN:test.locqr.dev}
ed25519-secret-key-path: ${ED25519_SECRET_KEY_PATH:../dev/keys/ed25519-secret.b64}
registration-cert-path: ${REGISTRATION_CERT_PATH:../dev/certs/test-registration.b64}
# Webpage-context clients only (test website, later the companion web
# stub) — the extension's service worker bypasses CORS via manifest
# host_permissions instead. Ports match dev.md's documented defaults.
allowed-origins: ${ALLOWED_ORIGINS:https://test.locqr.dev:5173,https://test.locqr.dev:5174}
# Webpage-context clients (test website, companion web stub) plus the dev
# extension's own origin. The extension's host_permissions bypasses CORS
# enforcement on its *own* side, but not the server's — CorsConfig has to
# allow-list it too on any path the extension also calls directly (PIN
# confirmation, security reports), or those start getting 403'd the
# moment that path gets a CORS mapping for the companion. Ports match
# dev.md's documented defaults; extension ID matches dev.md's "Extension
# ID in development" (derived from extension/key.pem).
allowed-origins: ${ALLOWED_ORIGINS:https://test.locqr.dev:5173,https://test.locqr.dev:5174,chrome-extension://daijigbjegngkjckedcbhhdjkampjgia}
run:
qr-ttl-seconds: 90
max-auto-refresh: 3
pin-ttl-seconds: 120
relay-wait-ttl-seconds: 60
relay:
buffer-ttl-seconds: 8
@@ -92,6 +92,7 @@ class RunFlowIntegrationTest {
assertThat(body.qrTtl()).isEqualTo(90);
assertThat(body.maxAutoRefresh()).isEqualTo(3);
assertThat(body.pinTtl()).isEqualTo(120);
assertThat(body.relayWaitTtl()).isEqualTo(60);
assertThat(body.signedToken()).isNotBlank();
RunBundleFetchResponse firstFetch = client.get().uri("/run/bundle/{runId}", runId)
@@ -91,7 +91,7 @@ class SigningServiceTest {
keyFile.toString(),
"/nonexistent",
java.util.List.of(),
new LocqrProperties.Run(90, 3, 120),
new LocqrProperties.Run(90, 3, 120, 60),
new LocqrProperties.Relay(8));
}
}
@@ -130,7 +130,7 @@ class RelaySessionRegistryTest {
private RelaySessionRegistry registry(int bufferTtlSeconds) {
RelaySessionRegistry registry = new RelaySessionRegistry(new LocqrProperties(
"test.locqr.dev", "unused", "unused", java.util.List.of(),
new LocqrProperties.Run(90, 3, 120),
new LocqrProperties.Run(90, 3, 120, 60),
new LocqrProperties.Relay(bufferTtlSeconds)));
created.add(registry);
return registry;
@@ -47,7 +47,7 @@ class RegistrationCertificateStoreTest {
"unused",
certPath,
java.util.List.of(),
new LocqrProperties.Run(90, 3, 120),
new LocqrProperties.Run(90, 3, 120, 60),
new LocqrProperties.Relay(8));
}
}