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

Navigating to http://localhost// causes a security exception #243

Closed
f0rk opened this issue Mar 10, 2016 · 10 comments
Closed

Navigating to http://localhost// causes a security exception #243

f0rk opened this issue Mar 10, 2016 · 10 comments

Comments

@f0rk
Copy link

f0rk commented Mar 10, 2016

Originally from: remix-run/react-router#3168

When attempting to set pathname to "//" you will receive a security error similar to the following:

Uncaught SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'http:' cannot be created in a document with origin 'http://localhost'.

This is because it attempts to push // onto the history, which is interpreted as a scheme-relative URL.

@mjackson
Copy link
Member

Well, either the browsers throw or we will. You probably shouldn't be pushing that path.

@taion
Copy link
Contributor

taion commented Mar 10, 2016

We should probably handle this on our side to keep consistent behavior between what happens with the History API and what happens when using the full refresh fallback.

@f0rk
Copy link
Author

f0rk commented Mar 10, 2016

So close this issue and reopen remix-run/react-router#3168 ?

@taion
Copy link
Contributor

taion commented Mar 10, 2016

No – it still needs to be handled on the history side. Among other things, this limitation only arises with browser history (or other histories that use the HTML5 history API). It won't arise with hash history or memory history.

Among other things, history.push doesn't even go through any code in the router.

@mjackson
Copy link
Member

All I'm saying is we should probably just throw here if you try and push a path like that. So it's either we throw or the browser does.

@taion
Copy link
Contributor

taion commented Mar 10, 2016

Via an invariant, of course, so we can have an error message that gets stripped out for production builds 😀

@mjackson
Copy link
Member

I decided to handle this via a warning instead. We already warn about paths with protocols and/or domains attached. Protocol-relative URLs are just another case of the same problem.

@necolas
Copy link

necolas commented May 3, 2016

The drawback with only warning is that it doesn't account for weird URLs linking to your app or site. So we've had to patch this issue in our wrapper around history.

@mjackson
Copy link
Member

mjackson commented May 3, 2016

@necolas What does your patch do? Do you try to guess the right URL and redirect?

@necolas
Copy link

necolas commented May 7, 2016

This is what we do:

import { useRouterHistory } from 'react-router';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import useScroll from '../scroll-behavior';

const REGEX_LEADING_SLASHES = /^\/\/+/;
/**
 * Fixes relative paths with multiple leading slashes
 */
export const fixProtocolRelativeUrls = (history, location) => {
  const { pathname, search, hash } = location;
  if (pathname && REGEX_LEADING_SLASHES.test(pathname)) {
    const normalizedPathname = pathname.replace(leadingSlashes, '/');
    const newPath = normalizedPathname + search + hash;
    history.replace(newPath);
  }
};

let history;
const _createBrowserHistory = () => {
  if (!history) {
    history = useScroll(useRouterHistory(createBrowserHistory));
    fixProtocolRelativeUrls(history, window.location);
  }
  return history;
};

export default _createBrowserHistory();

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants