Commit 6e23b75c authored by Isaac Hinman's avatar Isaac Hinman Committed by Joe Haddad

feat: Validate url.format calls (#6379)

parent 61de215e
/* global __NEXT_DATA__ */ /* global __NEXT_DATA__ */
import { parse, format } from 'url' import { parse } from 'url'
import mitt from '../mitt' import mitt from '../mitt'
import shallowEquals from './shallow-equals' import shallowEquals from './shallow-equals'
import { loadGetInitialProps, getURL } from '../utils' import { loadGetInitialProps, getURL, formatWithValidation } from '../utils'
export default class Router { export default class Router {
static events = mitt() static events = mitt()
...@@ -38,7 +38,7 @@ export default class Router { ...@@ -38,7 +38,7 @@ export default class Router {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// in order for `e.state` to work on the `onpopstate` event // in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization // we have to register the initial route upon initialization
this.changeState('replaceState', format({ pathname, query }), as) this.changeState('replaceState', formatWithValidation({ pathname, query }), as)
window.addEventListener('popstate', this.onPopState) window.addEventListener('popstate', this.onPopState)
} }
...@@ -80,7 +80,7 @@ export default class Router { ...@@ -80,7 +80,7 @@ export default class Router {
// Actually, for (1) we don't need to nothing. But it's hard to detect that event. // Actually, for (1) we don't need to nothing. But it's hard to detect that event.
// So, doing the following for (1) does no harm. // So, doing the following for (1) does no harm.
const { pathname, query } = this const { pathname, query } = this
this.changeState('replaceState', format({ pathname, query }), getURL()) this.changeState('replaceState', formatWithValidation({ pathname, query }), getURL())
return return
} }
...@@ -163,8 +163,8 @@ export default class Router { ...@@ -163,8 +163,8 @@ export default class Router {
async change (method, _url, _as, options) { async change (method, _url, _as, options) {
// If url and as provided as an object representation, // If url and as provided as an object representation,
// we'll format them into the string version here. // we'll format them into the string version here.
const url = typeof _url === 'object' ? format(_url) : _url const url = typeof _url === 'object' ? formatWithValidation(_url) : _url
let as = typeof _as === 'object' ? format(_as) : _as let as = typeof _as === 'object' ? formatWithValidation(_as) : _as
// Add the ending slash to the paths. So, we can serve the // Add the ending slash to the paths. So, we can serve the
// "<page>/index.html" directly for the SSR page. // "<page>/index.html" directly for the SSR page.
......
import { format } from 'url'
export function execOnce (fn) { export function execOnce (fn) {
let used = false let used = false
return (...args) => { return (...args) => {
...@@ -54,3 +56,19 @@ export async function loadGetInitialProps (Component, ctx) { ...@@ -54,3 +56,19 @@ export async function loadGetInitialProps (Component, ctx) {
return props return props
} }
export const urlObjectKeys = ['auth', 'hash', 'host', 'hostname', 'href', 'path', 'pathname', 'port', 'protocol', 'query', 'search', 'slashes']
export function formatWithValidation (url, options) {
if (process.env.NODE_ENV === 'development') {
if (url !== null && typeof url === 'object') {
Object.keys(url).forEach((key) => {
if (!urlObjectKeys.includes(key)) {
console.warn(`Unknown key passed via urlObject into url.format: ${key}`)
}
})
}
}
return format(url, options)
}
/* global __NEXT_DATA__ */ /* global __NEXT_DATA__ */
import { resolve, format, parse } from 'url' import { resolve, parse } from 'url'
import React, { Component, Children } from 'react' import React, { Component, Children } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import Router, { Router as _Router } from 'next/router' import Router, { Router as _Router } from 'next/router'
import { execOnce, getLocationOrigin } from 'next-server/dist/lib/utils' import { execOnce, formatWithValidation, getLocationOrigin } from 'next-server/dist/lib/utils'
function isLocal (href) { function isLocal (href) {
const url = parse(href, false, true) const url = parse(href, false, true)
...@@ -47,10 +47,10 @@ class Link extends Component { ...@@ -47,10 +47,10 @@ class Link extends Component {
formatUrls = memoizedFormatUrl((href, asHref) => { formatUrls = memoizedFormatUrl((href, asHref) => {
return { return {
href: href && typeof href === 'object' href: href && typeof href === 'object'
? format(href) ? formatWithValidation(href)
: href, : href,
as: asHref && typeof asHref === 'object' as: asHref && typeof asHref === 'object'
? format(asHref) ? formatWithValidation(asHref)
: asHref : asHref
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment