Skip to content

Commit bbd92bc

Browse files
committed
Return 400 for invalid UTF-8 multipart fields
1 parent 357b5bb commit bbd92bc

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.43.0
44

5+
- Fix: invalid UTF-8 in multipart text fields now returns `400 Bad Request` instead of `500 Internal Server Error`.
56
- OIDC protected and public paths now respect the site prefix when it is defined.
67
- Fix: OIDC provider metadata refreshes now always happen in the background, and with a timeout. Previously, a slow OIDC provider could prevent SQLPage from handling requests for an arbitrary amount of time.
78
- Fix: forms without submit or reset buttons no longer keep extra bottom spacing.

src/webserver/http_request_info.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,14 @@ async fn extract_text(
273273
.await
274274
.map(|bytes| bytes.data)
275275
.map_err(|e| anyhow!("failed to read form field data: {e}"))?;
276-
Ok(String::from_utf8(data.to_vec())?)
276+
String::from_utf8(data.to_vec()).map_err(|e| {
277+
anyhow!(super::ErrorWithStatus {
278+
status: actix_web::http::StatusCode::BAD_REQUEST,
279+
})
280+
.context(format!(
281+
"could not parse multipart form field as utf-8 text: {e}"
282+
))
283+
})
277284
}
278285

279286
async fn extract_file(

tests/requests/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,34 @@ async fn test_variables_function() -> actix_web::Result<()> {
188188
Ok(())
189189
}
190190

191+
#[actix_web::test]
192+
async fn test_invalid_utf8_multipart_text_field_returns_bad_request() -> actix_web::Result<()> {
193+
let req = get_request_to("/tests/requests/variables.sql")
194+
.await?
195+
.insert_header(("content-type", "multipart/form-data; boundary=1234567890"))
196+
.set_payload(
197+
b"--1234567890\r\n\
198+
Content-Disposition: form-data; name=\"x\"\r\n\
199+
Content-Type: text/plain\r\n\
200+
\r\n\
201+
\xff\r\n\
202+
--1234567890--\r\n"
203+
.as_slice(),
204+
)
205+
.to_srv_request();
206+
let status = match main_handler(req).await {
207+
Ok(resp) => resp.status(),
208+
Err(err) => err.as_response_error().status_code(),
209+
};
210+
211+
assert_eq!(
212+
status,
213+
StatusCode::BAD_REQUEST,
214+
"assertion error, expected 400 bad request on invalid utf8 payload, got {}",
215+
status
216+
);
217+
218+
Ok(())
219+
}
220+
191221
mod webhook_hmac;

0 commit comments

Comments
 (0)