Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More explicit error messages when uploading corrupt images #3249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/bsky/tests/views/posts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { forSnapshot, stripViewerFromPost } from '../_util'
import { RecordEmbed, VideoEmbed } from '../../src/views/types'
import { RecordWithMedia } from '../../dist/views/types'
import { ids } from '../../src/lexicon/lexicons'
import { XRPCError } from '@atproto/xrpc'

describe('pds posts views', () => {
let network: TestNetwork
Expand Down Expand Up @@ -189,4 +190,22 @@ describe('pds posts views', () => {
expect(data.posts.length).toBe(1)
expect(forSnapshot(data.posts[0])).toMatchSnapshot()
})

it('upload malformed svg', async () => {
try {
await pdsAgent.api.com.atproto.repo.uploadBlob(
Buffer.from('<svgQ'),
{
headers: sc.getHeaders(sc.dids.alice),
encoding: 'text/html',
},
)
throw new Error('Didnt throw')
} catch (e) {
expect(e).toBeInstanceOf(XRPCError)
expect((e as XRPCError).success).toBeFalsy()
expect((e as XRPCError).error).toBe("InvalidRequest")
expect((e as XRPCError).message).toBe('Image detected but content is unparsable')
}
})
})
5 changes: 4 additions & 1 deletion packages/pds/src/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Readable } from 'stream'
import { pipeline } from 'stream/promises'
import sharp from 'sharp'
import { errHasMsg } from '@atproto/common'
import { InvalidRequestError } from '@atproto/xrpc-server'

export async function maybeGetInfo(
stream: Readable,
Expand All @@ -15,10 +16,12 @@ export async function maybeGetInfo(
])
metadata = result
} catch (err) {
// If the buffer doesn't have any identifiable image in it, return null
if (errHasMsg(err, 'Input buffer contains unsupported image format')) {
return null
}
throw err
// Otherwise, the buffer has a corrupt image in it
throw new InvalidRequestError("Image detected but content is unparsable")
}
const { size, height, width, format } = metadata
if (
Expand Down