mirror of
https://github.com/andrewyng/openworker.git
synced 2026-11-20 20:37:44 +00:00
Merge pull request #897 from andrewyng/fix/service-attachment-admission
Fix attachments on service clusters
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
//! Authenticated bounded uploads; model inputs are independent of sandbox files.
|
||||
use crate::http::{ApiError, App};
|
||||
use anyhow::ensure;
|
||||
use axum::{extract::Path, Extension, Json};
|
||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||
use openworker_runtime::{
|
||||
attachments::{Part, Upload},
|
||||
attachments::{InspectionUnsupported, Metadata, Part, Upload, CHUNK, MAX_TURN},
|
||||
safety::Phase,
|
||||
store::StateStore,
|
||||
};
|
||||
use serde_json::Value;
|
||||
@@ -15,20 +18,115 @@ pub(crate) async fn upload(
|
||||
let blob = app.store.clone().input_attachment(&sid, &id, request)?;
|
||||
Ok(Json(blob.reply()))
|
||||
}
|
||||
pub(crate) async fn inspect(app: &App, sid: &str, parts: &[Part]) -> Result<Vec<Part>, ApiError> {
|
||||
app.store.input_safety_policy(sid)?;
|
||||
pub(crate) async fn inspect(
|
||||
app: &App,
|
||||
sid: &str,
|
||||
request: &str,
|
||||
parts: &[Part],
|
||||
) -> Result<Vec<Part>, ApiError> {
|
||||
inspect_parts(app, sid, request, parts)
|
||||
.await
|
||||
.map_err(ApiError)
|
||||
}
|
||||
async fn inspect_parts(
|
||||
app: &App,
|
||||
sid: &str,
|
||||
request: &str,
|
||||
parts: &[Part],
|
||||
) -> anyhow::Result<Vec<Part>> {
|
||||
let (session, _) = app.store.input_safety_policy(sid)?;
|
||||
let policy = app.config.safety.merge(&session)?;
|
||||
openworker_runtime::attachments::validate_parts(parts)?;
|
||||
// Check ownership using the request-scoped store before trusted runtime
|
||||
// inspection. Never allow an ID to cross a project/principal boundary.
|
||||
let mut checked = Vec::new();
|
||||
let mut total = 0usize;
|
||||
// API admission uses the authorized request-scoped store. The API runtime
|
||||
// does not own this session's worker lease, so its metadata/inspection path
|
||||
// must not be used here. Never claim or steal a lease just to inspect input.
|
||||
for part in parts {
|
||||
if let Part::Image { attachment_id } | Part::Document { attachment_id } = part {
|
||||
app.store
|
||||
.clone()
|
||||
.input_attachment(sid, attachment_id, Upload::Stat)?;
|
||||
}
|
||||
checked.push(match part {
|
||||
Part::Text { text } => Part::Text {
|
||||
text: crate::service_safety::inspect_attachment_text(app, sid, request, text)
|
||||
.await
|
||||
.map_err(|e| e.0)?,
|
||||
},
|
||||
Part::Image { attachment_id } | Part::Document { attachment_id } => {
|
||||
let blob = app
|
||||
.store
|
||||
.clone()
|
||||
.input_attachment(sid, attachment_id, Upload::Stat)?;
|
||||
ensure!(blob.sealed, "Attachment upload is incomplete");
|
||||
total = total.saturating_add(blob.metadata.size);
|
||||
ensure!(total <= MAX_TURN, "Turn attachment byte limit exceeded");
|
||||
let image = matches!(part, Part::Image { .. });
|
||||
ensure!(
|
||||
if image {
|
||||
blob.metadata.media_type.starts_with("image/")
|
||||
} else {
|
||||
["application/pdf", "text/plain"]
|
||||
.contains(&blob.metadata.media_type.as_str())
|
||||
},
|
||||
"Attachment type mismatch"
|
||||
);
|
||||
let id = if blob.metadata.media_type == "text/plain" {
|
||||
let text = std::str::from_utf8(&blob.bytes)?;
|
||||
let replacement =
|
||||
crate::service_safety::inspect_attachment_text(app, sid, request, text)
|
||||
.await
|
||||
.map_err(|e| e.0)?;
|
||||
if replacement.as_bytes() != blob.bytes {
|
||||
let bytes = replacement.into_bytes();
|
||||
let hash = openworker_runtime::bundle::digest(&bytes);
|
||||
let identity = openworker_runtime::bundle::digest(&serde_json::to_vec(
|
||||
&serde_json::json!([
|
||||
blob.metadata.name,
|
||||
blob.metadata.media_type,
|
||||
hash
|
||||
]),
|
||||
)?);
|
||||
let metadata = Metadata {
|
||||
id: format!("att_{identity}"),
|
||||
sha256: hash,
|
||||
size: bytes.len(),
|
||||
..blob.metadata.clone()
|
||||
};
|
||||
let mut store = app.store.clone();
|
||||
store.input_attachment(
|
||||
sid,
|
||||
&metadata.id,
|
||||
Upload::Begin {
|
||||
metadata: metadata.clone(),
|
||||
},
|
||||
)?;
|
||||
for (i, chunk) in bytes.chunks(CHUNK).enumerate() {
|
||||
store.input_attachment(
|
||||
sid,
|
||||
&metadata.id,
|
||||
Upload::Chunk {
|
||||
offset: i * CHUNK,
|
||||
data: STANDARD.encode(chunk),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
store.input_attachment(sid, &metadata.id, Upload::Commit)?;
|
||||
metadata.id
|
||||
} else {
|
||||
attachment_id.clone()
|
||||
}
|
||||
} else {
|
||||
if policy.protects(Phase::UserInput, None)
|
||||
|| policy.protects(Phase::ModelInput, None)
|
||||
{
|
||||
return Err(InspectionUnsupported.into());
|
||||
}
|
||||
attachment_id.clone()
|
||||
};
|
||||
if image {
|
||||
Part::Image { attachment_id: id }
|
||||
} else {
|
||||
Part::Document { attachment_id: id }
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(app
|
||||
.runtime
|
||||
.prepare_input_content(sid, parts, Some(&app.config.safety))
|
||||
.await?)
|
||||
Ok(checked)
|
||||
}
|
||||
|
||||
@@ -323,7 +323,8 @@ async fn send(
|
||||
None,
|
||||
),
|
||||
(None, Some(parts)) => {
|
||||
let parts = crate::input_attachments::inspect(&app, &sid, &parts).await?;
|
||||
let parts =
|
||||
crate::input_attachments::inspect(&app, &sid, &submission_id, &parts).await?;
|
||||
let text = openworker_runtime::attachments::summary(&parts);
|
||||
(text, Some(parts))
|
||||
}
|
||||
|
||||
@@ -36,6 +36,30 @@ async fn inspect_with(
|
||||
openworker_store_postgres::SubmissionError::InvalidRequest.into(),
|
||||
));
|
||||
}
|
||||
inspect_policy(app, sid, request, text, extra).await
|
||||
}
|
||||
/// UTF-8 documents have their own bounded upload limit, independent of the
|
||||
/// 32 KB conversational-text limit. Admission never claims a worker lease.
|
||||
pub(crate) async fn inspect_attachment_text(
|
||||
app: &App,
|
||||
sid: &str,
|
||||
request: &str,
|
||||
text: &str,
|
||||
) -> Result<String, ApiError> {
|
||||
if text.len() > openworker_runtime::attachments::MAX_FILE {
|
||||
return Err(ApiError(
|
||||
openworker_store_postgres::SubmissionError::InvalidRequest.into(),
|
||||
));
|
||||
}
|
||||
inspect_policy(app, Some(sid), request, text, None).await
|
||||
}
|
||||
async fn inspect_policy(
|
||||
app: &App,
|
||||
sid: Option<&str>,
|
||||
request: &str,
|
||||
text: &str,
|
||||
extra: Option<&Config>,
|
||||
) -> Result<String, ApiError> {
|
||||
let (config, pins) = if let Some(sid) = sid {
|
||||
let (session, pins) = app.store.input_safety_policy(sid)?;
|
||||
(app.config.safety.merge(&session)?, pins)
|
||||
|
||||
@@ -33,7 +33,7 @@ async fn uploads_cross_api_replicas_are_scoped_and_recoverable() {
|
||||
let mut store = PostgresStore::open(db.clone()).unwrap();
|
||||
store.auth_bootstrap(ADMIN, "admin").unwrap();
|
||||
let admin = store.authorized(ADMIN).unwrap();
|
||||
for sid in ["s_one", "s_two"] {
|
||||
for sid in ["s_one", "s_two", "s_three"] {
|
||||
let mut session:Session=serde_json::from_value(json!({"id":sid,"name":"Input","workspace":"/work","coworker":"test","bundle_digest":"bundle","schema_digest":null,"interactions":[],"turn":null,"sandbox":null,"closed":false,"resource_policy":openworker_runtime::resource_policy::Resolved::default()})).unwrap();
|
||||
if sid == "s_two" {
|
||||
session
|
||||
@@ -46,6 +46,20 @@ async fn uploads_cross_api_replicas_are_scoped_and_recoverable() {
|
||||
openworker_runtime::safety::Action::Block,
|
||||
));
|
||||
}
|
||||
if sid == "s_three" {
|
||||
session
|
||||
.safety
|
||||
.guardrails
|
||||
.user_input
|
||||
.push(openworker_runtime::safety::Rule::regex(
|
||||
"redact-secret",
|
||||
"SECRET_[0-9]+",
|
||||
openworker_runtime::safety::Action::Redact {
|
||||
replacement: "[redacted]".into(),
|
||||
keep_last: 0,
|
||||
},
|
||||
));
|
||||
}
|
||||
store.save(&session, "session_created", json!({})).unwrap();
|
||||
}
|
||||
for (token, principal, sid, role) in [
|
||||
@@ -80,7 +94,9 @@ async fn uploads_cross_api_replicas_are_scoped_and_recoverable() {
|
||||
None,
|
||||
None,
|
||||
Default::default(),
|
||||
Some(Box::new(store.clone())),
|
||||
// An API replica does not own the worker's session leases. Sharing the
|
||||
// worker store here concealed admission's accidental lease dependency.
|
||||
Some(Box::new(PostgresStore::open(db.clone()).unwrap())),
|
||||
)
|
||||
.unwrap();
|
||||
let mut urls = vec![];
|
||||
@@ -272,6 +288,70 @@ async fn uploads_cross_api_replicas_are_scoped_and_recoverable() {
|
||||
.unwrap()
|
||||
.input_attachment("s_one", "notes", Upload::Stat)
|
||||
.is_err());
|
||||
// Guardrails still inspect documents and produce immutable replacement
|
||||
// references without acquiring or changing the worker's session lease.
|
||||
let original = b"Document includes SECRET_12345.";
|
||||
let metadata = Metadata {
|
||||
id: "original".into(),
|
||||
name: "secret.txt".into(),
|
||||
media_type: "text/plain".into(),
|
||||
size: original.len(),
|
||||
sha256: openworker_runtime::bundle::digest(original),
|
||||
};
|
||||
for upload in [
|
||||
Upload::Begin { metadata },
|
||||
Upload::Chunk {
|
||||
offset: 0,
|
||||
data: STANDARD.encode(original),
|
||||
},
|
||||
Upload::Commit,
|
||||
] {
|
||||
store
|
||||
.input_attachment("s_three", "original", upload)
|
||||
.unwrap();
|
||||
}
|
||||
let redacted = post(
|
||||
1,
|
||||
ADMIN,
|
||||
"/v1/sessions/s_three/turns".into(),
|
||||
json!({"content":[{"type":"text","text":""},{"type":"document","attachment_id":"original"}]}),
|
||||
"redaction",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let status = redacted.status();
|
||||
let receipt: Value = redacted.json().await.unwrap();
|
||||
assert_eq!(status, 202, "{receipt}");
|
||||
let input = store
|
||||
.submission_input("s_three", receipt["submissionId"].as_str().unwrap())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
input[0],
|
||||
Part::Text {
|
||||
text: String::new()
|
||||
}
|
||||
);
|
||||
let Part::Document { attachment_id } = &input[1] else {
|
||||
panic!("Document ordering changed")
|
||||
};
|
||||
assert_ne!(attachment_id, "original");
|
||||
let blob = admin
|
||||
.clone()
|
||||
.input_attachment("s_three", attachment_id, Upload::Stat)
|
||||
.unwrap();
|
||||
assert_eq!(blob.bytes, b"Document includes [redacted].");
|
||||
assert_eq!(
|
||||
store
|
||||
.input_attachment("s_three", "original", Upload::Stat)
|
||||
.unwrap()
|
||||
.bytes,
|
||||
original
|
||||
);
|
||||
assert!(
|
||||
store.get("s_three").is_ok(),
|
||||
"API inspection must not steal the worker lease"
|
||||
);
|
||||
for server in servers {
|
||||
server.abort();
|
||||
let _ = server.await;
|
||||
|
||||
Reference in New Issue
Block a user