Skip to content

Conversation

@Oxyjun
Copy link
Contributor

@Oxyjun Oxyjun commented Jan 28, 2026

Summary

  • Fix potential panics in Rust example by adding bounds checking for malformed authorization headers and credentials
  • Update Hono example to use only PASSWORD binding (hardcode username as "admin") for consistency with JS/TS/Rust examples
  • Add /logout route to Hono example for consistency with other examples

@github-actions
Copy link
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/workers/ @cloudflare/workers-docs, @GregBrimble, @irvinebroque, @mikenomitch, @korinne, @WalshyDev, @cloudflare/deploy-config, @cloudflare/pcx-technical-writing, @cloudflare/wrangler, @mattietk

@github-actions
Copy link
Contributor

github-actions bot commented Jan 28, 2026

@elithrar
Copy link
Collaborator

elithrar commented Feb 5, 2026

/bonk review this

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk
Copy link
Contributor

ask-bonk bot commented Feb 5, 2026

Review Summary

The PR makes three well-motivated changes to the Basic Auth code example. Here's my assessment:

Changes

1. Rust: Fix potential panics on malformed authorization headers (lines 298-317)

Good fix. The old code accessed auth[0] and auth[1] before verifying the vector had at least 2 elements, which would panic on malformed Authorization headers (e.g., "Basic" with no space/value). The new code correctly checks auth.len() < 2 before indexing.

2. Rust: Fix potential panic on credentials without a colon (lines 311-317)

Good fix. The old code used .split(':').collect() and then directly indexed credentials[0] and credentials[1], which would panic if the decoded credentials didn't contain a colon. The new approach using .find(':') with an explicit is_none() check is safer and also consistent with how the JS/TS examples handle this (using indexOf).

3. Hono: Consistency improvements (lines 351-383)

  • Removing USERNAME from Bindings and hardcoding "admin" makes the Hono example consistent with JS/TS/Rust examples, which all hardcode the username.
  • Adding the /logout route brings feature parity across all four examples.

Minor observations (not blocking)

  • Rust unwrap() on base64 decode (basic-auth.mdx:307): BASE64_STANDARD.decode(encoded).unwrap() will still panic if the base64 payload is malformed. Since this PR focused on other panics, this is a pre-existing issue and could be addressed separately.
  • Rust lacks timing-safe comparison (basic-auth.mdx:319): The != operator isn't timing-safe, unlike the JS/TS examples. Acceptable given the caution banner stating this isn't production-ready code, but worth noting.
  • Rust == None idiom (basic-auth.mdx:287): authorization == None is not idiomatic Rust (Clippy would flag this). .is_none() is preferred. Pre-existing, not introduced by this PR.

Verdict

The changes are correct, well-scoped, and improve both safety and consistency. No issues with the new code.

github run

@elithrar
Copy link
Collaborator

elithrar commented Feb 6, 2026

/bonk fix the latent issues here. Rust code should be correct.

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk
Copy link
Contributor

ask-bonk bot commented Feb 6, 2026

Here's a summary of the three fixes applied to the Rust code in src/content/docs/workers/examples/basic-auth.mdx:

1. split(" ") -> splitn(2, ' ') (correctness fix)

The old code used .split(" ").collect() which splits on every space. The Authorization header format is Basic <encoded>, and while standard base64 doesn't contain spaces, using splitn(2, ' ') is semantically correct -- it splits only on the first space, matching how the JS/TS examples use destructuring with authorization.split(" ") (which also takes only the first two parts via destructuring).

2. let Some(...) else instead of .is_none() + .unwrap() (idiomatic Rust)

Two instances of the pattern:

if x.is_none() { return ...; }
let x = x.unwrap();

were replaced with the idiomatic let-else pattern:

let Some(x) = expr else { return ...; };

This eliminates unwrap() calls entirely, which Clippy would flag and which are a code smell even when logically safe.

3. splitn iterator with unwrap_or("") instead of Vec indexing

Instead of collecting into a Vec<&str> and indexing with bounds checks, the new code uses the splitn iterator directly with unwrap_or("") fallbacks, which is both safer and more idiomatic.

github run

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

Labels

product:workers Related to Workers product size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants