Skip to content

Commit fb1360a

Browse files
committed
agent: sanitize draft error messages before persisting
Postgres text columns cannot store strings containing a null byte. This sanitizes error messages by replacing null bytes with the unicode replacement character, so that they can be stored in the database.
1 parent 32fe33b commit fb1360a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

crates/agent/src/draft.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ pub async fn insert_errors(
105105
txn: &mut sqlx::Transaction<'_, sqlx::Postgres>,
106106
) -> anyhow::Result<()> {
107107
for err in errors {
108-
drafts_sql::insert_error(
109-
draft_id,
110-
err.scope.unwrap_or(err.catalog_name),
111-
err.detail,
112-
txn,
113-
)
114-
.await
115-
.context("inserting error")?;
108+
let mut detail = err.detail;
109+
// Replace null bytes with the Unicode replacement character because
110+
// postgres chokes on them.
111+
if detail.contains('\0') {
112+
detail = detail.replace('\0', "\u{FFFD}")
113+
}
114+
drafts_sql::insert_error(draft_id, err.scope.unwrap_or(err.catalog_name), detail, txn)
115+
.await
116+
.context("inserting error")?;
116117
}
117118
Ok(())
118119
}

0 commit comments

Comments
 (0)