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: empty filename should not be treated as string #28

Merged
merged 8 commits into from
Jul 12, 2023

Conversation

edmundhung
Copy link

If you create a FormData object on the browser with empty file input, a default empty file entry (i.e. new File([], '')) would be generated. However, this is presented as an empty string instead when you read it on the server.

You can test it here.

I am not sure where to add this test. Let me know if you have any suggestion.

@changeset-bot
Copy link

changeset-bot bot commented Jan 13, 2023

🦋 Changeset detected

Latest commit: e0b02f3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@remix-run/web-fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -338,6 +338,7 @@ describe('Request', () => {
ogFormData.append('a', 1);
ogFormData.append('b', 2);
ogFormData.append('file', new File(['content'], 'file.txt'));
ogFormData.append('empty-file', new File([], '', { type: 'application/octet-stream' }));
Copy link

@jenseng jenseng Jan 13, 2023

Choose a reason for hiding this comment

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

might be worth adding a comment/link here indicating this is exactly what happens to empty file inputs when constructing the form data set (e.g. native form submission, or new FormData(someForm))

@jenseng
Copy link

jenseng commented Jan 13, 2023

Perhaps a good place to test on the remix side would be in https://github.com/remix-run/remix/blob/main/integration/form-test.ts, i.e. you could a test that's currently expected to fail via test.fail (see other examples in the file), which should then change to passing when this is incorporated into remix

@edmundhung
Copy link
Author

Perhaps a good place to test on the remix side would be in https://github.com/remix-run/remix/blob/main/integration/form-test.ts, i.e. you could a test that's currently expected to fail via test.fail (see other examples in the file), which should then change to passing when this is incorporated into remix

If this is considered a patch, it will be applied to existing remix app anytime the package manager updates it. But I am a bit worry as some users might be relying on this behaviour without knowing it was wrong.

export async function action({ request }) {
  const formData = await request.formData();
  const file = formData.get('file');

  // Case 1: Simply check if file is truthy
  if (file) {
    // ...
  }

  // Case 2: Check if it is an file
  if (file instanceof File) {
    // ...
  }
}

Both cases will be always true after the patch. 😅

@jenseng
Copy link

jenseng commented Jan 13, 2023

Since it's a breaking change from the POV of this package, maybe the changeset should indicate major? That would prevent existing remix versions from picking it up, and you could reliably write a failing test over there.

As far as bumping the web-fetch dep within remix to use this, I don't know if that's big enough to warrant major over there, as it could be argued this is a patchset-level bugfix and is unlikely to actually break anyone 🤷

Comment on lines +415 to +422
ogFormData.append('file', Buffer.from(''), {
// Note: This doesn't work at the moment due to https://github.com/form-data/form-data/issues/412.
// There is a v4 released which has a fix that might handle this but I
// wasn't positive if it had breaking changes that would impact us so we
// can handle an upgrade separately.
filename: '',
contentType: 'application/octet-stream',
});

Choose a reason for hiding this comment

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

@jacob-ebey Would love your eyes here. I think this is the equivalent using the node FormData (using an empty buffer) - but it runs into an issue in the npm form-data package that doesn't support empty string filenames - but that's exactly what web browsers submit:

Screenshot 2023-07-12 at 12 48 55 PM

Since it's not an issue for the bug at hand, I figured we could deal with a form-data PR and/or update separately.

@brophdawg11
Copy link

I don't think this would warrant a major for this repo? It feels like a bug fix to me since we're not spec-compliant currently. There's always a non-zero change someone is relying on buggy behavior.

@brophdawg11 brophdawg11 merged commit cf9ee6f into remix-run:main Jul 12, 2023
11 checks passed
@kentcdodds
Copy link
Member

Thanks! :)

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.

5 participants