fix(ui-flow): attachment download timeout and size guard
Race download reads with a 30s timeout and reject oversized inline IPC payloads so the UI does not stay on Downloading indefinitely. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -1394,10 +1394,23 @@ async function dispatchUiMessage (platform, msg, hooks = {}) {
|
||||
}
|
||||
if (t === 'request-attachment-download') {
|
||||
const attachmentId = msg.attachmentId
|
||||
const downloadTimeoutMs = Number(process.env.PEARCORD_ATTACHMENT_DOWNLOAD_MS || 30000)
|
||||
const maxInlineBytes = Number(process.env.PEARCORD_ATTACHMENT_DOWNLOAD_MAX || 6 * 1024 * 1024)
|
||||
try {
|
||||
const row = await platform.attachments?.get(attachmentId)
|
||||
if (!row) throw new Error('attachment not found')
|
||||
const { buf, source } = await platform.readAttachmentBytesWithSource(attachmentId)
|
||||
const { buf, source } = await Promise.race([
|
||||
platform.readAttachmentBytesWithSource(attachmentId),
|
||||
new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new Error('download timed out')), downloadTimeoutMs)
|
||||
})
|
||||
])
|
||||
if (!buf?.length) throw new Error('attachment file empty')
|
||||
if (buf.length > maxInlineBytes) {
|
||||
throw new Error(
|
||||
`file too large to download inline (${Math.round(buf.length / 1024 / 1024)} MB)`
|
||||
)
|
||||
}
|
||||
send({
|
||||
type: 'attachment-download',
|
||||
attachmentId: row.id,
|
||||
|
||||
Reference in New Issue
Block a user