mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* feat(labs): let listed browser origins call the local API A web page from an http(s) origin listed in Labs › Browser access to the local API can call Hex's authenticated loopback API with fetch. Its unauthenticated CORS preflight is answered 204 for that exact origin, and every response — 401, 404, and SSE streams included — carries that origin and Vary: Origin. Requests still need the bearer token; nothing allows credentials or *. Off, a missing Origin, or an origin not listed answers exactly as before. Origins save as scheme://host[:port] only: *, null, paths, queries, fragments, and userinfo are dropped, a trailing / and a default port are normalized away, and the scheme and host are lowercased. * fix(local_api): refuse CORS for a duplicate Origin without rejecting it A request with two Origin headers is routed exactly as before this lab existed (401 unauthenticated, 200 authenticated) but never earns CORS headers. The Labs origins field keeps exactly what was typed through its own debounced save, so a port or trailing comma is never rewritten under the caret. Docs name the pre-request errors that carry no CORS headers and how IPv6 and non-ASCII origins are written.
58 lines
2 KiB
Rust
58 lines
2 KiB
Rust
//! Browser access to the local API (Labs › Browser access to the local API):
|
||
//! the request origins this API answers cross-origin browser requests for.
|
||
//!
|
||
//! A browser sends an unauthenticated `OPTIONS` preflight before a page's
|
||
//! `fetch` can carry its bearer token, so a preflight for an allowed origin
|
||
//! is answered without authentication; every other request still needs the
|
||
//! token exactly as before, and its response (a `401` included) carries the
|
||
//! same `Access-Control-Allow-Origin`. Nothing here sends
|
||
//! `Access-Control-Allow-Credentials`, and `*` is never allowed.
|
||
|
||
use super::HttpRequest;
|
||
|
||
/// An allowed request origin, and whether the browser asked for
|
||
/// private-network access (a page on the public internet reaching this
|
||
/// loopback API).
|
||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||
pub(super) struct CorsOrigin {
|
||
pub(super) origin: String,
|
||
pub(super) private_network: bool,
|
||
}
|
||
|
||
/// How this local API learns its allowed browser origins, read for each
|
||
/// request so turning the lab off takes effect at once.
|
||
#[derive(Clone)]
|
||
pub(crate) struct Cors {
|
||
origins: fn() -> Vec<String>,
|
||
}
|
||
|
||
impl Default for Cors {
|
||
fn default() -> Self {
|
||
Self {
|
||
origins: crate::labs::api_browser_origins,
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Cors {
|
||
/// A gate under a test's control, so route tests need no global Labs
|
||
/// state.
|
||
#[cfg(test)]
|
||
pub(super) fn gated(origins: fn() -> Vec<String>) -> Self {
|
||
Self { origins }
|
||
}
|
||
|
||
/// The request's `Origin` when the lab allows it, with the
|
||
/// private-network request it carried. No lab, no `Origin`, or an origin
|
||
/// that isn't listed leaves the response exactly as it was.
|
||
pub(super) fn allowed(&self, request: &HttpRequest) -> Option<CorsOrigin> {
|
||
let origin = request.origin.as_deref()?;
|
||
(self.origins)()
|
||
.iter()
|
||
.any(|allowed| allowed == origin)
|
||
.then(|| CorsOrigin {
|
||
origin: origin.to_owned(),
|
||
private_network: request.private_network,
|
||
})
|
||
}
|
||
}
|