Skip to content

Commit b5f41d4

Browse files
committed
feat(orchestration): gzip location snapshot publishes
A location now sends its snapshot with Content-Encoding: gzip. The server keeps the 64 MiB wire bound and, after authenticating the location token, streams the body through a 512 MiB decompressed-size bound into the JSON parser. Encodings other than gzip get 415, and a body over either bound gets 413. A server that refuses the encoded body with 400 or 415 gets the snapshot again as plain JSON, so new locations still publish to older servers.
1 parent 20c406a commit b5f41d4

11 files changed

Lines changed: 474 additions & 5 deletions

File tree

‎clients/typescript/src/schema.d.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,20 @@ export interface operations {
42294229
};
42304230
content?: never;
42314231
};
4232+
/** @description Body over its wire or decompressed bound */
4233+
413: {
4234+
headers: {
4235+
[name: string]: unknown;
4236+
};
4237+
content?: never;
4238+
};
4239+
/** @description Content encoding other than gzip */
4240+
415: {
4241+
headers: {
4242+
[name: string]: unknown;
4243+
};
4244+
content?: never;
4245+
};
42324246
};
42334247
};
42344248
metadata: {

‎crates/sqlbuild-orchestration/src/api/_helpers/resources/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) mod person_maintenance;
1212
pub(crate) mod person_node_sql;
1313
pub(crate) mod person_operations;
1414
pub(crate) mod person_reads;
15+
pub(crate) mod request_json;
1516
pub(crate) mod router;
1617
pub(crate) mod run_reads;
1718
pub(crate) mod runs;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! JSON request bodies that may arrive gzip-encoded, decoded within a decompressed-size bound.
2+
3+
use std::io::{BufReader, Read};
4+
5+
use axum::http::{HeaderMap, StatusCode, header};
6+
use flate2::read::MultiGzDecoder;
7+
use serde::de::DeserializeOwned;
8+
9+
use crate::api::constants::{GZIP_ENCODING, IDENTITY_ENCODING, JSON_MEDIA_TYPE};
10+
use crate::api::errors::ApiError;
11+
use crate::api::models::BoundedReader;
12+
13+
/// `body` as JSON after its declared encoding; gzip streams into the parser and stops past `limit` bytes.
14+
pub(crate) fn request_json<T: DeserializeOwned>(
15+
headers: &HeaderMap,
16+
body: &[u8],
17+
limit: usize,
18+
) -> Result<T, ApiError> {
19+
if !json_content(headers) {
20+
return Err(rejection(
21+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
22+
"expected an application/json body".into(),
23+
));
24+
}
25+
match encodings(headers)?.as_slice() {
26+
[] => serde_json::from_slice(body).map_err(|error| parse_rejection(&error)),
27+
[encoding] if encoding == GZIP_ENCODING => {
28+
let mut reader = BoundedReader {
29+
inner: MultiGzDecoder::new(body),
30+
remaining: limit,
31+
exceeded: false,
32+
};
33+
let parsed: Result<T, serde_json::Error> =
34+
serde_json::from_reader(BufReader::new(&mut reader));
35+
match parsed {
36+
Ok(value) => Ok(value),
37+
Err(_) if reader.exceeded => Err(rejection(
38+
StatusCode::PAYLOAD_TOO_LARGE,
39+
format!("decompressed body exceeds {limit} bytes"),
40+
)),
41+
Err(error) if error.is_io() => Err(rejection(
42+
StatusCode::BAD_REQUEST,
43+
"request body is not valid gzip".into(),
44+
)),
45+
Err(error) => Err(parse_rejection(&error)),
46+
}
47+
}
48+
_ => Err(rejection(
49+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
50+
"only gzip content encoding is accepted".into(),
51+
)),
52+
}
53+
}
54+
55+
impl<R: Read> Read for BoundedReader<R> {
56+
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
57+
let window = buffer.len().min(self.remaining.saturating_add(1));
58+
let read = self.inner.read(&mut buffer[..window])?;
59+
if read > self.remaining {
60+
self.exceeded = true;
61+
return Err(std::io::Error::other("decompressed body exceeds its bound"));
62+
}
63+
self.remaining -= read;
64+
Ok(read)
65+
}
66+
}
67+
68+
fn json_content(headers: &HeaderMap) -> bool {
69+
let Some(value) = headers.get(header::CONTENT_TYPE) else {
70+
return false;
71+
};
72+
let Ok(text) = value.to_str() else {
73+
return false;
74+
};
75+
let essence: String = text
76+
.split(';')
77+
.next()
78+
.unwrap_or_default()
79+
.trim()
80+
.to_ascii_lowercase();
81+
essence == JSON_MEDIA_TYPE
82+
|| (essence.starts_with("application/") && essence.ends_with("+json"))
83+
}
84+
85+
/// Declared encodings other than identity, in order; an unreadable header is refused.
86+
fn encodings(headers: &HeaderMap) -> Result<Vec<String>, ApiError> {
87+
let mut encodings: Vec<String> = Vec::new();
88+
for value in headers.get_all(header::CONTENT_ENCODING) {
89+
let text = value.to_str().map_err(|_| {
90+
rejection(
91+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
92+
"only gzip content encoding is accepted".into(),
93+
)
94+
})?;
95+
encodings.extend(
96+
text.split(',')
97+
.map(|encoding| encoding.trim().to_ascii_lowercase())
98+
.filter(|encoding| !encoding.is_empty() && encoding != IDENTITY_ENCODING),
99+
);
100+
}
101+
Ok(encodings)
102+
}
103+
104+
/// The statuses axum's JSON extractor uses: malformed JSON is 400, a wrong shape is 422.
105+
fn parse_rejection(error: &serde_json::Error) -> ApiError {
106+
if error.is_data() {
107+
rejection(
108+
StatusCode::UNPROCESSABLE_ENTITY,
109+
format!("Failed to deserialize the JSON body into the target type: {error}"),
110+
)
111+
} else {
112+
rejection(
113+
StatusCode::BAD_REQUEST,
114+
format!("Failed to parse the request body as JSON: {error}"),
115+
)
116+
}
117+
}
118+
119+
fn rejection(status: StatusCode, message: String) -> ApiError {
120+
ApiError {
121+
status,
122+
message,
123+
decision_id: None,
124+
}
125+
}

‎crates/sqlbuild-orchestration/src/api/_helpers/resources/snapshots.rs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//! A location credential publishes and reads only its own location's snapshots.
22
3+
use axum::body::Bytes;
34
use axum::extract::{Path, State};
4-
use axum::http::StatusCode;
5+
use axum::http::{HeaderMap, StatusCode};
56
use axum::{Extension, Json};
67
use uuid::Uuid;
78

89
use crate::api::_helpers::authentication::bearer::require_location;
910
use crate::api::_helpers::resources::docker_image;
11+
use crate::api::_helpers::resources::request_json::request_json;
12+
use crate::api::constants::DECOMPRESSED_BODY_BYTES;
1013
use crate::api::errors::ApiError;
1114
use crate::api::models::{DevelopmentScope, DockerImageSource};
1215
use crate::credentials::models::TokenIdentity;
@@ -22,20 +25,30 @@ use crate::store::models::ControlStore;
2225
request_body = SnapshotInput,
2326
responses((status = 200, body = SnapshotRecord), (status = 400, body = crate::api::models::ApiErrorBody),
2427
(status = 401, description = "Invalid credential"), (status = 403, description = "Location not authorized"),
25-
(status = 409, description = "Stale generation")), security(("bearer" = [])))]
28+
(status = 409, description = "Stale generation"), (status = 413, description = "Body over its wire or decompressed bound"),
29+
(status = 415, description = "Content encoding other than gzip")), security(("bearer" = [])))]
2630
pub(crate) async fn publish(
2731
State(store): State<ControlStore>,
2832
(Extension(identity), Extension(images)): (
2933
Extension<TokenIdentity>,
3034
Extension<std::sync::Arc<Vec<DockerImageSource>>>,
3135
),
32-
(Path((location_id, target)), development): (
36+
(Path((location_id, target)), development, headers): (
3337
Path<(Uuid, String)>,
3438
Option<Extension<DevelopmentScope>>,
39+
HeaderMap,
3540
),
36-
Json(input): Json<SnapshotInput>,
41+
body: Bytes,
3742
) -> Result<Json<SnapshotRecord>, ApiError> {
3843
require_location(&identity, location_id)?;
44+
let input: SnapshotInput =
45+
tokio::task::spawn_blocking(move || request_json(&headers, &body, DECOMPRESSED_BODY_BYTES))
46+
.await
47+
.map_err(|_| ApiError {
48+
status: StatusCode::INTERNAL_SERVER_ERROR,
49+
message: "snapshot decoding stopped".into(),
50+
decision_id: None,
51+
})??;
3952
if input.location_id != location_id || input.target != target {
4053
return Err(ApiError {
4154
status: StatusCode::BAD_REQUEST,

‎crates/sqlbuild-orchestration/src/api/constants.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! Shared HTTP transport bounds for the server and native clients.
22
33
pub const BODY_BYTES: usize = 64 * 1024 * 1024;
4+
/// A gzip request body may decompress to at most this many bytes before it is refused.
5+
pub(crate) const DECOMPRESSED_BODY_BYTES: usize = 512 * 1024 * 1024;
6+
pub(crate) const GZIP_ENCODING: &str = "gzip";
7+
pub(crate) const IDENTITY_ENCODING: &str = "identity";
8+
pub(crate) const JSON_MEDIA_TYPE: &str = "application/json";
49
/// Two 1024-byte passwords can each expand sixfold through JSON escaping.
510
pub(crate) const PASSWORD_BODY_BYTES: usize = 16 * 1024;
611
pub(crate) const PASSWORD_COOKIE: &str = "__Host-sqb-session";

‎crates/sqlbuild-orchestration/src/api/models.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,11 @@ pub(crate) struct UiAsset {
431431
pub(crate) path: &'static str,
432432
pub(crate) body: &'static [u8],
433433
}
434+
435+
/// A decompressing reader that fails once it would yield more than `remaining` bytes.
436+
#[derive(Debug)]
437+
pub(crate) struct BoundedReader<R> {
438+
pub(crate) inner: R,
439+
pub(crate) remaining: usize,
440+
pub(crate) exceeded: bool,
441+
}

‎crates/sqlbuild-orchestration/src/snapshots/main/publish_remote_snapshot.rs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
//! A code location sends retained compiled data over HTTP and never opens the control store.
22
3+
use reqwest::StatusCode;
4+
use reqwest::header::{CONTENT_ENCODING, CONTENT_TYPE};
5+
6+
use crate::api::constants::{GZIP_ENCODING, JSON_MEDIA_TYPE};
37
use crate::api::errors::ClientError;
48
use crate::api::main::client_response::client_response;
59
use crate::api::main::connect_client::connect_client;
610
use crate::snapshots::models::{SnapshotInput, SnapshotRecord};
11+
use crate::store::main::compress::compress;
712

13+
/// Sends the snapshot gzip-encoded, and once more as plain JSON when a server refuses the encoding.
814
pub async fn publish_remote_snapshot(
915
server_url: &str,
1016
bearer: &str,
@@ -19,5 +25,24 @@ pub async fn publish_remote_snapshot(
1925
&input.target,
2026
"snapshots",
2127
])?;
22-
client_response(connection.client.post(url).json(input)).await
28+
let body = serde_json::to_vec(input)
29+
.map_err(|_| ClientError::Configuration("snapshot is not serializable as JSON"))?;
30+
let gzipped =
31+
compress(&body).map_err(|_| ClientError::Configuration("snapshot could not be gzipped"))?;
32+
let request = |body: Vec<u8>| {
33+
connection
34+
.client
35+
.post(url.clone())
36+
.header(CONTENT_TYPE, JSON_MEDIA_TYPE)
37+
.body(body)
38+
};
39+
match client_response(request(gzipped).header(CONTENT_ENCODING, GZIP_ENCODING)).await {
40+
Err(ClientError::Rejected(status))
41+
if status == StatusCode::BAD_REQUEST
42+
|| status == StatusCode::UNSUPPORTED_MEDIA_TYPE =>
43+
{
44+
client_response(request(body)).await
45+
}
46+
result => result,
47+
}
2348
}

‎crates/sqlbuild-orchestration/tests/api/helpers.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,53 @@ pub(crate) fn raw_get(address: &str, path: &str) -> (u16, String) {
101101
.expect("numeric status");
102102
(status, body.to_owned())
103103
}
104+
105+
/// Gzip members that decompress to a JSON string field longer than the decompressed bound.
106+
pub(crate) fn gzip_bomb() -> Vec<u8> {
107+
let filler = crate::store::main::compress::compress(&vec![b'a'; 1024 * 1024]).expect("gzip");
108+
let mut body = crate::store::main::compress::compress(br#"{"padding":""#).expect("gzip");
109+
for _ in 0..=crate::api::constants::DECOMPRESSED_BODY_BYTES / (1024 * 1024) {
110+
body.extend_from_slice(&filler);
111+
}
112+
body
113+
}
114+
115+
/// Plain JSON one byte longer than the wire bound.
116+
pub(crate) fn oversized_json() -> Vec<u8> {
117+
let mut body = br#"{"padding":""#.to_vec();
118+
body.resize(crate::api::constants::BODY_BYTES - 1, b'a');
119+
body.extend_from_slice(br#""}"#);
120+
body
121+
}
122+
123+
pub(crate) fn gzipped_empty_object() -> Vec<u8> {
124+
crate::store::main::compress::compress(b"{}").expect("gzip")
125+
}
126+
127+
pub(crate) fn not_gzip() -> Vec<u8> {
128+
b"{}".to_vec()
129+
}
130+
131+
/// A DuckShop-10k-sized snapshot whose plain JSON is larger than the wire bound.
132+
pub(crate) fn large_snapshot(location_id: uuid::Uuid) -> crate::snapshots::models::SnapshotInput {
133+
let mut input = crate::runs::tests::helpers::snapshot(location_id);
134+
input.nodes = (0..10_282)
135+
.map(|index| {
136+
let sql = format!(
137+
"select {index} as order_id, '{}' as note from orders_{index}",
138+
"x".repeat(3_400)
139+
);
140+
(
141+
format!("model:orders_{index}"),
142+
crate::snapshots::models::SnapshotNode {
143+
kind: "model".into(),
144+
definition: serde_json::json!({"materialization": "table"}),
145+
raw_sql: Some(sql.clone()),
146+
compiled_sql: Some(sql),
147+
python_source: None,
148+
},
149+
)
150+
})
151+
.collect();
152+
input
153+
}

‎crates/sqlbuild-orchestration/tests/api/test_contracts.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ mod test_person_location_work;
7070
mod test_person_sessions;
7171
#[path = "test_proxy.rs"]
7272
mod test_proxy;
73+
#[path = "test_publish_encoding.rs"]
74+
mod test_publish_encoding;
7375
#[path = "test_reads.rs"]
7476
mod test_reads;
7577
#[path = "test_statistics_recovery.rs"]

0 commit comments

Comments
 (0)