Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(auth): cap and rate limit profile picture uploads (#9523)

Eric Hayes5237c5762ad1
diff --git a/apps/web/src/lib/service-clients/service-auth/generated/client.ts b/apps/web/src/lib/service-clients/service-auth/generated/client.ts
index 87c26fb1a9..38d36b9ee0 100644
--- a/apps/web/src/lib/service-clients/service-auth/generated/client.ts
+++ b/apps/web/src/lib/service-clients/service-auth/generated/client.ts
@@ -6987,6 +6987,16 @@ export type createProfilePictureUploadResponse401 = {
status: 401;
};
+export type createProfilePictureUploadResponse413 = {
+ data: ErrorResponse;
+ status: 413;
+};
+
+export type createProfilePictureUploadResponse429 = {
+ data: ErrorResponse;
+ status: 429;
+};
+
export type createProfilePictureUploadResponse500 = {
data: ErrorResponse;
status: 500;
@@ -6999,6 +7009,8 @@ export type createProfilePictureUploadResponseSuccess =
export type createProfilePictureUploadResponseError = (
| createProfilePictureUploadResponse400
| createProfilePictureUploadResponse401
+ | createProfilePictureUploadResponse413
+ | createProfilePictureUploadResponse429
| createProfilePictureUploadResponse500
) & {
headers: Headers;
diff --git a/apps/web/src/lib/service-clients/service-auth/generated/schemas/createProfilePictureUploadRequest.ts b/apps/web/src/lib/service-clients/service-auth/generated/schemas/createProfilePictureUploadRequest.ts
index d1117c22bf..c30fca998b 100644
--- a/apps/web/src/lib/service-clients/service-auth/generated/schemas/createProfilePictureUploadRequest.ts
+++ b/apps/web/src/lib/service-clients/service-auth/generated/schemas/createProfilePictureUploadRequest.ts
@@ -10,4 +10,9 @@ export interface CreateProfilePictureUploadRequest {
content_type: string;
/** File name, such as `avatar.png`. */
file_name: string;
+ /**
+ * Exact size of the image in bytes. Storage rejects a body of any other size.
+ * @minimum 0
+ */
+ size_bytes: number;
}
diff --git a/apps/web/src/lib/service-clients/service-auth/openapi.json b/apps/web/src/lib/service-clients/service-auth/openapi.json
index 05f91dcb91..962f63d98d 100644
--- a/apps/web/src/lib/service-clients/service-auth/openapi.json
+++ b/apps/web/src/lib/service-clients/service-auth/openapi.json
@@ -5998,7 +5998,7 @@
}
},
"400": {
- "description": "Not a supported image type",
+ "description": "Not a supported image type, or empty",
"content": {
"application/json": {
"schema": {
@@ -6017,6 +6017,26 @@
}
}
},
+ "413": {
+ "description": "The image is larger than 16 MB",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
+ "429": {
+ "description": "Too many uploads this hour",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
@@ -6915,7 +6935,7 @@
},
"CreateProfilePictureUploadRequest": {
"type": "object",
- "required": ["file_name", "content_type"],
+ "required": ["file_name", "content_type", "size_bytes"],
"properties": {
"content_type": {
"type": "string",
@@ -6924,6 +6944,12 @@
"file_name": {
"type": "string",
"description": "File name, such as `avatar.png`."
+ },
+ "size_bytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Exact size of the image in bytes. Storage rejects a body of any other size.",
+ "minimum": 0
}
}
},
diff --git a/apps/web/src/lib/service-clients/service-static-files/generated/client.ts b/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
index 38068a9be3..f74f47af23 100644
--- a/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
+++ b/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
@@ -18,6 +18,11 @@ export type putPresignedUrlResponse200 = {
status: 200;
};
+export type putPresignedUrlResponse400 = {
+ data: string;
+ status: 400;
+};
+
export type putPresignedUrlResponse401 = {
data: string;
status: 401;
@@ -32,6 +37,7 @@ export type putPresignedUrlResponseSuccess = putPresignedUrlResponse200 & {
headers: Headers;
};
export type putPresignedUrlResponseError = (
+ | putPresignedUrlResponse400
| putPresignedUrlResponse401
| putPresignedUrlResponse500
) & {
diff --git a/apps/web/src/lib/service-clients/service-static-files/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/index.ts
index f2e3400500..f3370ca46b 100644
--- a/apps/web/src/lib/service-clients/service-static-files/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/index.ts
@@ -12,5 +12,6 @@ export * from './deleteResultError';
export * from './errorResponse';
export * from './fileMetadata';
export * from './putFileRequest';
+export * from './putFileRequestContentLength';
export * from './putFileRequestContentType';
export * from './putFileResponse';
diff --git a/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequest.ts b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequest.ts
index 752bffb35d..5165d2a66e 100644
--- a/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequest.ts
+++ b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequest.ts
@@ -4,9 +4,16 @@
* static_file_service
* OpenAPI spec version: 0.1.0
*/
+import type { PutFileRequestContentLength } from './putFileRequestContentLength';
import type { PutFileRequestContentType } from './putFileRequestContentType';
export interface PutFileRequest {
+ /**
+ * Exact size of the upload in bytes. When set, it is signed into the upload
+URL, so storage rejects a body of any other size.
+ * @minimum 0
+ */
+ content_length?: PutFileRequestContentLength;
/** optional mime type if type cannot be infered from file_name */
content_type?: PutFileRequestContentType;
/** extra metadata to store with file
diff --git a/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequestContentLength.ts b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequestContentLength.ts
new file mode 100644
index 0000000000..1365627048
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-static-files/generated/schemas/putFileRequestContentLength.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * static_file_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * Exact size of the upload in bytes. When set, it is signed into the upload
+URL, so storage rejects a body of any other size.
+ * @minimum 0
+ */
+export type PutFileRequestContentLength = number | null;
diff --git a/apps/web/src/lib/service-clients/service-static-files/openapi.json b/apps/web/src/lib/service-clients/service-static-files/openapi.json
index f655bd020d..9092bcb8dc 100644
--- a/apps/web/src/lib/service-clients/service-static-files/openapi.json
+++ b/apps/web/src/lib/service-clients/service-static-files/openapi.json
@@ -35,6 +35,16 @@
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -400,6 +410,12 @@
"type": "object",
"required": ["file_name"],
"properties": {
+ "content_length": {
+ "type": ["integer", "null"],
+ "format": "int64",
+ "description": "Exact size of the upload in bytes. When set, it is signed into the upload\nURL, so storage rejects a body of any other size.",
+ "minimum": 0
+ },
"content_type": {
"type": ["string", "null"],
"description": "optional mime type if type cannot be infered from file_name"
diff --git a/crates/static_file_service_client/src/put_file.rs b/crates/static_file_service_client/src/put_file.rs
index 5d3d85e9ea..2cfcef82a6 100644
--- a/crates/static_file_service_client/src/put_file.rs
+++ b/crates/static_file_service_client/src/put_file.rs
@@ -15,6 +15,9 @@ pub struct PutFileRequest {
/// extra metadata to store with file
/// don't put anything private in here it is public
pub extension_data: Option<Value>,
+ /// Exact upload size in bytes, signed into the upload URL when set.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub content_length: Option<u64>,
}
// HACK: duplicate logic. put into model crate later
@@ -57,6 +60,7 @@ impl StaticFileServiceClient {
file_name: file_url.to_string(),
content_type: Some(content_type.clone()),
extension_data: None,
+ content_length: None,
},
file_bytes,
&content_type,
@@ -83,6 +87,7 @@ impl StaticFileServiceClient {
file_name: file_name.to_string(),
content_type: Some(content_type.to_string()),
extension_data: None,
+ content_length: None,
},
bytes,
content_type,
@@ -104,6 +109,7 @@ impl StaticFileServiceClient {
file_name: file_name.to_string(),
content_type: Some(content_type.to_string()),
extension_data: None,
+ content_length: None,
},
bytes,
content_type,
@@ -120,6 +126,30 @@ impl StaticFileServiceClient {
owner_id: &str,
file_name: &str,
content_type: &str,
+ ) -> Result<PutFileResponse> {
+ self.reserve(owner_id, file_name, content_type, None).await
+ }
+
+ /// Like [`Self::reserve_for_user`], but storage rejects an upload of any
+ /// size other than `content_length`.
+ #[tracing::instrument(skip(self), err)]
+ pub async fn reserve_sized_for_user(
+ &self,
+ owner_id: &str,
+ file_name: &str,
+ content_type: &str,
+ content_length: u64,
+ ) -> Result<PutFileResponse> {
+ self.reserve(owner_id, file_name, content_type, Some(content_length))
+ .await
+ }
+
+ async fn reserve(
+ &self,
+ owner_id: &str,
+ file_name: &str,
+ content_type: &str,
+ content_length: Option<u64>,
) -> Result<PutFileResponse> {
let full_url = format!("{}/internal/file", self.url);
let res = self
@@ -130,6 +160,7 @@ impl StaticFileServiceClient {
file_name: file_name.to_string(),
content_type: Some(content_type.to_string()),
extension_data: None,
+ content_length,
})
.send()
.await?;
diff --git a/packages/sdk/generated/auth/types.gen.ts b/packages/sdk/generated/auth/types.gen.ts
index 8a7e186245..745a69f720 100644
--- a/packages/sdk/generated/auth/types.gen.ts
+++ b/packages/sdk/generated/auth/types.gen.ts
@@ -354,6 +354,10 @@ export type CreateProfilePictureUploadRequest = {
* File name, such as `avatar.png`.
*/
file_name: string;
+ /**
+ * Exact size of the image in bytes. Storage rejects a body of any other size.
+ */
+ size_bytes: number;
};
/**
@@ -4778,10 +4782,18 @@ export type CreateProfilePictureUploadData = {
export type CreateProfilePictureUploadErrors = {
/**
- * Not a supported image type
+ * Not a supported image type, or empty
*/
400: ErrorResponse;
401: string;
+ /**
+ * The image is larger than 16 MB
+ */
+ 413: ErrorResponse;
+ /**
+ * Too many uploads this hour
+ */
+ 429: ErrorResponse;
500: ErrorResponse;
};
diff --git a/packages/sdk/generated/static-files/types.gen.ts b/packages/sdk/generated/static-files/types.gen.ts
index 1f0e458c24..7e929ad40d 100644
--- a/packages/sdk/generated/static-files/types.gen.ts
+++ b/packages/sdk/generated/static-files/types.gen.ts
@@ -91,6 +91,11 @@ export type FileMetadata = {
};
export type PutFileRequest = {
+ /**
+ * Exact size of the upload in bytes. When set, it is signed into the upload
+ * URL, so storage rejects a body of any other size.
+ */
+ content_length?: number | null;
/**
* optional mime type if type cannot be infered from file_name
*/
@@ -129,6 +134,7 @@ export type PutPresignedUrlData = {
};
export type PutPresignedUrlErrors = {
+ 400: string;
401: string;
500: string;
};
diff --git a/packages/sdk/package.json b/packages/sdk/package.json
index b1fa10a8d2..4d6d702f31 100644
--- a/packages/sdk/package.json
+++ b/packages/sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "@macro-inc/sdk",
- "version": "0.2.2",
+ "version": "0.3.0",
"repository": {
"type": "git",
"url": "git+https://github.com/macro-inc/macro.git",
diff --git a/packages/sdk/specs/auth.json b/packages/sdk/specs/auth.json
index 05f91dcb91..962f63d98d 100644
--- a/packages/sdk/specs/auth.json
+++ b/packages/sdk/specs/auth.json
@@ -5998,7 +5998,7 @@
}
},
"400": {
- "description": "Not a supported image type",
+ "description": "Not a supported image type, or empty",
"content": {
"application/json": {
"schema": {
@@ -6017,6 +6017,26 @@
}
}
},
+ "413": {
+ "description": "The image is larger than 16 MB",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
+ "429": {
+ "description": "Too many uploads this hour",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
@@ -6915,7 +6935,7 @@
},
"CreateProfilePictureUploadRequest": {
"type": "object",
- "required": ["file_name", "content_type"],
+ "required": ["file_name", "content_type", "size_bytes"],
"properties": {
"content_type": {
"type": "string",
@@ -6924,6 +6944,12 @@
"file_name": {
"type": "string",
"description": "File name, such as `avatar.png`."
+ },
+ "size_bytes": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Exact size of the image in bytes. Storage rejects a body of any other size.",
+ "minimum": 0
}
}
},
diff --git a/packages/sdk/specs/static-files.json b/packages/sdk/specs/static-files.json
index f655bd020d..9092bcb8dc 100644
--- a/packages/sdk/specs/static-files.json
+++ b/packages/sdk/specs/static-files.json
@@ -35,6 +35,16 @@
}
}
},
+ "400": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"401": {
"description": "",
"content": {
@@ -400,6 +410,12 @@
"type": "object",
"required": ["file_name"],
"properties": {
+ "content_length": {
+ "type": ["integer", "null"],
+ "format": "int64",
+ "description": "Exact size of the upload in bytes. When set, it is signed into the upload\nURL, so storage rejects a body of any other size.",
+ "minimum": 0
+ },
"content_type": {
"type": ["string", "null"],
"description": "optional mime type if type cannot be infered from file_name"
diff --git a/packages/sdk/src/entities/me/namespace.ts b/packages/sdk/src/entities/me/namespace.ts
index da8f20007b..9768fa9c39 100644
--- a/packages/sdk/src/entities/me/namespace.ts
+++ b/packages/sdk/src/entities/me/namespace.ts
@@ -2,9 +2,6 @@ import { MacroError, unwrap } from '../../utils';
import type { MacroClient } from '../../utils/client';
import { StaticImage } from '../images/image';
-/** The largest profile picture, matching the web app's limit. */
-const MAX_PROFILE_PICTURE_BYTES = 16 * 1000 * 1000;
-
/** How to store an uploaded profile picture. */
export interface UploadProfilePictureOptions {
/** File name, such as `avatar.png`. */
@@ -27,22 +24,19 @@ export class MeNamespace {
/**
* Upload an image to use as your profile picture. This only stores it; pass
- * the result to {@link MeNamespace.update} to start using it.
+ * the result to {@link MeNamespace.update} to start using it. Pictures can be
+ * at most 16 MB, and each user can start 20 uploads an hour.
*/
async uploadProfilePicture(
data: Blob | Uint8Array<ArrayBuffer>,
options: UploadProfilePictureOptions,
): Promise<StaticImage> {
- const size = data instanceof Blob ? data.size : data.byteLength;
- if (size > MAX_PROFILE_PICTURE_BYTES) {
- throw new MacroError('profile picture is larger than 16 MB');
- }
-
const { file_id, upload_url } = unwrap(
await this.client.auth.createProfilePictureUpload({
body: {
file_name: options.fileName,
content_type: options.contentType,
+ size_bytes: data instanceof Blob ? data.size : data.byteLength,
},
}),
);
diff --git a/services/authentication_service/AGENTS.md b/services/authentication_service/AGENTS.md
index 5813a63e03..fe0bbc37e3 100644
--- a/services/authentication_service/AGENTS.md
+++ b/services/authentication_service/AGENTS.md
@@ -98,7 +98,9 @@ inputs cannot select an agent, and ordinary user credentials remain unattributed
- `POST /user/agent_setup` joins the inviting team and replaces the
`Agent signup` API key. It is repeatable until `tutorialComplete` is true.
- `POST /user/profile_picture/file` reserves an SFS upload for API-key callers,
- since `static_file_service` cannot check keys. `PUT` on that path takes the SFS
+ since `static_file_service` cannot check keys. It caps a picture at 16 MB and a
+ user at 20 uploads an hour, and signs the declared size into the upload URL so
+ storage rejects any other size. `PUT` on that path takes the SFS
id and accepts only the caller's PNG, JPEG, WebP, or GIF; it does not wait for
the asynchronous upload event.
diff --git a/services/authentication_service/src/api/user/put_profile_picture_file.rs b/services/authentication_service/src/api/user/put_profile_picture_file.rs
index b50026d248..e3ae53a562 100644
--- a/services/authentication_service/src/api/user/put_profile_picture_file.rs
+++ b/services/authentication_service/src/api/user/put_profile_picture_file.rs
@@ -4,7 +4,15 @@ use macro_db_client::user::update_profile_picture::update_profile_picture;
use model::response::{EmptyResponse, ErrorResponse};
use models_sfs::FileMetadata;
-use crate::api::context::{ApiContext, AuthorizationService};
+use rate_limit::domain::{
+ models::{RateLimitConfig, RateLimitKey},
+ ports::RateLimitService,
+};
+
+use crate::api::{
+ context::{ApiContext, AuthorizationService},
+ middleware::rate_limit::RATE_LIMIT_ENABLED,
+};
#[cfg(test)]
mod test;
@@ -13,6 +21,13 @@ mod test;
/// carry scripts and external references.
const PROFILE_PICTURE_TYPES: [&str; 4] = ["image/png", "image/jpeg", "image/webp", "image/gif"];
+/// The largest profile picture, matching the web app's limit.
+const MAX_PROFILE_PICTURE_BYTES: u64 = 16 * 1000 * 1000;
+
+/// Uploads one user may start per hour. Pictures rarely change, so this only
+/// stops a key from filling storage.
+const PROFILE_PICTURE_UPLOADS_PER_HOUR: u64 = 20;
+
#[derive(serde::Serialize, serde::Deserialize, Debug, utoipa::ToSchema)]
pub struct PutProfilePictureFileRequest {
/// Id of an image the caller uploaded to static file storage.
@@ -66,6 +81,33 @@ pub struct CreateProfilePictureUploadRequest {
pub file_name: String,
/// Mime type of the image.
pub content_type: String,
+ /// Exact size of the image in bytes. Storage rejects a body of any other size.
+ pub size_bytes: u64,
+}
+
+/// Why an upload cannot be reserved.
+#[derive(Debug, PartialEq, Eq)]
+enum ProfilePictureUploadError {
+ UnsupportedType,
+ Empty,
+ TooLarge,
+}
+
+fn validate_profile_picture_upload(
+ content_type: &str,
+ size_bytes: u64,
+) -> Result<(), ProfilePictureUploadError> {
+ if !PROFILE_PICTURE_TYPES.contains(&content_type) {
+ return Err(ProfilePictureUploadError::UnsupportedType);
+ }
+ if size_bytes == 0 {
+ return Err(ProfilePictureUploadError::Empty);
+ }
+ if size_bytes > MAX_PROFILE_PICTURE_BYTES {
+ return Err(ProfilePictureUploadError::TooLarge);
+ }
+
+ Ok(())
}
/// Reserves storage for a new profile picture. Static file storage has no
@@ -77,36 +119,89 @@ pub struct CreateProfilePictureUploadRequest {
request_body = CreateProfilePictureUploadRequest,
responses(
(status = 200, body = ProfilePictureUpload),
- (status = 400, body = ErrorResponse, description = "Not a supported image type"),
+ (status = 400, body = ErrorResponse, description = "Not a supported image type, or empty"),
(status = 401, body = String),
+ (status = 413, body = ErrorResponse, description = "The image is larger than 16 MB"),
+ (status = 429, body = ErrorResponse, description = "Too many uploads this hour"),
(status = 500, body = ErrorResponse),
),
)]
#[tracing::instrument(
skip_all,
- fields(user_id = %authorization.authorization.user.macro_user_id),
+ fields(
+ user_id = %authorization.authorization.user.macro_user_id,
+ size_bytes = req.size_bytes,
+ rate_limited = tracing::field::Empty,
+ ),
)]
pub async fn create_upload_handler(
State(ctx): State<ApiContext>,
authorization: MacroAuthorizationExtractor<AuthorizationService, UserOrInternal>,
Json(req): Json<CreateProfilePictureUploadRequest>,
) -> Result<Json<ProfilePictureUpload>, (StatusCode, Json<ErrorResponse<'static>>)> {
- if !PROFILE_PICTURE_TYPES.contains(&req.content_type.as_str()) {
- return Err(error(StatusCode::BAD_REQUEST, "not a supported image type"));
- }
+ validate_profile_picture_upload(&req.content_type, req.size_bytes).map_err(|e| match e {
+ ProfilePictureUploadError::UnsupportedType => {
+ error(StatusCode::BAD_REQUEST, "not a supported image type")
+ }
+ ProfilePictureUploadError::Empty => error(StatusCode::BAD_REQUEST, "the image is empty"),
+ ProfilePictureUploadError::TooLarge => error(
+ StatusCode::PAYLOAD_TOO_LARGE,
+ "profile pictures can be at most 16 MB",
+ ),
+ })?;
+
+ let user_id = authorization.authorization.user.macro_user_id.as_ref();
+
+ let ticket = if RATE_LIMIT_ENABLED {
+ let key = RateLimitKey::builder(&"profile-picture-upload")
+ .append(&user_id)
+ .finish();
+ let config = RateLimitConfig::new(
+ PROFILE_PICTURE_UPLOADS_PER_HOUR,
+ std::time::Duration::from_secs(60 * 60),
+ );
+ let result = ctx
+ .rate_limit_service
+ .check_rate_limit(key, config)
+ .await
+ .map_err(|e| {
+ tracing::error!(error=?e, "unable to check profile picture rate limit");
+ error(StatusCode::INTERNAL_SERVER_ERROR, "unable to start upload")
+ })?;
+ tracing::Span::current().record("rate_limited", result.is_err());
+ let ticket = result.map_err(|_| {
+ error(
+ StatusCode::TOO_MANY_REQUESTS,
+ "too many profile picture uploads, try again later",
+ )
+ })?;
+ Some(ticket)
+ } else {
+ None
+ };
let reserved = ctx
.static_file_service_client
- .reserve_for_user(
- authorization.authorization.user.macro_user_id.as_ref(),
- &req.file_name,
- &req.content_type,
- )
- .await
- .map_err(|e| {
+ .reserve_sized_for_user(user_id, &req.file_name, &req.content_type, req.size_bytes)
+ .await;
+ let reserved = match reserved {
+ Ok(reserved) => reserved,
+ Err(e) => {
tracing::error!(error=?e, "unable to reserve profile picture upload");
- error(StatusCode::INTERNAL_SERVER_ERROR, "unable to start upload")
- })?;
+ if let Some(ticket) = ticket {
+ // Our failure should not spend the caller's allowance.
+ let _ = ctx
+ .rate_limit_service
+ .rollback_ticket(ticket)
+ .await
+ .inspect_err(|e| tracing::warn!(error=?e, "unable to roll back rate limit"));
+ }
+ return Err(error(
+ StatusCode::INTERNAL_SERVER_ERROR,
+ "unable to start upload",
+ ));
+ }
+ };
Ok(Json(ProfilePictureUpload {
file_id: reserved.id,
diff --git a/services/authentication_service/src/api/user/put_profile_picture_file/test.rs b/services/authentication_service/src/api/user/put_profile_picture_file/test.rs
index c7eeb4e9a1..f9e1d14ebe 100644
--- a/services/authentication_service/src/api/user/put_profile_picture_file/test.rs
+++ b/services/authentication_service/src/api/user/put_profile_picture_file/test.rs
@@ -1,6 +1,9 @@
use models_sfs::FileMetadata;
-use super::{ProfilePictureFileError, validate_profile_picture_file};
+use super::{
+ MAX_PROFILE_PICTURE_BYTES, ProfilePictureFileError, ProfilePictureUploadError,
+ validate_profile_picture_file, validate_profile_picture_upload,
+};
const CALLER: &str = "macro|[email protected]";
@@ -41,3 +44,35 @@ fn svg_and_other_files_are_rejected() {
);
}
}
+
+#[test]
+fn an_image_at_the_size_limit_can_be_uploaded() {
+ assert_eq!(
+ validate_profile_picture_upload("image/png", MAX_PROFILE_PICTURE_BYTES),
+ Ok(())
+ );
+}
+
+#[test]
+fn an_image_over_the_size_limit_is_rejected() {
+ assert_eq!(
+ validate_profile_picture_upload("image/png", MAX_PROFILE_PICTURE_BYTES + 1),
+ Err(ProfilePictureUploadError::TooLarge)
+ );
+}
+
+#[test]
+fn an_empty_upload_is_rejected() {
+ assert_eq!(
+ validate_profile_picture_upload("image/png", 0),
+ Err(ProfilePictureUploadError::Empty)
+ );
+}
+
+#[test]
+fn an_unsupported_type_is_rejected_before_its_size() {
+ assert_eq!(
+ validate_profile_picture_upload("image/svg+xml", MAX_PROFILE_PICTURE_BYTES + 1),
+ Err(ProfilePictureUploadError::UnsupportedType)
+ );
+}
diff --git a/services/static_file_service/AGENTS.md b/services/static_file_service/AGENTS.md
index b40a1f3e04..38d3f0c275 100644
--- a/services/static_file_service/AGENTS.md
+++ b/services/static_file_service/AGENTS.md
@@ -25,6 +25,11 @@ handle upload.
- Not here: what entity a file is attached to (document, chat, etc.) — callers
pass `models_sfs` references; this service doesn't know its callers' domains.
+## Upload size
+
+- A presigned URL only limits size when the caller sets `content_length`, which
+ signs that exact size into the URL. Without it, an upload can be any size.
+
## Checks
- `cargo test -p static_file_service` (`SQLX_OFFLINE` unset).
diff --git a/services/static_file_service/src/api/file/put_presigned_url.rs b/services/static_file_service/src/api/file/put_presigned_url.rs
index 2c95c6c2d5..b1399f5440 100644
--- a/services/static_file_service/src/api/file/put_presigned_url.rs
+++ b/services/static_file_service/src/api/file/put_presigned_url.rs
@@ -18,6 +18,7 @@ use uuid::Uuid;
request_body = PutFileRequest,
responses(
(status = 200, body=PutFileResponse),
+ (status = 400, body=String),
(status = 401, body=String),
(status = 500, body=String),
),
@@ -28,6 +29,14 @@ pub async fn put_presigned_url(
user: MacroAuthorizationExtractor<AuthorizationService, UserOrInternal>,
Json(request): Json<PutFileRequest>,
) -> Result<Response, Response> {
+ // Checked before any metadata is written, so a rejected request leaves no row.
+ if request
+ .content_length
+ .is_some_and(|length| i64::try_from(length).is_err())
+ {
+ return Err((StatusCode::BAD_REQUEST, "content length is out of range").into_response());
+ }
+
let content_type = if let Some(content_type) = request.content_type {
Ok(content_type)
} else {
@@ -47,6 +56,8 @@ pub async fn put_presigned_url(
})
}?;
+ let request_content_length = request.content_length;
+
let id: String = Uuid::new_v4().to_string();
let static_file_key = s3_key::StaticFileKey::new(&id);
let s3_key = static_file_key.to_key();
@@ -75,7 +86,7 @@ pub async fn put_presigned_url(
let upload_url = ctx
.storage_client
- .put_presigned_url(s3_key.clone(), content_type.clone())
+ .put_presigned_url(s3_key.clone(), content_type.clone(), request_content_length)
.await
.map_err(|err| {
tracing::error!(error=?err, "could not create presigned url");
diff --git a/services/static_file_service/src/model/api.rs b/services/static_file_service/src/model/api.rs
index ba71765844..f23a4d0f5a 100644
--- a/services/static_file_service/src/model/api.rs
+++ b/services/static_file_service/src/model/api.rs
@@ -13,6 +13,10 @@ pub struct PutFileRequest {
/// extra metadata to store with file
/// don't put anything private in here it is public
pub extension_data: Option<Value>,
+ /// Exact size of the upload in bytes. When set, it is signed into the upload
+ /// URL, so storage rejects a body of any other size.
+ #[serde(default)]
+ pub content_length: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
diff --git a/services/static_file_service/src/service/s3/client.rs b/services/static_file_service/src/service/s3/client.rs
index 13b8039bad..022fecca92 100644
--- a/services/static_file_service/src/service/s3/client.rs
+++ b/services/static_file_service/src/service/s3/client.rs
@@ -17,13 +17,23 @@ impl S3Client {
}
#[tracing::instrument(skip(self))]
- pub async fn put_presigned_url(&self, key: String, content_type: String) -> Result<String> {
+ pub async fn put_presigned_url(
+ &self,
+ key: String,
+ content_type: String,
+ content_length: Option<u64>,
+ ) -> Result<String> {
let duration = Duration::from_secs(2 * 60);
+ let content_length = content_length
+ .map(i64::try_from)
+ .transpose()
+ .context("content length is out of range")?;
let presigned_url = self
.inner
.put_object()
.key(key)
.content_type(content_type)
+ .set_content_length(content_length)
.bucket(self.storage_bucket.clone())
.presigned(PresigningConfig::expires_in(duration)?)
.await
@@ -198,3 +208,43 @@ impl S3Client {
all_results
}
}
+
+#[cfg(test)]
+mod test {
+ use aws_sdk_s3::config::{BehaviorVersion, Credentials, Region};
+
+ use super::*;
+
+ fn client() -> S3Client {
+ let config = aws_sdk_s3::Config::builder()
+ .behavior_version(BehaviorVersion::latest())
+ .region(Region::new("us-east-1"))
+ .credentials_provider(Credentials::new("id", "secret", None, None, "test"))
+ .build();
+ S3Client::new(aws_sdk_s3::Client::from_conf(config), "bucket".to_string())
+ }
+
+ #[tokio::test]
+ async fn a_declared_length_is_signed_so_storage_rejects_other_sizes() {
+ let url = client()
+ .put_presigned_url("key".to_string(), "image/png".to_string(), Some(5))
+ .await
+ .unwrap();
+
+ assert!(
+ url.to_lowercase()
+ .contains("x-amz-signedheaders=content-length"),
+ "{url}"
+ );
+ }
+
+ #[tokio::test]
+ async fn no_declared_length_leaves_the_size_unsigned() {
+ let url = client()
+ .put_presigned_url("key".to_string(), "image/png".to_string(), None)
+ .await
+ .unwrap();
+
+ assert!(!url.to_lowercase().contains("content-length"), "{url}");
+ }
+}