Repository navigation
feat: Have ExtensionBasedMapper handle extensions correctly - #173
Conversation
RubenVerborgh
left a comment
There was a problem hiding this comment.
All nits except for unescaped regex creation.
|
|
||
| if (path.includes('/..')) { | ||
| throw new UnsupportedHttpError('Disallowed /.. segment in URL.'); | ||
| } |
There was a problem hiding this comment.
Would ../ also be a problem?
There was a problem hiding this comment.
I think /.. covers all problem scenarios? I copied this from the NSS.
There was a problem hiding this comment.
I'm fine with that, if we add an assertion that the URL starts with /. Then we also know it contains at least one slash 🙂
(Both should be the case, but malformed requests exist.)
|
|
||
| // Would conflict with how new extensions get stored | ||
| if (/\$\.\w+$/u.test(path)) { | ||
| throw new UnsupportedHttpError('Identifiers should not match /\\$\\.\\w+$/.'); |
There was a problem hiding this comment.
| throw new UnsupportedHttpError('Identifiers should not match /\\$\\.\\w+$/.'); | |
| throw new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension'); |
| const name = path.slice(path.lastIndexOf('/') + 1); | ||
| const folder = path.slice(0, path.length - name.length); |
There was a problem hiding this comment.
| const name = path.slice(path.lastIndexOf('/') + 1); | |
| const folder = path.slice(0, path.length - name.length); | |
| const [, name, folder] = path.match(/^(.*\/)(.*)$/) |
Also, might want an assert earlier up that there is indeed a slash in the path.
There was a problem hiding this comment.
Update: assert for "starts with slash".
| const name = path.slice(path.lastIndexOf('/') + 1); | ||
| const folder = path.slice(0, path.length - name.length); | ||
|
|
||
| let actualName: string | undefined; |
There was a problem hiding this comment.
Use documentName and fileName?
| try { | ||
| const files = await fsPromises.readdir(folder); | ||
| actualName = files.find( | ||
| (file): boolean => new RegExp(`^${name}(?:\\$\\..+)?$`, 'u').test(file), |
There was a problem hiding this comment.
This is definitely problematic because no escaping happens of name.
Also, we should only make the regex once (if we use one).
There was a problem hiding this comment.
Yea true, I initially had a solution with file.startsWith and then slicing of the end and checking that, but then I wanted to be fancy and do it in a single statement. Will revert it back to the previous solution.
| ); | ||
| } catch { | ||
| // Parent folder does not exist (or is not a folder) | ||
| throw new NotFoundHttpError(); |
There was a problem hiding this comment.
Looks like this one is not needed, as it is the same as !actualName.
There was a problem hiding this comment.
This one will be thrown if folder does not exist (or is not a directory). actualName is a file in folder.
There was a problem hiding this comment.
I mean that, when you follow the code path, even if you don't throw the exception here, it will be thrown right below because actualName will be undefined. But no strong opinion; otherwise the catch is just empty and that's also not nice.
| } | ||
|
|
||
| // New file | ||
| if (contentType !== this.getContentTypeFromExtension(path)) { |
There was a problem hiding this comment.
This might need a comment to explain
|
Are we testing percent-encoded URLs as well? So the regular stuff, but also sneaky things like |
Currently the only thing that happens is a call to |
I think it should be
Yeah, no 🙂 Let's not do that. |
The difference is that for |
b458d68 to
d726cbb
Compare
|
So after some more thinking about it I expanded the scope of this PR to also add some URL cleaning to the basic target extractor we currently have (since what happens there impacts the assumptions we're allowed to make later). The URL there now decodes all the percent-encodings that shouldn't really be there. This way we always get the same URL (this is also important for having consistent subjects in the metadata). This also makes sure the |
This resolves #150 and part of #151.
It does not handle #146 yet. That would require more changes in the file store which is not the focus of this PR (and I don't want to do many changes there until #167 is handled).
Several changes were necessary in the file store and the file store tests (some of the mocks broke since the
fscalls aren't in the same order anymore), but the main thing to look at is what happens in theExtensionBasedMapper.It is not fully yet as I would want it. It should only have the 2 public functions from the interface, but some of the other public functions would require more drastic changes to the file store to remove, for which I refer to my comment above.