Encapsulate Responses API proxy authentication (#55296)

## What changed

Introduce a shared `Auth` abstraction that supplies sensitive authorization headers and the client's redirect policy. Preserve the existing static API key allocation and default redirect behavior, and return an empty `502` response if header acquisition fails.

Unify the stdin authentication entry point around platform-specific `read_from_stdin` helpers.

## Testing

Add a test verifying that both compact and pretty `Debug` output redact API keys.

GitOrigin-RevId: 6a924534f00c1b209b24e8b0af117091cfa3ec07
This commit is contained in:
ee
2026-10-19 15:06:10 +00:00
committed by copyberry
parent 7c073196a1
commit f6e8ca3fc1
4 changed files with 84 additions and 14 deletions
@@ -12,21 +12,20 @@ const AUTH_HEADER_PREFIX: &[u8] = b"Bearer ";
/// value with the auth token used with `Bearer`. The header value is returned
/// as a `&'static str` whose bytes are locked in memory to avoid accidental
/// exposure.
#[cfg(unix)]
pub(crate) fn read_auth_header_from_stdin() -> Result<&'static str> {
read_auth_header_with(read_from_unix_stdin)
read_auth_header_with(read_from_stdin)
}
#[cfg(windows)]
pub(crate) fn read_auth_header_from_stdin() -> Result<&'static str> {
pub(crate) fn read_from_stdin(buffer: &mut [u8]) -> std::io::Result<usize> {
use std::io::Read;
// Use of `stdio::io::stdin()` has the problem mentioned in the docstring on
// the UNIX version of `read_from_unix_stdin()`, so this should ultimately
// the UNIX version of `read_from_stdin()`, so this should ultimately
// be replaced the low-level Windows equivalent. Because we do not have an
// equivalent of mlock() on Windows right now, it is not pressing until we
// address that issue.
read_auth_header_with(|buffer| std::io::stdin().read(buffer))
std::io::stdin().read(buffer)
}
/// We perform a low-level read with `read(2)` because `stdio::io::stdin()` has
@@ -38,7 +37,7 @@ pub(crate) fn read_auth_header_from_stdin() -> Result<&'static str> {
/// it out, whereas we aim to guarantee there is exactly one copy of the API key
/// in memory, protected by mlock(2).
#[cfg(unix)]
fn read_from_unix_stdin(buffer: &mut [u8]) -> std::io::Result<usize> {
pub(crate) fn read_from_stdin(buffer: &mut [u8]) -> std::io::Result<usize> {
use libc::c_void;
use libc::read;