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

fix: fix attachment file size check differences between FE and BE #6955

Merged
merged 2 commits into from
Dec 8, 2023

Conversation

sebastianwzq
Copy link
Contributor

@sebastianwzq sebastianwzq commented Dec 8, 2023

Problem

We received Invalid answer: AttachmentValidator: File size more than limit on the backend. Given that attachment size is checked on the frontend, this shouldn't happen in normal cases.

The validation condition on frontend and backend is different.

Frontend

       if (
          !IMAGE_UPLOAD_TYPES_TO_COMPRESS.includes(file.type) &&
          maxSize &&
          file.size > maxSize
        ) {
          return {
            code: 'file-too-large',
            message: `You have exceeded the limit, please upload a file below ${readableMaxSize}`,
          }
        }

backend

const makeAttachmentSizeValidator: AttachmentValidatorConstructor =
  (attachmentField) => (response) => {
    const { attachmentSize } = attachmentField
    const byteSizeLimit = parseInt(attachmentSize) * MB
    return response.content.byteLength < byteSizeLimit
      ? right(response)
      : left(`AttachmentValidator:\t File size more than limit`)
  }

Given that limit = 1MB and uploaded file size = 1MB, frontend will allow the upload to go true while backend will reject the upload, resulting in users experiencing 400 errors.

Closes FRM-1571

Solution

Standardised validation conditions across frontend and backend

Breaking Changes

  • Yes - this PR contains breaking changes
    • Details ...
  • No - this PR is backwards compatible

Features:

  • Details ...

Improvements:

  • Details ...

Bug Fixes:

  • Details ...

Before & After Screenshots

BEFORE:

AFTER:

Tests

  • create email mode form with attachment field of 1mb limit
  • run mkfile 1000000 test.txt to create a 1mb file (note that formsg uses 1mb = 1000000 instead of 1024*1024)
  • upload attachment, you should not encounter any errors

Deploy Notes

New environment variables:

  • env var : env var details

New scripts:

  • script : script details

New dependencies:

  • dependency : dependency details

New dev dependencies:

  • dependency : dependency details

@sebastianwzq sebastianwzq changed the title fix: fix file size check differences between FE and BE fix: fix attachment file size check differences between FE and BE Dec 8, 2023
Copy link

linear bot commented Dec 8, 2023

Copy link
Contributor

@justynoh justynoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sebastianwzq sebastianwzq merged commit b0f85ac into develop Dec 8, 2023
31 checks passed
@sebastianwzq sebastianwzq deleted the fix/fix-frontend-backend-file-size-check branch December 8, 2023 07:59
@KenLSM KenLSM mentioned this pull request Dec 11, 2023
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants