Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add gzip magic byte check (#2159)

Leendert de Borst8b854bb9e2af
diff --git a/core/rust/src/vault_codec/compress.rs b/core/rust/src/vault_codec/compress.rs
index ff0bbe2a1..e7cdd0137 100644
--- a/core/rust/src/vault_codec/compress.rs
+++ b/core/rust/src/vault_codec/compress.rs
@@ -29,6 +29,20 @@ pub fn gunzip_to_string(bytes: &[u8]) -> VaultResult<String> {
Ok(out)
}
+/// gzip magic bytes (RFC 1952). A gzip stream always begins with these two bytes; UTF-8 JSON never
+/// does (it starts with `{`, `[`, or whitespace), so their presence unambiguously flags gzip.
+pub const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];
+
+/// Decompress a packed payload to its envelope JSON string, supporting both gzipped and plain values:
+/// gzip (leading `1f 8b`) is gunzipped; anything else is treated as raw, uncompressed UTF-8 JSON.
+pub fn decompress_to_string(bytes: &[u8]) -> VaultResult<String> {
+ if bytes.starts_with(&GZIP_MAGIC) {
+ return gunzip_to_string(bytes);
+ }
+ String::from_utf8(bytes.to_vec())
+ .map_err(|e| VaultError::General(format!("uncompressed payload is not valid UTF-8: {}", e)))
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/core/rust/src/vault_codec/mod.rs b/core/rust/src/vault_codec/mod.rs
index 6e69f7b54..40215c134 100644
--- a/core/rust/src/vault_codec/mod.rs
+++ b/core/rust/src/vault_codec/mod.rs
@@ -89,9 +89,9 @@ pub fn pack_payload(payload_json: &str) -> VaultResult<Vec<u8>> {
compress::gzip(envelope_json.as_bytes())
}
-/// Unpack a payload: gunzip > parse envelope > verify the embedded content hash.
+/// Unpack a payload: decompress (gzip or plain JSON) > parse envelope > verify the embedded content hash.
pub fn unpack_payload(plain_bytes: &[u8]) -> VaultResult<String> {
- let envelope_json = compress::gunzip_to_string(plain_bytes)?;
+ let envelope_json = compress::decompress_to_string(plain_bytes)?;
let envelope: serde_json::Value = serde_json::from_str(&envelope_json)?;
if !envelope.get("schemaVersion").map(|v| v.is_number()).unwrap_or(false) {
diff --git a/core/rust/src/vault_codec/tests.rs b/core/rust/src/vault_codec/tests.rs
index b416e880a..22003df08 100644
--- a/core/rust/src/vault_codec/tests.rs
+++ b/core/rust/src/vault_codec/tests.rs
@@ -189,6 +189,20 @@ fn pack_unpack_roundtrip_verifies_content_hash() {
assert_eq!(unpacked_val, payload);
}
+#[test]
+fn unpack_payload_accepts_uncompressed_raw_json() {
+ // Plain (uncompressed) value: the envelope is raw UTF-8 JSON with no gzip magic bytes.
+ // unpack_payload supports both gzipped and plain values, so this must round-trip.
+ let payload = json!({ "schemaVersion": 1, "tables": { "Items": [] }, "userSalt": "abcd" });
+ let content_hash = hash::content_hash(&payload);
+ let envelope = json!({ "schemaVersion": 1, "contentHash": content_hash, "payload": payload });
+ let raw = serde_json::to_string(&envelope).unwrap().into_bytes();
+ assert_ne!(&raw[0..2], &[0x1f, 0x8b]); // sanity: no gzip magic present
+ let unpacked = unpack_payload(&raw).unwrap();
+ let unpacked_val: serde_json::Value = serde_json::from_str(&unpacked).unwrap();
+ assert_eq!(unpacked_val, payload);
+}
+
#[test]
fn unpack_payload_rejects_tampered_payload() {
let payload = json!({ "a": 1 });