Skip to content

Commit

Permalink
fix(submission): capture split error (#7360)
Browse files Browse the repository at this point in the history
* add test case when filename is null

* fix: check for nullish filename
  • Loading branch information
KenLSM authored Jun 6, 2024
1 parent 81574d4 commit 3f36c10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/app/modules/submission/__tests__/submission.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,30 @@ describe('submission.service', () => {
expect(result._unsafeUnwrapErr()).toEqual(new InvalidFileExtensionError())
})

it('should reject submissions when attachment responses are invalid', async () => {
// Special case where we found instances where the filename was not a string
// See https://www.notion.so/opengov/TypeError-Cannot-read-properties-of-undefined-reading-split-in-file-validation-js-6f4dcc17e6fc48319d8f7f0f997685c2?pvs=4
// We can remove this test case when the issue is found and fixed
const processedResponse1 = generateNewAttachmentResponse({
content: readFileSync('./__tests__/unit/backend/resources/invalid.py'),
filename: 'mock.jpg',
})

processedResponse1.filename = null as unknown as string

// Omit attributes only present in processed fields
const response1 = omit(processedResponse1, [
'isVisible',
'isUserVerified',
])

const result = await SubmissionService.validateAttachments(
[response1],
FormResponseMode.Email,
)
expect(result._unsafeUnwrapErr()).toEqual(new InvalidFileExtensionError())
})

it('should reject submissions when there are invalid file types in zip', async () => {
const processedResponse1 = generateNewAttachmentResponse({
content: readFileSync(
Expand Down
10 changes: 10 additions & 0 deletions src/app/modules/submission/__tests__/submission.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ describe('submission.utils', () => {
})

describe('getInvalidFileExtensions', () => {
it('should throw error when filename is not a string', async () => {
// Special case where we found instances where the filename was not a string
// See https://www.notion.so/opengov/TypeError-Cannot-read-properties-of-undefined-reading-split-in-file-validation-js-6f4dcc17e6fc48319d8f7f0f997685c2?pvs=4
// We can remove this test case when the issue is found and fixed
const promiseOutcome = getInvalidFileExtensions([
{ ...validSingleFile, filename: null as unknown as string },
])
await expect(promiseOutcome).toReject()
})

it('should return empty array when given a single valid file', async () => {
const invalid = await getInvalidFileExtensions([validSingleFile])
expect(invalid).toEqual([])
Expand Down
16 changes: 15 additions & 1 deletion src/app/modules/submission/submission.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,21 @@ export const getInvalidFileExtensions = (
// Turn it into an array of promises that each resolve
// to an array of file extensions that are invalid (if any)
const promises = attachments.map((attachment) => {
const extension = FileValidation.getFileExtension(attachment.filename)
const { filename } = attachment
// Special case where we found instances where the filename was not a string
// See https://www.notion.so/opengov/TypeError-Cannot-read-properties-of-undefined-reading-split-in-file-validation-js-6f4dcc17e6fc48319d8f7f0f997685c2?pvs=4
// We can remove this handling when the issue is found and fixed
if (filename == null) {
logger.error({
message: 'A string is expected, but received null or undefined',
meta: {
action: 'getInvalidFileExtensions',
filename,
},
})
return Promise.reject(new Error('filename is required'))
}
const extension = FileValidation.getFileExtension(filename)
if (FileValidation.isInvalidFileExtension(extension)) {
return Promise.resolve([extension])
}
Expand Down

0 comments on commit 3f36c10

Please sign in to comment.