mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-03 08:10:27 +00:00
* fix: clarify session cookie, token scope and MCP selection refusals Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: clarify unresolved references, app, job and integration refusal messages Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): clarify skipped install scripts and add --run-install-scripts to app bundle and dev Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: clarify app isolation, approval, MCP redirect and embedder messages Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: clarify why CUSTOM_INSTANCE_DB is refused as an app input or resource reference Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: bump ee-repo-ref Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: run install scripts on existing node_modules with --run-install-scripts, generic path scope hint Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: match the reworded resource and variable access refusals Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: bump ee-repo-ref Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
365 lines
14 KiB
Rust
365 lines
14 KiB
Rust
/*
|
|
* Author: Ruben Fiszel
|
|
* Copyright: Windmill Labs, Inc 2026
|
|
* This file and its contents are licensed under the AGPLv3 License.
|
|
* Please see the included NOTICE for copyright information and
|
|
* LICENSE-AGPL for a copy of the license.
|
|
*/
|
|
|
|
//! Whether a request was sent by a page of this instance's own origin.
|
|
//!
|
|
//! The session cookie is `SameSite=Lax`, which a browser attaches to every request from a
|
|
//! *same-site* page: any sibling host under the same registrable domain (every
|
|
//! `*.windmill.dev` host for a `<name>.windmill.dev` instance, every `*.corp.com` host for
|
|
//! `windmill.corp.com`), forms and top-level GETs alike. The cookie is therefore only treated
|
|
//! as the caller's intent on requests from the instance's own origin, a bookmark or a typed
|
|
//! URL, or a non-browser client.
|
|
|
|
use http::{header, HeaderMap, Uri};
|
|
use url::Url;
|
|
|
|
/// Hosts, besides the instance's own, whose pages may send requests that authenticate with
|
|
/// the session cookie. `*` accepts every site.
|
|
pub const TRUSTED_ORIGINS_ENV: &str = "SESSION_COOKIE_TRUSTED_ORIGINS";
|
|
|
|
lazy_static::lazy_static! {
|
|
static ref TRUSTED_ORIGINS: TrustedOrigins = TrustedOrigins::parse(
|
|
&std::env::var(TRUSTED_ORIGINS_ENV).unwrap_or_default()
|
|
);
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct TrustedOrigins {
|
|
any: bool,
|
|
hosts: Vec<String>,
|
|
}
|
|
|
|
impl TrustedOrigins {
|
|
/// A comma-separated list of hosts or origins (`dash.corp.com`, `https://dash.corp.com`).
|
|
pub fn parse(value: &str) -> Self {
|
|
let mut trusted = TrustedOrigins::default();
|
|
for entry in value.split(',').map(str::trim).filter(|e| !e.is_empty()) {
|
|
if entry == "*" {
|
|
trusted.any = true;
|
|
} else if let Some(host) = Url::parse(entry)
|
|
.ok()
|
|
.and_then(|u| u.host_str().map(str::to_owned))
|
|
.or_else(|| host_of(entry))
|
|
{
|
|
trusted.hosts.push(host);
|
|
} else {
|
|
tracing::warn!("{TRUSTED_ORIGINS_ENV}: ignoring {entry:?}, not a host or origin");
|
|
}
|
|
}
|
|
trusted
|
|
}
|
|
|
|
fn allows(&self, host: &str) -> bool {
|
|
self.hosts.iter().any(|h| h.eq_ignore_ascii_case(host))
|
|
}
|
|
}
|
|
|
|
/// Why a request is taken to come from a page of another origin.
|
|
#[derive(Debug)]
|
|
pub enum Foreign {
|
|
/// `Sec-Fetch-Site` says so (`same-site`, `cross-site`, or a value this code does not know).
|
|
Declared(String),
|
|
/// No `Sec-Fetch-Site`; `Origin` names another host, or an opaque origin (`null`).
|
|
Origin { origin: String, instance_hosts: Vec<String> },
|
|
/// No `Sec-Fetch-Site` and no `Origin`; `Referer` names another host.
|
|
Referer { referer: String, instance_hosts: Vec<String> },
|
|
}
|
|
|
|
impl Foreign {
|
|
/// Logged when a request is refused. The `Origin`/`Referer` legs compare against the hosts
|
|
/// the backend can see, so a proxy that rewrites `Host` without setting
|
|
/// `X-Forwarded-Host`, with `BASE_URL` naming another host, refuses requests that were
|
|
/// same-origin: say so, so it reads as a misconfiguration.
|
|
pub fn log(&self, what: &str) {
|
|
match self {
|
|
Foreign::Declared(site) => {
|
|
tracing::info!(sec_fetch_site = %site, "{what}: the request comes from another site")
|
|
}
|
|
Foreign::Origin { origin: page, instance_hosts }
|
|
| Foreign::Referer { referer: page, instance_hosts } => tracing::warn!(
|
|
page = %page,
|
|
?instance_hosts,
|
|
"{what}: the page that sent the request is on another host; if it was this \
|
|
instance, set `X-Forwarded-Host` on the proxy or configure `BASE_URL`"
|
|
),
|
|
}
|
|
}
|
|
|
|
/// What the caller is told when the session cookie of this request is set aside. The
|
|
/// proxy hint is left out when the browser itself declared the page another site.
|
|
pub fn cookie_refusal_message(&self, headers: &HeaderMap) -> String {
|
|
let (page, proxy_hint) = match self {
|
|
Foreign::Origin { origin: page, .. } | Foreign::Referer { referer: page, .. } => {
|
|
(Some(page.clone()), true)
|
|
}
|
|
Foreign::Declared(_) => (
|
|
headers
|
|
.get(header::ORIGIN)
|
|
.map(|o| String::from_utf8_lossy(o.as_bytes()).into_owned())
|
|
.or_else(|| header_url_host(headers, header::REFERER)),
|
|
false,
|
|
),
|
|
};
|
|
let from = page.map(|p| format!(" ({p})")).unwrap_or_default();
|
|
let hint = if proxy_hint {
|
|
" If that page is this instance, set BASE_URL to the address you browse or have the \
|
|
proxy send X-Forwarded-Host."
|
|
} else {
|
|
""
|
|
};
|
|
format!(
|
|
"This request came from a page on another host{from}; the session cookie is only \
|
|
accepted from this instance's own pages.{hint} {TRUSTED_ORIGINS_HINT} From scripts, \
|
|
send a token in the Authorization header."
|
|
)
|
|
}
|
|
}
|
|
|
|
/// How to let pages of another site use the session cookie.
|
|
pub const TRUSTED_ORIGINS_HINT: &str =
|
|
"To allow another site, add it to SESSION_COOKIE_TRUSTED_ORIGINS (comma-separated).";
|
|
|
|
/// [`foreign_signal`] against the configured `BASE_URL` and trusted origins.
|
|
pub fn foreign_request(headers: &HeaderMap, uri: &Uri) -> Option<Foreign> {
|
|
foreign_signal(
|
|
headers,
|
|
uri,
|
|
windmill_common::BASE_URL.load().as_str(),
|
|
&TRUSTED_ORIGINS,
|
|
)
|
|
}
|
|
|
|
/// `Sec-Fetch-Site` decides when present: only `same-origin` and `none` (a bookmark, a typed
|
|
/// URL) are this instance's own. Fetch Metadata rides only on potentially trustworthy URLs, so
|
|
/// a plain-http instance (and Safari before 16.4) never gets it; then `Origin`, which a
|
|
/// browser sends on every non-GET request, must name an instance host, and failing that
|
|
/// `Referer`, the only thing a top-level GET navigation carries. A request with none of them
|
|
/// is not from a browser page and is not refused.
|
|
///
|
|
/// The `Referer` leg is weak: `strict-origin-when-cross-origin` drops it on an https-to-http
|
|
/// downgrade, so an https page pointing at a plain-http instance sends neither header there.
|
|
pub fn foreign_signal(
|
|
headers: &HeaderMap,
|
|
uri: &Uri,
|
|
base_url: &str,
|
|
trusted: &TrustedOrigins,
|
|
) -> Option<Foreign> {
|
|
if trusted.any {
|
|
return None;
|
|
}
|
|
let page_host = || {
|
|
header_url_host(headers, header::ORIGIN)
|
|
.or_else(|| header_url_host(headers, header::REFERER))
|
|
};
|
|
if let Some(site) = headers.get("sec-fetch-site") {
|
|
let site = String::from_utf8_lossy(site.as_bytes()).to_ascii_lowercase();
|
|
if site == "same-origin" || site == "none" {
|
|
return None;
|
|
}
|
|
if !trusted.hosts.is_empty() && page_host().is_some_and(|h| trusted.allows(&h)) {
|
|
return None;
|
|
}
|
|
return Some(Foreign::Declared(site));
|
|
}
|
|
let is_ours = |host: &str| {
|
|
trusted.allows(host)
|
|
|| instance_hosts(headers, uri, base_url).any(|h| h.eq_ignore_ascii_case(host))
|
|
};
|
|
if let Some(origin) = headers.get(header::ORIGIN) {
|
|
if header_url_host(headers, header::ORIGIN).is_some_and(|h| is_ours(&h)) {
|
|
return None;
|
|
}
|
|
return Some(Foreign::Origin {
|
|
origin: String::from_utf8_lossy(origin.as_bytes()).into_owned(),
|
|
instance_hosts: instance_hosts(headers, uri, base_url).collect(),
|
|
});
|
|
}
|
|
let referer = header_url_host(headers, header::REFERER)?;
|
|
(!is_ours(&referer)).then(|| Foreign::Referer {
|
|
referer,
|
|
instance_hosts: instance_hosts(headers, uri, base_url).collect(),
|
|
})
|
|
}
|
|
|
|
/// Every host a same-origin request can name. `Host` alone is not enough: a reverse proxy that
|
|
/// forwards without preserving it (nginx `proxy_pass` with no `proxy_set_header Host $host`)
|
|
/// hands the backend the upstream's name. A browser page cannot set any of these on a request
|
|
/// that carries the cookie (a custom header needs a CORS preflight no route grants with
|
|
/// credentials), and `BASE_URL` is instance config. Ports are not compared: the dev frontend
|
|
/// and the API differ only by port.
|
|
fn instance_hosts<'a>(
|
|
headers: &'a HeaderMap,
|
|
uri: &'a Uri,
|
|
base_url: &'a str,
|
|
) -> impl Iterator<Item = String> + 'a {
|
|
[
|
|
uri.host()
|
|
.map(str::to_owned)
|
|
.or_else(|| header_host(headers, header::HOST)),
|
|
header_host(headers, "x-forwarded-host"),
|
|
Url::parse(base_url)
|
|
.ok()
|
|
.and_then(|url| url.host_str().map(str::to_owned)),
|
|
]
|
|
.into_iter()
|
|
.flatten()
|
|
}
|
|
|
|
fn header_url_host(headers: &HeaderMap, name: header::HeaderName) -> Option<String> {
|
|
let value = headers.get(name)?.to_str().ok()?;
|
|
Url::parse(value).ok()?.host_str().map(str::to_owned)
|
|
}
|
|
|
|
fn header_host(headers: &HeaderMap, name: impl header::AsHeaderName) -> Option<String> {
|
|
host_of(headers.get(name)?.to_str().ok()?)
|
|
}
|
|
|
|
/// The host in a `Host`-shaped header value: `host[:port]`, where `host` may be a bracketed
|
|
/// IPv6 literal, and where a chain of proxies appends to `X-Forwarded-Host` so only the first
|
|
/// entry is the one the browser addressed.
|
|
pub fn host_of(value: &str) -> Option<String> {
|
|
let host = value.split(',').next()?.trim();
|
|
Url::parse(&format!("http://{host}"))
|
|
.ok()?
|
|
.host_str()
|
|
.map(str::to_owned)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const BASE: &str = "https://windmill.example";
|
|
|
|
fn signal_with(headers: &[(&str, &str)], trusted: &TrustedOrigins) -> Option<Foreign> {
|
|
let mut map = HeaderMap::new();
|
|
for (name, value) in headers {
|
|
map.append(
|
|
header::HeaderName::from_bytes(name.as_bytes()).unwrap(),
|
|
value.parse().unwrap(),
|
|
);
|
|
}
|
|
foreign_signal(
|
|
&map,
|
|
&Uri::from_static("/api/w/ws/jobs/run/p/f"),
|
|
BASE,
|
|
trusted,
|
|
)
|
|
}
|
|
|
|
fn signal(headers: &[(&str, &str)]) -> Option<Foreign> {
|
|
signal_with(headers, &TrustedOrigins::default())
|
|
}
|
|
|
|
#[test]
|
|
fn sec_fetch_site_decides_when_present() {
|
|
for site in ["same-origin", "none", "Same-Origin"] {
|
|
assert!(signal(&[("sec-fetch-site", site)]).is_none(), "{site}");
|
|
}
|
|
for site in ["same-site", "cross-site", "unknown"] {
|
|
assert!(
|
|
matches!(
|
|
signal(&[("sec-fetch-site", site)]),
|
|
Some(Foreign::Declared(_))
|
|
),
|
|
"{site}"
|
|
);
|
|
}
|
|
// An `Origin` naming the instance does not outvote the browser's own verdict.
|
|
assert!(signal(&[
|
|
("sec-fetch-site", "same-site"),
|
|
("host", "windmill.example"),
|
|
("origin", "https://windmill.example"),
|
|
])
|
|
.is_some());
|
|
// Nor does a foreign `Referer` outvote `same-origin`.
|
|
assert!(signal(&[
|
|
("sec-fetch-site", "same-origin"),
|
|
("host", "windmill.example"),
|
|
("referer", "https://other.example/page"),
|
|
])
|
|
.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn origin_then_referer_stand_in_when_sec_fetch_site_is_absent() {
|
|
let host = ("host", "windmill-server.internal:8000");
|
|
assert!(signal(&[host, ("origin", "http://windmill.example")]).is_none());
|
|
assert!(signal(&[
|
|
("host", "windmill.example:8000"),
|
|
("origin", "http://windmill.example:3000")
|
|
])
|
|
.is_none());
|
|
assert!(signal(&[
|
|
host,
|
|
("x-forwarded-host", "alias.example, proxy.internal"),
|
|
("origin", "http://alias.example")
|
|
])
|
|
.is_none());
|
|
assert!(matches!(
|
|
signal(&[host, ("origin", "http://hub.example")]),
|
|
Some(Foreign::Origin { .. })
|
|
));
|
|
assert!(matches!(
|
|
signal(&[host, ("origin", "null")]),
|
|
Some(Foreign::Origin { .. })
|
|
));
|
|
// `Origin` outranks `Referer`.
|
|
assert!(signal(&[
|
|
host,
|
|
("origin", "http://hub.example"),
|
|
("referer", "http://windmill.example/")
|
|
])
|
|
.is_some());
|
|
assert!(signal(&[host, ("referer", "http://windmill.example/apps")]).is_none());
|
|
assert!(matches!(
|
|
signal(&[host, ("referer", "http://hub.example/x")]),
|
|
Some(Foreign::Referer { .. })
|
|
));
|
|
// No browser page named: a script, the CLI, a server-side webhook caller.
|
|
assert!(signal(&[host]).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn trusted_origins_admit_named_sites() {
|
|
let trusted = TrustedOrigins::parse(" https://dash.example , tools.example:8443 ,");
|
|
let from =
|
|
|site, origin| signal_with(&[("sec-fetch-site", site), ("origin", origin)], &trusted);
|
|
assert!(from("same-site", "https://dash.example").is_none());
|
|
assert!(from("same-site", "https://tools.example").is_none());
|
|
assert!(from("same-site", "https://hub.example").is_some());
|
|
// A top-level navigation has no `Origin`; the `Referer` names the page.
|
|
assert!(signal_with(
|
|
&[
|
|
("sec-fetch-site", "same-site"),
|
|
("referer", "https://dash.example/p")
|
|
],
|
|
&trusted
|
|
)
|
|
.is_none());
|
|
assert!(signal_with(&[("origin", "https://dash.example")], &trusted).is_none());
|
|
let any = TrustedOrigins::parse("*");
|
|
assert!(signal_with(&[("sec-fetch-site", "cross-site")], &any).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn host_of_strips_port_brackets_and_proxy_chain() {
|
|
assert_eq!(
|
|
host_of("windmill.example:8000"),
|
|
Some("windmill.example".into())
|
|
);
|
|
assert_eq!(host_of("[::1]:8000"), Some("[::1]".into()));
|
|
assert_eq!(
|
|
host_of("windmill.example, proxy.internal"),
|
|
Some("windmill.example".into())
|
|
);
|
|
assert_eq!(host_of(""), None);
|
|
assert_eq!(host_of("not a host"), None);
|
|
}
|
|
}
|