From ac18cf8c826a4e32b63ac3e1dfd119b60df2bc48 Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Mon, 23 Nov 2026 05:15:23 +0500 Subject: [PATCH 1/4] login: refuse a sign-in page off the console's origin or a code with control characters When you run `proofline login`, the server answers with an approval page address and a short code. The CLI opened that address in the browser without checking where it pointed, and printed the code as is. A mis-routed, compromised or custom --base-url server could therefore make the CLI open another site (a look-alike approval page) or a file: or custom-scheme URL, and could put terminal escape sequences (ESC[2J clears the screen) or bidi controls (U+202E reverses the text) into the sign-in prompt. device::start now refuses the reply, before anything is printed or opened, when: - verification_uri or verification_uri_complete does not parse, or is not on http.origin() (same rule as the Chrome extension's auth.ts); - user_code is changed by text::one_line, i.e. it holds a control or bidi character. The returned addresses are the parsed URLs' ASCII serialization. Refusing instead of cleaning the code: a cleaned code would differ from the one the server expects, so the person would type a code that can never be approved. Tests (tests/http.rs): a_sign_in_page_on_another_origin_is_refused and a_sign_in_code_that_could_rewrite_the_terminal_is_refused fail on main and pass now. The device-code stub now answers with its real origin (from the Host header); before, it had no port, so it was off-origin. --- src/device.rs | 29 ++++++++++++++++++++-- tests/http.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/device.rs b/src/device.rs index 759b74a..e8bb1a8 100644 --- a/src/device.rs +++ b/src/device.rs @@ -6,10 +6,12 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; use secrecy::{ExposeSecret as _, SecretString}; use serde::Deserialize; +use url::Url; use uuid::Uuid; use crate::api::{ApiError, Http}; use crate::credentials::{Credential, Source}; +use crate::text; const GRANT: &str = "urn:ietf:params:oauth:grant-type:device_code"; const SLOW_DOWN_SECONDS: u64 = 5; @@ -75,16 +77,39 @@ pub async fn start(http: &Http, scopes: &[String]) -> Result Result { + let page = Url::parse(address).map_err(|_| http.protocol(format!("{field} is not a URL")))?; + if page.origin().ascii_serialization() != http.origin().as_key() { + return Err(http.protocol(format!("{field} is not on {}", http.origin()))); + } + Ok(page.into()) +} + /// Polls until the person approves, refuses or the code expires. /// `organization` names the organization when the server does not report it. pub async fn finish( diff --git a/tests/http.rs b/tests/http.rs index 3d1b9d4..792f7c6 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -429,17 +429,25 @@ struct Fake { polls: Arc, report_organization: bool, summary: Option, + /// Fields that replace the stand-in's own device-code reply. + challenge: Option, } -async fn code() -> Json { - Json(json!({ +/// Answers with approval pages on the stand-in's own origin, as the console does. +async fn code(State(fake): State, headers: HeaderMap) -> Json { + let host = headers[header::HOST].to_str().unwrap(); + let mut body = json!({ "device_code": "d".repeat(52), "user_code": "WDJB-MJHT", - "verification_uri": "http://127.0.0.1/auth/device", - "verification_uri_complete": "http://127.0.0.1/auth/device?user_code=WDJB-MJHT", + "verification_uri": format!("http://{host}/auth/device"), + "verification_uri_complete": format!("http://{host}/auth/device?user_code=WDJB-MJHT"), "expires_in": 600, "interval": 1, - })) + }); + if let Some(Value::Object(fields)) = fake.challenge { + body.as_object_mut().unwrap().extend(fields); + } + Json(body) } async fn token(State(fake): State, Json(body): Json) -> Response { @@ -597,6 +605,57 @@ async fn device_sign_in_waits_for_approval_and_keeps_the_organization() { assert!(!format!("{credential:?}").contains(TOKEN)); } +#[tokio::test] +async fn a_sign_in_page_on_another_origin_is_refused() { + for (field, address) in [ + ("verification_uri", "https://proofline.example/auth/device"), + ( + "verification_uri_complete", + "https://proofline.example/auth/device?user_code=WDJB-MJHT", + ), + ( + "verification_uri_complete", + "file:///C:/Windows/System32/calc.exe", + ), + ("verification_uri", "not a url"), + ] { + let http = Http::new( + serve(Fake { + challenge: Some(json!({ field: address })), + ..Fake::default() + }) + .await, + ) + .unwrap(); + let refusal = device::start(&http, &[]).await.unwrap_err(); + assert!( + matches!(&refusal, DeviceError::Api(ApiError::Protocol { .. })), + "{field} = {address}: {refusal:?}" + ); + assert!(refusal.to_string().contains(field), "{refusal}"); + } +} + +#[tokio::test] +async fn a_sign_in_code_that_could_rewrite_the_terminal_is_refused() { + for code in ["WDJB-MJHT\u{1b}[2J", "WDJB-\u{202E}THJM", "WDJB\nMJHT"] { + let http = Http::new( + serve(Fake { + challenge: Some(json!({ "user_code": code })), + ..Fake::default() + }) + .await, + ) + .unwrap(); + let refusal = device::start(&http, &[]).await.unwrap_err(); + assert!( + matches!(&refusal, DeviceError::Api(ApiError::Protocol { .. })), + "{code:?}: {refusal:?}" + ); + assert!(!refusal.to_string().contains(code), "{refusal}"); + } +} + #[tokio::test] async fn a_server_that_omits_the_organization_needs_one_named() { let http = Http::new(serve(Fake::default()).await).unwrap(); From c1f35f9e9c44bdad9c345da9aff6981976a9c339 Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Mon, 23 Nov 2026 05:18:50 +0500 Subject: [PATCH 2/4] login: also require http(s) for the approval page, and test the kept URLs Proofline review, round 1. 1. A blob: URL takes the origin of the URL inside it, so "blob:/id" passed the origin check although it is not a web page. approval_page now also requires the http or https scheme. New case in a_sign_in_page_on_another_origin_is_refused (failed before this change). 2. The sign-in test did not look at the URLs start returns, so it would still pass if the complete URL were dropped or the raw text returned. The stub now takes "{origin}" in an overriding field, and the test sends same-origin pages with dot segments ("/auth/./device") and asserts the exact cleaned-up values. Returning the raw text makes it fail. --- src/device.rs | 8 ++++++-- tests/http.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/device.rs b/src/device.rs index e8bb1a8..587b131 100644 --- a/src/device.rs +++ b/src/device.rs @@ -101,10 +101,14 @@ pub async fn start(http: &Http, scopes: &[String]) -> Result Result { let page = Url::parse(address).map_err(|_| http.protocol(format!("{field} is not a URL")))?; - if page.origin().ascii_serialization() != http.origin().as_key() { + if !matches!(page.scheme(), "http" | "https") + || page.origin().ascii_serialization() != http.origin().as_key() + { return Err(http.protocol(format!("{field} is not on {}", http.origin()))); } Ok(page.into()) diff --git a/tests/http.rs b/tests/http.rs index 792f7c6..8dcc2b2 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -434,18 +434,24 @@ struct Fake { } /// Answers with approval pages on the stand-in's own origin, as the console does. +/// `{origin}` in an overriding field stands for that origin. async fn code(State(fake): State, headers: HeaderMap) -> Json { - let host = headers[header::HOST].to_str().unwrap(); + let origin = format!("http://{}", headers[header::HOST].to_str().unwrap()); let mut body = json!({ "device_code": "d".repeat(52), "user_code": "WDJB-MJHT", - "verification_uri": format!("http://{host}/auth/device"), - "verification_uri_complete": format!("http://{host}/auth/device?user_code=WDJB-MJHT"), + "verification_uri": format!("{origin}/auth/device"), + "verification_uri_complete": format!("{origin}/auth/device?user_code=WDJB-MJHT"), "expires_in": 600, "interval": 1, }); if let Some(Value::Object(fields)) = fake.challenge { - body.as_object_mut().unwrap().extend(fields); + for (name, value) in fields { + body[name] = match value { + Value::String(text) => Value::String(text.replace("{origin}", &origin)), + other => other, + }; + } } Json(body) } @@ -587,11 +593,25 @@ fn org() -> Uuid { async fn device_sign_in_waits_for_approval_and_keeps_the_organization() { let fake = Fake { report_organization: true, + // Same-origin pages whose dot segments the parsed URL removes. + challenge: Some(json!({ + "verification_uri": "{origin}/auth/./device", + "verification_uri_complete": "{origin}/auth/x/../device?user_code=WDJB-MJHT", + })), ..Fake::default() }; - let http = Http::new(serve(fake.clone()).await).unwrap(); + let origin = serve(fake.clone()).await; + let http = Http::new(origin.clone()).unwrap(); let challenge = device::start(&http, &[]).await.unwrap(); assert_eq!(challenge.user_code.expose_secret(), "WDJB-MJHT"); + assert_eq!(challenge.verification_uri, format!("{origin}/auth/device")); + assert_eq!( + challenge + .verification_uri_complete + .as_ref() + .map(|complete| complete.expose_secret().to_owned()), + Some(format!("{origin}/auth/device?user_code=WDJB-MJHT")) + ); assert!(format!("{challenge:?}").contains("REDACTED")); assert!(!format!("{challenge:?}").contains("WDJB")); let credential = device::finish(&http, &challenge, None).await.unwrap(); @@ -618,6 +638,8 @@ async fn a_sign_in_page_on_another_origin_is_refused() { "file:///C:/Windows/System32/calc.exe", ), ("verification_uri", "not a url"), + // A blob: URL takes the origin of the URL inside it. + ("verification_uri_complete", "blob:{origin}/0190f3a0"), ] { let http = Http::new( serve(Fake { From c8eeeeeeda82bfc8afbce2c6184d7649c242fb6d Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Mon, 23 Nov 2026 05:19:53 +0500 Subject: [PATCH 3/4] tests: use ORIGIN as the stub's placeholder so clippy passes "{origin}" in a plain string looks like a format argument to clippy (literal_string_with_formatting_args), which fails with -D warnings. --- tests/http.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/http.rs b/tests/http.rs index 8dcc2b2..aa01c43 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -434,7 +434,7 @@ struct Fake { } /// Answers with approval pages on the stand-in's own origin, as the console does. -/// `{origin}` in an overriding field stands for that origin. +/// `ORIGIN` in an overriding field stands for that origin. async fn code(State(fake): State, headers: HeaderMap) -> Json { let origin = format!("http://{}", headers[header::HOST].to_str().unwrap()); let mut body = json!({ @@ -448,7 +448,7 @@ async fn code(State(fake): State, headers: HeaderMap) -> Json { if let Some(Value::Object(fields)) = fake.challenge { for (name, value) in fields { body[name] = match value { - Value::String(text) => Value::String(text.replace("{origin}", &origin)), + Value::String(text) => Value::String(text.replace("ORIGIN", &origin)), other => other, }; } @@ -595,8 +595,8 @@ async fn device_sign_in_waits_for_approval_and_keeps_the_organization() { report_organization: true, // Same-origin pages whose dot segments the parsed URL removes. challenge: Some(json!({ - "verification_uri": "{origin}/auth/./device", - "verification_uri_complete": "{origin}/auth/x/../device?user_code=WDJB-MJHT", + "verification_uri": "ORIGIN/auth/./device", + "verification_uri_complete": "ORIGIN/auth/x/../device?user_code=WDJB-MJHT", })), ..Fake::default() }; @@ -639,7 +639,7 @@ async fn a_sign_in_page_on_another_origin_is_refused() { ), ("verification_uri", "not a url"), // A blob: URL takes the origin of the URL inside it. - ("verification_uri_complete", "blob:{origin}/0190f3a0"), + ("verification_uri_complete", "blob:ORIGIN/0190f3a0"), ] { let http = Http::new( serve(Fake { From 5ea12ba603f5b05930c010245c2ccbb5cc31a54d Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Mon, 23 Nov 2026 05:24:27 +0500 Subject: [PATCH 4/4] tests: refuse an approval page that differs from the console only by port Proofline review, round 2: every refused http(s) case also changed the host, so a check that ignored the port would still pass. The new case keeps the stand-in's scheme and host and uses port 1, which an ephemeral test port never is. A host-only comparison makes it fail. --- tests/http.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/http.rs b/tests/http.rs index aa01c43..e04c3ab 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -637,6 +637,8 @@ async fn a_sign_in_page_on_another_origin_is_refused() { "verification_uri_complete", "file:///C:/Windows/System32/calc.exe", ), + // Same scheme and host as the stand-in, whose port is never 1. + ("verification_uri", "http://127.0.0.1:1/auth/device"), ("verification_uri", "not a url"), // A blob: URL takes the origin of the URL inside it. ("verification_uri_complete", "blob:ORIGIN/0190f3a0"),