Skip to content

Commit bf00fca

Browse files
committed
fix(push-gateway): report base64 failures as encoding rejections
A malformed base64 proof was logged as an invalid CBOR structure, which points operators at the wrong layer. Give it its own variant and the `encoding` reason. A single-test binary now checks the emitted warning's level and fields through the verifier, where the shared unit-test binary cannot because of tracing's global callsite cache. Signed-off-by: Tom Brow <[email protected]>
1 parent 8f3a806 commit bf00fca

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎crates/buzz-push-gateway/src/app_attest.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl AppAttestVerifier {
9292
}
9393
let cbor = STANDARD
9494
.decode(attestation_b64)
95-
.map_err(|_| verification_error("attestation", error::AppAttestError::InvalidCbor))?;
95+
.map_err(|_| verification_error("attestation", error::AppAttestError::InvalidBase64))?;
9696
if cbor.is_empty() || cbor.len() > crate::model::MAX_APP_ATTESTATION_BYTES {
9797
return Err(verification_error(
9898
"attestation",
@@ -131,7 +131,7 @@ impl AppAttestVerifier {
131131
}
132132
let cbor = STANDARD
133133
.decode(assertion_b64)
134-
.map_err(|_| verification_error("assertion", error::AppAttestError::InvalidCbor))?;
134+
.map_err(|_| verification_error("assertion", error::AppAttestError::InvalidBase64))?;
135135
if cbor.is_empty() || cbor.len() > MAX_ASSERTION_BYTES {
136136
return Err(verification_error(
137137
"assertion",
@@ -163,6 +163,7 @@ fn verification_error(operation: &'static str, error: error::AppAttestError) ->
163163
use error::AppAttestError as E;
164164
let reason = match error {
165165
E::ResourceLimit => "capacity",
166+
E::InvalidBase64 => "encoding",
166167
E::InvalidCbor | E::InvalidFormat | E::AuthenticatorDataTooShort => "structure",
167168
E::InvalidCertificate => "certificate",
168169
E::InvalidNonce | E::InvalidSignature => "signature",

‎crates/buzz-push-gateway/src/app_attest/error.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{error::Error, fmt};
44
pub enum AppAttestError {
55
/// Certificate structure or trust validation failed.
66
InvalidCertificate,
7+
/// The proof is not valid standard base64.
8+
InvalidBase64,
79
/// CBOR is malformed, ambiguous, or does not match the operation schema.
810
InvalidCbor,
911
/// An operational parsing budget was exceeded.
@@ -23,6 +25,7 @@ impl fmt::Display for AppAttestError {
2325
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2426
match self {
2527
Self::InvalidCertificate => write!(f, "invalid certificate path or structure"),
28+
Self::InvalidBase64 => write!(f, "invalid base64 encoding"),
2629
Self::InvalidCbor => write!(f, "invalid CBOR structure"),
2730
Self::ResourceLimit => write!(f, "verification resource limit"),
2831
AppAttestError::InvalidNonce => write!(f, "invalid nonce"),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! App Attest rejections must tell operators which check failed. This is the
2+
//! only test in its binary, so no other thread races tracing's global
3+
//! callsite-interest cache.
4+
use base64::{engine::general_purpose::STANDARD, Engine as _};
5+
use buzz_push_gateway::app_attest::AppAttestVerifier;
6+
use std::sync::{Arc, Mutex};
7+
8+
#[derive(Clone, Default)]
9+
struct Capture(Arc<Mutex<Vec<u8>>>);
10+
impl std::io::Write for Capture {
11+
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
12+
self.0.lock().unwrap().extend_from_slice(buf);
13+
Ok(buf.len())
14+
}
15+
fn flush(&mut self) -> std::io::Result<()> {
16+
Ok(())
17+
}
18+
}
19+
20+
#[test]
21+
fn attestation_rejections_log_operation_reason_and_failed_check() {
22+
let root = std::fs::read(concat!(
23+
env!("CARGO_MANIFEST_DIR"),
24+
"/tests/fixtures/apple-app-attestation-root.pem"
25+
))
26+
.unwrap();
27+
let verifier = AppAttestVerifier::new("TEAM.test.app".into(), root).unwrap();
28+
let key_id = STANDARD.encode([7; 32]);
29+
let cases = [
30+
("not base64!", "encoding", "invalid base64 encoding"),
31+
(
32+
&*STANDARD.encode(b"not cbor"),
33+
"structure",
34+
"invalid CBOR structure",
35+
),
36+
];
37+
for (attestation, reason, detail) in cases {
38+
let capture = Capture::default();
39+
let writer = capture.clone();
40+
let subscriber = tracing_subscriber::fmt()
41+
.json()
42+
.with_max_level(tracing::Level::WARN)
43+
.with_writer(move || writer.clone())
44+
.finish();
45+
let result = tracing::subscriber::with_default(subscriber, || {
46+
verifier.verify_attestation(attestation, &key_id, b"client-data")
47+
});
48+
assert!(result.is_err());
49+
let bytes = capture.0.lock().unwrap().clone();
50+
let line: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
51+
assert_eq!(line["level"], "WARN", "{line}");
52+
assert_eq!(
53+
line["fields"]["message"],
54+
"App Attest verification rejected"
55+
);
56+
assert_eq!(line["fields"]["operation"], "attestation");
57+
assert_eq!(line["fields"]["reason"], reason, "{line}");
58+
assert_eq!(line["fields"]["detail"], detail, "{line}");
59+
}
60+
}

0 commit comments

Comments
 (0)