Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ mod web;

#[cfg(test)]
mod aead_db_tamper_tests;
#[cfg(test)]
mod parent_secret_response_tests;

use apple_signin::AppleJwtVerifier;
use oauth::{AppleProvider, GithubProvider, GoogleProvider, OAuthManager};
Expand Down Expand Up @@ -2223,20 +2225,33 @@ async fn get_secret(key_name: &str) -> Result<String, Error> {
crate::aws_credentials::MAX_VSOCK_RESPONSE_BYTES,
)?;

let parent_response: ParentResponse = serde_json::from_str(&response)?;
if parent_response.response_type == "secret" {
let secret_json: Value =
serde_json::from_str(parent_response.response_value.as_str().unwrap())?;
parse_parent_secret_response(&response)
}

// Assuming the secret is always a JSON object with a single key-value pair
if let Some((_, value)) = secret_json.as_object().and_then(|obj| obj.iter().next()) {
Ok(value.as_str().unwrap_or_default().to_string())
} else {
Err(Error::SecretParsingError)
}
} else {
Err(Error::AuthenticationError)
fn parse_parent_secret_response(response: &str) -> Result<String, Error> {
let parent_response: ParentResponse = serde_json::from_str(response)?;
if parent_response.response_type != "secret" {
return Err(Error::AuthenticationError);
}

let response_value = parent_response
.response_value
.as_str()
.ok_or(Error::SecretParsingError)?;
let secret_json: Value = serde_json::from_str(response_value)?;

// The parent returns a JSON object containing exactly the requested secret value.
let secret_object = secret_json.as_object().ok_or(Error::SecretParsingError)?;
if secret_object.len() != 1 {
return Err(Error::SecretParsingError);
}

secret_object
.values()
.next()
.and_then(Value::as_str)
.map(ToOwned::to_owned)
.ok_or(Error::SecretParsingError)
}

async fn get_or_create_enclave_key(
Expand Down
76 changes: 76 additions & 0 deletions src/parent_secret_response_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::{parse_parent_secret_response, Error};

#[test]
fn parses_valid_parent_secret_response() {
let response = r#"{
"response_type": "secret",
"response_value": "{\"database_url\":\"postgres://localhost/test\"}"
}"#;

let secret = parse_parent_secret_response(response).expect("valid response should parse");

assert_eq!(secret, "postgres://localhost/test");
}

#[test]
fn rejects_non_string_response_value_without_panicking() {
let response = r#"{
"response_type": "secret",
"response_value": {"database_url": "postgres://localhost/test"}
}"#;

assert!(matches!(
parse_parent_secret_response(response),
Err(Error::SecretParsingError)
));
}

#[test]
fn rejects_truncated_parent_response() {
let response = r#"{"response_type":"secret","response_value":"{}""#;

assert!(matches!(
parse_parent_secret_response(response),
Err(Error::JsonError(_))
));
}

#[test]
fn rejects_unexpected_parent_response_type() {
let response = r#"{"response_type":"credentials","response_value":"{}"}"#;

assert!(matches!(
parse_parent_secret_response(response),
Err(Error::AuthenticationError)
));
}

#[test]
fn rejects_malformed_encoded_secret() {
let response = r#"{"response_type":"secret","response_value":"not json"}"#;

assert!(matches!(
parse_parent_secret_response(response),
Err(Error::JsonError(_))
));
}

#[test]
fn rejects_invalid_secret_object_shapes() {
for response_value in [
"{}",
r#"{"database_url":42}"#,
r#"{"database_url":"first","jwt_secret":"second"}"#,
] {
let response = serde_json::json!({
"response_type": "secret",
"response_value": response_value,
})
.to_string();

assert!(matches!(
parse_parent_secret_response(&response),
Err(Error::SecretParsingError)
));
}
}
Loading