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: encode filenames #539

Merged
merged 1 commit into from
Oct 25, 2021
Merged

fix: encode filenames #539

merged 1 commit into from
Oct 25, 2021

Conversation

zebateira
Copy link
Contributor

This PR fixes upload not working on the website when the filename includes a non ISO-8859-1 character.
We were missing an encode/decodeURIComponent. This only affects the files because the filename is sent via header X-NAME.
I made the fix in the lib/api.js file on the website code. Should this done on the js client instead?

Fixes #532

@olizilla
Copy link
Contributor

Should this done on the js client instead?

Yes. The client should do the encode. The api should decode it before storing it in the db, and return the decoded version in responses, so the user doesn't see them.

We need to add tests and to document that when using the /car and /upload enpoints directly you need to encode the file name, and that the client will do this for you.

@zebateira are you up for trying out this fix on the api and client? we could pair on it some if that would help?

@zebateira
Copy link
Contributor Author

@olizilla yeah sounds good 👍

@zebateira zebateira force-pushed the fix/filename-encoding branch 6 times, most recently from 7300e67 to b195e5b Compare October 19, 2021 15:34
@zebateira
Copy link
Contributor Author

zebateira commented Oct 19, 2021

Updated:

  • removed logic from website
  • added encoding on js client
  • added decoding on api

In order to have a test on the api, I added the name to the return body:

return new JSONResponse({ cid, name })

I don't think we can test this in another way because of the DB mock right? My intuition was to (1) upload (2) get files list (3) check filename is correct, but since we are not connected to the DB we can't do this?

@zebateira zebateira requested a review from olizilla October 19, 2021 15:39
@zebateira
Copy link
Contributor Author

Right now this can't be tested fully on the preview link because it points to staging, so only after merging will we see this working fully.

Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Code looks good, but I think we should not return the name in create just for testing purposes. Name is obtained in listUploads, so it seems like we are adding a test just because.

With this in mind, I think it would be more useful to just add to the mock response (here) a validation where if we receive an invalid name we throw an error instead of a "good" answer. This way, the test is just sending a name that needs to be encoded and validate it does not make it way to the DB request

@vasco-santos
Copy link
Contributor

@zebateira we should also document this. Not sure where it fits better, but perhaps adding information here and we also need the docs

@zebateira
Copy link
Contributor Author

zebateira commented Oct 20, 2021

@vasco-santos yeah, the api test is not the best.

I think it would be more useful to just add to the mock response (here) a validation where if we receive an invalid name we throw an error instead of a "good" answer.

What do you mean add a validation in the mock response? A validation assertion in the mock response code here?
But, how would that assert that we are running this decoding loc:

let name = xName && decodeURIComponent(xName)

@vasco-santos
Copy link
Contributor

What do you mean add a validation in the mock response? A validation in the mock response code here?

In the mock server for the graphql post, you can access what data is sent in that post, which means you can inspect there the content of name from https://github.com/web3-storage/web3.storage/blob/fix/filename-encoding/packages/api/src/car.js#L167

The two options are:

  • API receives and encoded string (what we did in the client)
  • API receives a not encoded problematic string

So two tests for the API to exercise both of these paths. In the mock server (condition that the performed operation is the createUpload), you can get the content of the name property from the request and do something like:

const encoded = encodeURIComponent(name)
const decoded = decodeURIComponent(encoded)
if (decoded !== name) {
  return gqlResponse(500, {
    errors: [{ message: `error message` }]
  })
}

This will fail when you provide to the API a name such as upload name. So you can have the test check if the request failed.

@zebateira zebateira force-pushed the fix/filename-encoding branch 2 times, most recently from c7a02a2 to 412ea40 Compare October 20, 2021 15:35
@zebateira
Copy link
Contributor Author

zebateira commented Oct 20, 2021

@vasco-santos thank you for the help!
I added the validation to the db mock as you suggested. The test fails if the filename is not decoded.
Let me know if this works better now.

@zebateira zebateira force-pushed the fix/filename-encoding branch from 412ea40 to 1b35fc6 Compare October 20, 2021 15:38
Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Cool, I think it is better now 🎉 just another detail similar but for client testing

Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

This looks good! Can we add docs for this to the API as Oli suggested?

@zebateira zebateira force-pushed the fix/filename-encoding branch from f7019ac to 75f3f12 Compare October 21, 2021 15:23
@zebateira
Copy link
Contributor Author

zebateira commented Oct 21, 2021

Docs added!
Will PR the docs site in a sec. (Here)

@vasco-santos vasco-santos requested review from vasco-santos and removed request for vasco-santos October 25, 2021 16:22
Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

LGTM

@vasco-santos vasco-santos merged commit de01972 into main Oct 25, 2021
@vasco-santos vasco-santos deleted the fix/filename-encoding branch October 25, 2021 16:56
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.

Files with names that contain non ISO-8859-1 code point throw exception
3 participants