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

Handle query params correctly for client app urls with no trailing slash #8120

Conversation

muffinresearch
Copy link
Contributor

@muffinresearch muffinresearch commented Jun 3, 2019

Fixes mozilla/addons#13207

From local testing (to exercise both the prefix and the trailing slash middleware) this results in the following:

/android?foo=1 => /en-US/android/?foo=1
/en-US/android?foo=1 => /en-US/android/?foo=1
/en-US?foo=1 => /en-US/firefox/?foo=1

@muffinresearch
Copy link
Contributor Author

I'll add some tests for locale only urls to this.

@willdurand willdurand self-assigned this Jun 4, 2019
@codecov-io
Copy link

codecov-io commented Jun 4, 2019

Codecov Report

Merging #8120 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   mozilla/addons-frontend#8120      +/-   ##
=========================================
+ Coverage   98.09%   98.1%   +<.01%     
=========================================
  Files         260     260              
  Lines        7363    7372       +9     
  Branches     1331    1333       +2     
=========================================
+ Hits         7223    7232       +9     
  Misses        126     126              
  Partials       14      14
Impacted Files Coverage Δ
src/core/middleware/prefixMiddleware.js 100% <100%> (ø) ⬆️
src/amo/components/SearchFilters/index.js 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32c493b...15777fd. Read the comment docs.

@muffinresearch
Copy link
Contributor Author

muffinresearch commented Jun 4, 2019

@willdurand this is ready for another look.

Copy link
Member

@willdurand willdurand left a comment

Choose a reason for hiding this comment

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

r+wc

@@ -11,12 +11,14 @@ import { getLanguage, isValidLang } from 'core/i18n/utils';
import log from 'core/logger';

export function prefixMiddleware(req, res, next, { _config = config } = {}) {
const URLParts = req.originalUrl.split('?');
Copy link
Member

Choose a reason for hiding this comment

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

Could we name the two different parts?

const [path, queryString] = req.originalUrl.split('?');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about doing that, but it assumes there's 2 parts, and that's not an unreasonable assumption, but if the url had multiple ? we'd lose the later ones using destructuring. Should we throwaway data if a url contains that mistake?

See below for a possible solution.

isApplicationFromHeader = true;
}

// Redirect to the new URL.
// For safety we'll deny a redirect to a URL starting with '//' since
// that will be treated as a protocol-free URL.
const newURL = `/${URLParts.join('/')}`;
URLParts[0] = `/${URLPathParts.join('/')}`;
const newURL = URLParts.join('?');
Copy link
Member

@willdurand willdurand Jun 4, 2019

Choose a reason for hiding this comment

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

If we have named variables for each url part, then we could build the url as is (I think):

const newURL = [`/${URLParts.join('/')}`, queryString].join('?');

This reads a bit better, no?

Copy link
Contributor Author

@muffinresearch muffinresearch Jun 4, 2019

Choose a reason for hiding this comment

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

Except that in the case of there being no queryString you need to check for it being undefined via the destructuring.

Both of the issues with destructuring are solved if the second parameter is a spread:

function splitURL(url) {
  const [path, ...queryString] = url.split('?');
  const URLParts = path.replace(/^\//, '').split('/');
  const newURL = [`/${URLParts.join('/')}`].concat(queryString).join('?');
  return newURL;
}

// Sane Query string                     
console.log(splitURL('/foo/bar?foo=1'));
// Bad query string
console.log(splitURL('/foo/bar?foo=1?foo=2'));
// No query string
console.log(splitURL('/foo/bar'));

Outputs:

  • /foo/bar?foo=1
  • /foo/bar?foo=1?foo=2
  • /foo/bar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if that's more readable though...

Copy link
Member

Choose a reason for hiding this comment

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

ah well, maybe not...

Is a URL with more than one ? valid? I thought this was not possible.

@muffinresearch
Copy link
Contributor Author

As discussed on irc merging as is, we can follow up as needed.

@muffinresearch muffinresearch merged commit 0b1cd30 into mozilla:master Jun 4, 2019
@muffinresearch muffinresearch deleted the fix-unslashed-client-app-urls-with-params branch June 4, 2019 16:18
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.

Broken Link
3 participants