Skip to content
Open
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ configured loopback port and preserve the browser's `Host` and `Origin` headers.
The returned launch URL uses the configured HTTPS origin and includes the
session token. Allow only trusted users to reach the proxy.

For a stable browser URL, pass `browser_auth_file := '/path/to/auth.json'` along
with `browser_origin`. The file can configure either or both sign-in methods:

```json
{
"accessKeySha256": "<lowercase SHA-256 of a 64-character lowercase hex key>",
"trustedProxy": {
"header": "X-Authenticated-User",
"value": "owner@example.net"
}
}
```

Generate an access key with `openssl rand -hex 32`, store it securely, and put
only its hash in the JSON. Access-key sign-in remembers the browser for seven
days. With a trusted proxy, the ordinary URL opens the notebook for the
specified identity. The proxy must authenticate the user and remove incoming
copies of the identity header before setting its own. Keep untrusted clients
away from the loopback listener. SQLNB tries an existing sign-in automatically
and shows the access-key form if that method is configured and a browser still
needs to sign in. Deployments with neither method use launch links. A valid
launch link can still open SQLNB with any configuration.

To connect an MCP client, start with `mcp := true` and follow the
[MCP setup guide](docs/mcp.md). MCP is disabled by default.

Expand Down
1 change: 1 addition & 0 deletions http/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ http-body-util = "0.1"
libc = "0.2"
mime_guess = "2"
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] }
ring = "0.17"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha2 = "0.10"
Expand Down
234 changes: 234 additions & 0 deletions http/src/browser_auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
use super::{header, ApiError, AppState, HeaderMap, HeaderValue, Json, State, StatusCode};
use ring::hmac;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
fs::File,
io::Read,
time::{SystemTime, UNIX_EPOCH},
};
use subtle::ConstantTimeEq;

const SESSION_SECONDS: u64 = 7 * 24 * 60 * 60;

#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BrowserAuthConfig {
access_key_sha256: Option<String>,
trusted_proxy: Option<TrustedProxyConfig>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TrustedProxyConfig {
header: String,
value: String,
}

#[derive(Clone, Default)]
pub(super) struct BrowserAuth {
access_key_digest: Option<String>,
trusted_proxy: Option<(header::HeaderName, HeaderValue)>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct Methods {
access_key: bool,
trusted_proxy: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(super) struct Credentials {
access_key: Option<String>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct Session {
launch_capability: String,
}

impl BrowserAuth {
pub(super) fn load(path: &str, origin: &str) -> Result<Self, String> {
if path.is_empty() {
return Ok(Self::default());
}
if !origin.starts_with("https://") {
return Err("browser_auth_file requires an HTTPS browser_origin".into());
}
let mut bytes = Vec::new();
File::open(path)
.and_then(|file| file.take(16_385).read_to_end(&mut bytes))
.map_err(|error| format!("could not read browser_auth_file: {error}"))?;
if bytes.len() > 16_384 {
return Err("browser_auth_file exceeds 16 KiB".into());
}
let config: BrowserAuthConfig =
serde_json::from_slice(&bytes).map_err(|_| "invalid browser_auth_file JSON")?;
if config
.access_key_sha256
.as_deref()
.is_some_and(|digest| !is_key(digest))
{
return Err("accessKeySha256 must be 64 lowercase hexadecimal characters".into());
}
let trusted_proxy = config
.trusted_proxy
.map(|proxy| {
let name = header::HeaderName::from_bytes(proxy.header.as_bytes())
.map_err(|_| "trustedProxy.header is not a valid header name")?;
if matches!(
name.as_str(),
"host" | "origin" | "authorization" | "cookie"
) || name.as_str().starts_with("sec-")
{
return Err("trustedProxy.header must be a dedicated identity header");
}
let value = HeaderValue::from_str(&proxy.value)
.map_err(|_| "trustedProxy.value is not a valid header value")?;
if value.is_empty() {
return Err("trustedProxy.value must not be empty");
}
Ok((name, value))
})
.transpose()?;
Ok(Self {
access_key_digest: config.access_key_sha256,
trusted_proxy,
})
}

fn accepts_proxy(&self, headers: &HeaderMap) -> bool {
let Some((name, expected)) = &self.trusted_proxy else {
return false;
};
let mut values = headers.get_all(name).iter();
values.next() == Some(expected) && values.next().is_none()
}
}

pub(super) async fn methods(State(state): State<AppState>) -> Json<Methods> {
Json(Methods {
access_key: state.browser_auth.access_key_digest.is_some(),
trusted_proxy: state.browser_auth.trusted_proxy.is_some(),
})
}

pub(super) async fn sign_in(
State(state): State<AppState>,
headers: HeaderMap,
Json(credentials): Json<Credentials>,
) -> Result<impl axum::response::IntoResponse, ApiError> {
let unauthorized = || {
ApiError::new(
StatusCode::UNAUTHORIZED,
"sign_in_required",
"Sign in to open this notebook.",
)
};
if headers
.get(header::ORIGIN)
.and_then(|value| value.to_str().ok())
!= Some(&state.origin)
{
return Err(unauthorized());
}

let mut output_headers = HeaderMap::new();
if let Some(access_key) = credentials.access_key {
let digest = state
.browser_auth
.access_key_digest
.as_deref()
.ok_or_else(unauthorized)?;
if !is_key(&access_key)
|| !bool::from(
format!("{:x}", Sha256::digest(access_key))
.as_bytes()
.ct_eq(digest.as_bytes()),
)
{
return Err(unauthorized());
}
set_cookie(&mut output_headers, &state.origin, digest);
} else if !state.browser_auth.accepts_proxy(&headers)
&& !state
.browser_auth
.access_key_digest
.as_deref()
.is_some_and(|digest| accepts_cookie(&headers, &state.origin, digest))
{
return Err(unauthorized());
}
Ok((
output_headers,
Json(Session {
launch_capability: state.token,
}),
))
}

fn is_key(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

fn unix_seconds() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}

fn cookie_name(origin: &str) -> String {
let hash = format!("{:x}", Sha256::digest(origin));
format!("__Host-sqlnb-{}", &hash[..16])
}

fn signature(origin: &str, digest: &str, expiry: u64) -> String {
let key = hmac::Key::new(hmac::HMAC_SHA256, digest.as_bytes());
let message = format!("sqlnb-session-v1\n{origin}\n{expiry}");
hmac::sign(&key, message.as_bytes())
.as_ref()
.iter()
.map(|byte| format!("{byte:02x}"))
.collect()
}

fn set_cookie(headers: &mut HeaderMap, origin: &str, digest: &str) {
let expiry = unix_seconds() + SESSION_SECONDS;
let cookie = format!(
"{}={expiry}.{}; Max-Age={SESSION_SECONDS}; Path=/; Secure; HttpOnly; SameSite=Strict",
cookie_name(origin),
signature(origin, digest, expiry),
);
headers.insert(header::SET_COOKIE, HeaderValue::from_str(&cookie).unwrap());
}

fn accepts_cookie(headers: &HeaderMap, origin: &str, digest: &str) -> bool {
let name = cookie_name(origin);
let value = headers
.get_all(header::COOKIE)
.iter()
.filter_map(|header| header.to_str().ok())
.flat_map(|header| header.split(';'))
.filter_map(|cookie| cookie.trim().split_once('='))
.find_map(|(key, value)| (key == name).then_some(value));
let Some((expiry, mac)) = value.and_then(|value| value.split_once('.')) else {
return false;
};
let Ok(expiry) = expiry.parse::<u64>() else {
return false;
};
expiry > unix_seconds()
&& is_key(mac)
&& bool::from(
signature(origin, digest, expiry)
.as_bytes()
.ct_eq(mac.as_bytes()),
)
}
1 change: 1 addition & 0 deletions http/src/imports/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn config(temp: &TempDir, port: u16) -> ServerConfig {
notebook_root: temp.path().join("notebooks").display().to_string(),
mcp_token: String::new(),
browser_origin: String::new(),
browser_auth_file: String::new(),
}
}

Expand Down
12 changes: 12 additions & 0 deletions http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod platform;
#[cfg(windows)]
mod windows_process;
use platform::*;
mod browser_auth;
mod duckdb_ui;
mod history;
mod imports;
Expand Down Expand Up @@ -71,12 +72,15 @@ struct ServerConfig {
mcp_token: String,
#[serde(default)]
browser_origin: String,
#[serde(default)]
browser_auth_file: String,
}

#[derive(Clone)]
struct AppState {
origin: String,
allowed_hosts: Vec<String>,
browser_auth: browser_auth::BrowserAuth,
token: String,
duckdb_version: String,
quack_url: Url,
Expand Down Expand Up @@ -337,6 +341,7 @@ fn build_state(config: ServerConfig, port: u16) -> Result<AppState, String> {
return Err("local UI capability must not be empty".into());
}
let (origin, allowed_hosts) = browser_origin_and_hosts(&config.browser_origin, port)?;
let browser_auth = browser_auth::BrowserAuth::load(&config.browser_auth_file, &origin)?;
let mut quack_url = parse_loopback_url(&config.quack_url, "Quack URL")?;
if quack_url.path().is_empty() || quack_url.path() == "/" {
quack_url.set_path("/quack");
Expand Down Expand Up @@ -372,6 +377,7 @@ fn build_state(config: ServerConfig, port: u16) -> Result<AppState, String> {
Ok(AppState {
origin,
allowed_hosts,
browser_auth,
token: config.token,
duckdb_version: config.duckdb_version,
quack_url,
Expand Down Expand Up @@ -2302,6 +2308,12 @@ async fn static_asset(
fn router(state: AppState) -> Router {
let mut router = Router::new()
.route("/api/v1/bootstrap", get(bootstrap))
.route(
"/api/v1/session",
get(browser_auth::methods)
.post(browser_auth::sign_in)
.layer(DefaultBodyLimit::max(4096)),
)
.route("/api/v1/files", get(list_files))
.route("/api/v1/git", get(git_status))
.route("/api/v1/git/diff", get(git_diff))
Expand Down
Loading
Loading