Skip to content

login: refuse a sign-in page off the console's origin or a code with control characters - #2

Open
Ahmar004 wants to merge 4 commits into
mainfrom
fix/validate-device-sign-in-fields
Open

Ahmar004 wants to merge 4 commits into
mainfrom
fix/validate-device-sign-in-fields

Conversation

@Ahmar004

Copy link
Copy Markdown
Owner

Fork review PR (Proofline). Upstream issue to follow.

The problem in one sentence

proofline login opened whatever approval address the server sent, on any site or scheme, and printed the sign-in code with any hidden terminal control characters in it.

Why it happens, step by step

  1. proofline login asks the console for a sign-in code (POST /auth/device/code, the RFC 8628 "device flow"). The reply holds user_code (the short code you type), verification_uri (the approval page) and, optionally, verification_uri_complete (the same page with the code filled in).
  2. device::start kept these fields exactly as sent.
  3. login printed user_code raw and passed verification_uri_complete to webbrowser::open after checking only that it parses as some URL. So https://look-alike.example/…, file:///… and custom schemes were all opened.
  4. A control character is an invisible character that tells the terminal to do something (ESC[2J clears the screen). A bidi control (U+202E) makes the text after it display reversed. Both reached the terminal unchanged.

The rest of the CLI already treats server text as untrusted (text::terminal_safe / text::one_line), and the Proofline Chrome extension already refuses an off-origin verification_uri_complete (lib/proofline/auth.ts). The CLI's sign-in was the one path that skipped both.

Who is affected

With the default https://proofline.sh this is defense in depth. It matters with a custom --base-url / PROOFLINE_BASE_URL (local or staging consoles), or a mis-routed or compromised origin: the reply could send you to a look-alike approval page, open a local file or a protocol handler, or rewrite the sign-in prompt.

What this PR changes

src/device.rs only (lib.rs is untouched). device::start now refuses the reply, as a protocol error and before anything is printed or opened, when:

  • verification_uri or verification_uri_complete doesn't parse, or its origin isn't http.origin() (same rule as the extension). A file: URL has an opaque origin, so it never matches. A new helper, approval_page, does this check.
  • user_code would be changed by text::one_line, meaning it holds a control or bidi character.

The kept addresses are the parsed URLs' serialization. That text is ASCII only, so printing it is safe.

Why refuse the code instead of cleaning it

A cleaned code (control characters replaced by �) would differ from the one the server expects, so the person would type a code that can never be approved. A clear error is better. A real console never sends such a code: RFC 8628 §6.1 recommends a short code from a small set of letters.

How I tested it (Windows 11, Rust 1.100.0)

Regression tests (tests/http.rs), written first:

Test main this PR
a_sign_in_page_on_another_origin_is_refused (other host for each field, file: URL, not a URL) FAILED (start returned Ok) ok
a_sign_in_code_that_could_rewrite_the_terminal_is_refused (ESC[2J, U+202E, newline) FAILED ok

The device-code stub used to answer http://127.0.0.1/auth/device, with no port, so it was already off-origin. It now builds the page from the request's Host header, so it is on the stub's real origin, like the console. The existing sign-in tests still pass. cargo test --test http: 42 passed.

End to end, the real binary against a tiny local stand-in server (proofline login --base-url http://127.0.0.1:8765, output through cat -v):

Server reply main this PR
code WDJB-MJHT\x1b[2J\u202eTHJM Your sign-in code is WDJB-MJHT^[[2JM-bM-^@M-.THJM. (escapes reach the terminal) … user_code contains control characters, exit 1
verification_uri_complete = https://proofline-login.example/approve accepted (opened without --no-browser) … verification_uri_complete is not on http://127.0.0.1:8765, exit 1, no browser
verification_uri_complete = file:///C:/Windows/System32/calc.exe accepted refused the same way
normal same-origin reply prompt printed, polls same prompt, unchanged

Whole suite: cargo fmt --check clean. cargo test --no-fail-fast gives the same 8 failures on main and on this branch: tests in tests/mcp.rs (5), tests/agent_install.rs, tests/local_patch.rs and src/update/tests.rs that fail on this Windows machine and have nothing to do with sign-in. cargo clippy --all-targets -D warnings reports the same 3 errors on main and here, all in src/agent/tests.rs, which this PR doesn't touch.

…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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 2 new findings to review.

View full review in Proofline

Comment thread src/device.rs
Comment on lines +87 to +90
let verification_uri = approval_page(http, "verification_uri", &body.verification_uri)?;
let verification_uri_complete = body
.verification_uri_complete
.map(|address| approval_page(http, "verification_uri_complete", &address))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Approval URL validation accepts same-origin blob: URLs

approval_page promises to reject custom schemes, but compares only origins. A blob: URL inherits its embedded URL’s origin and passes when that origin matches the console.

The CLI accepts an unsupported approval URL and may pass it to the browser.

Require the outer scheme to be http or https before comparing origins.

Details

Before this change, both fields were passed through without validation. This change introduces the scheme-rejection contract but does not enforce it for either field.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c1f35f9. Agreed: Url::origin() returns the inner URL's origin for blob:, so blob:<console origin>/id passed. I reproduced it first (new case in a_sign_in_page_on_another_origin_is_refused returned Ok), then made approval_page also require the http/https scheme. Same-origin plus an http(s) scheme means the scheme equals the console's.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked fixed.

Comment thread tests/http.rs
Comment on lines +437 to 440
async fn code(State(fake): State<Fake>, headers: HeaderMap) -> Json<Value> {
let host = headers[header::HOST].to_str().unwrap();
let mut body = json!({
"device_code": "d".repeat(52),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Successful sign-in test misses approval URL regressions

The fixture supplies already-serialized URLs, and the success test asserts neither URL returned by device::start.

The test still passes if validation succeeds but the complete URL is dropped or the original URL strings are returned unchanged.

Supply same-origin URLs that change on serialization and assert both exact serialized values, including the complete URL’s query.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c1f35f9 (placeholder renamed in c8eeeee). Agreed. The stub now accepts an ORIGIN placeholder, and the success test sends same-origin pages with dot segments (ORIGIN/auth/./device, ORIGIN/auth/x/../device?user_code=WDJB-MJHT) and asserts the exact cleaned-up values of both fields, including the query. Checked by mutation: making approval_page return the raw string fails the test (/auth/./device vs /auth/device).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked fixed.

…URLs

Proofline review, round 1.

1. A blob: URL takes the origin of the URL inside it, so
   "blob:<console origin>/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.
"{origin}" in a plain string looks like a format argument to clippy
(literal_string_with_formatting_args), which fails with -D warnings.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 1 new finding to review.

View full review in Proofline

Comment thread tests/http.rs
Comment on lines +628 to +631
#[tokio::test]
async fn a_sign_in_page_on_another_origin_is_refused() {
for (field, address) in [
("verification_uri", "https://proofline.example/auth/device"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Approval URL test misses port-only origin mismatches

Both HTTP(S) rejection cases change the scheme and hostname, so the test still passes if approval_page compares those components but ignores the port.

The test would miss a regression that accepts approval URLs on a different port.

Add a URL with the fixture’s scheme and hostname but a guaranteed different port, and assert a field-specific Protocol error.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5ea12ba. Agreed. Added http://127.0.0.1:1/auth/device: same scheme and host as the stand-in, port 1, which the ephemeral test port never is. It goes through the same field-specific Protocol assertion. Checked by mutation: comparing only the host makes the test fail.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked fixed.

…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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 1 new finding to review.

View full review in Proofline

Comment thread src/device.rs
Comment on lines +87 to +90
let verification_uri = approval_page(http, "verification_uri", &body.verification_uri)?;
let verification_uri_complete = body
.verification_uri_complete
.map(|address| approval_page(http, "verification_uri_complete", &address))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Device sign-in tests miss a required-complete-URL regression

Every device-code fixture supplies verification_uri_complete, so the new URL-validation coverage would still pass if device::start rejected responses that omit it.

Sign-in could fail before falling back to verification_uri without a test failing.

Add a successful response that omits verification_uri_complete and assert that the returned field is None.

Details

The base fixture also supplied both URLs, but it did not exercise the approval-URL validation added by this change.

View finding in Proofline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant