Skip to content

feat: Have ExtensionBasedMapper handle extensions correctly - #173

Merged
jvherweg merged 2 commits into
masterfrom
extension-based-mapper
Oct 7, 2020
Merged

jvherweg merged 2 commits into
masterfrom
extension-based-mapper

Conversation

@jvherweg

@jvherweg jvherweg commented Oct 2, 2020

Copy link
Copy Markdown
Member

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 fs calls aren't in the same order anymore), but the main thing to look at is what happens in the ExtensionBasedMapper.

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.

@jvherweg jvherweg added the ☀️ enhancement New feature or request label Oct 2, 2020

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All nits except for unescaped regex creation.


if (path.includes('/..')) {
throw new UnsupportedHttpError('Disallowed /.. segment in URL.');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would ../ also be a problem?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think /.. covers all problem scenarios? I copied this from the NSS.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.)

Comment thread src/storage/ExtensionBasedMapper.ts Outdated

// Would conflict with how new extensions get stored
if (/\$\.\w+$/u.test(path)) {
throw new UnsupportedHttpError('Identifiers should not match /\\$\\.\\w+$/.');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
throw new UnsupportedHttpError('Identifiers should not match /\\$\\.\\w+$/.');
throw new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension');

Comment thread src/storage/ExtensionBasedMapper.ts Outdated
Comment on lines +78 to +79
const name = path.slice(path.lastIndexOf('/') + 1);
const folder = path.slice(0, path.length - name.length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Update: assert for "starts with slash".

Comment thread src/storage/ExtensionBasedMapper.ts Outdated
const name = path.slice(path.lastIndexOf('/') + 1);
const folder = path.slice(0, path.length - name.length);

let actualName: string | undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use documentName and fileName?

Comment thread src/storage/ExtensionBasedMapper.ts Outdated
try {
const files = await fsPromises.readdir(folder);
actualName = files.find(
(file): boolean => new RegExp(`^${name}(?:\\$\\..+)?$`, 'u').test(file),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is definitely problematic because no escaping happens of name.
Also, we should only make the regex once (if we use one).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like this one is not needed, as it is the same as !actualName.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This one will be thrown if folder does not exist (or is not a directory). actualName is a file in folder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)) {

@RubenVerborgh RubenVerborgh Oct 5, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might need a comment to explain

Comment thread src/storage/FileIdentifierMapper.ts Resolved
@RubenVerborgh

Copy link
Copy Markdown
Member

Are we testing percent-encoded URLs as well?

So the regular stuff, but also sneaky things like ../ being percent-encoded.

@jvherweg

jvherweg commented Oct 6, 2020

Copy link
Copy Markdown
Member Author

Are we testing percent-encoded URLs as well?

So the regular stuff, but also sneaky things like ../ being percent-encoded.

Currently the only thing that happens is a call to decodeURI when converting to a filepath and a call to encodeURI when going back to an identifier. This only changes things that would have to be encoded in the first place though. This means that after a call to decodeURI, %24 will still be %24 and not $ since that is a legal URL character so wouldn't have need replacement in the first place (discovered this when I first used %24 in the unit tests since I assumed they would be replaced 😄 ). So the sneaky ../ would not be discovered, but it would also not be changed to something dangerous. The filename would simply also contain %46%46/.

@RubenVerborgh

RubenVerborgh commented Oct 6, 2020 •

Copy link
Copy Markdown
Member

Currently the only thing that happens is a call to decodeURI when converting to a filepath and a call to encodeURI when going back to an identifier.

I think it should be decodeURIComponent and its counterpart.

The filename would simply also contain %46%46/.

Yeah, no 🙂 Let's not do that.

@jvherweg

jvherweg commented Oct 6, 2020

Copy link
Copy Markdown
Member Author

Yeah, no 🙂 Let's not do that.

The difference is that for decodeURIComponent you first have to split on slashes to make sure you have separate components. I'll add that then. Since we do this before doing anything else this would also cause those hidden cases to be caught then.

@jvherweg
jvherweg force-pushed the extension-based-mapper branch from b458d68 to d726cbb Compare October 6, 2020 09:26
@jvherweg

jvherweg commented Oct 6, 2020

Copy link
Copy Markdown
Member Author

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 decodeURI handles those URLs correctly.

@jvherweg
jvherweg requested a review from RubenVerborgh October 6, 2020 09:28

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☀️ enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hide where the resource mapper gets the content type from

2 participants