Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
6e23b75c
Commit
6e23b75c
authored
Feb 20, 2019
by
Isaac Hinman
Committed by
Joe Haddad
Feb 20, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Validate url.format calls (#6379)
parent
61de215e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
10 deletions
+28
-10
router.js
packages/next-server/lib/router/router.js
+6
-6
utils.js
packages/next-server/lib/utils.js
+18
-0
link.js
packages/next/client/link.js
+4
-4
No files found.
packages/next-server/lib/router/router.js
View file @
6e23b75c
/* 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'
,
format
WithValidation
({
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'
,
format
WithValidation
({
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'
?
format
WithValidation
(
_url
)
:
_url
let
as
=
typeof
_as
===
'object'
?
format
(
_as
)
:
_as
let
as
=
typeof
_as
===
'object'
?
format
WithValidation
(
_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.
...
...
packages/next-server/lib/utils.js
View file @
6e23b75c
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
)
}
packages/next/client/link.js
View file @
6e23b75c
/* 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
)
?
format
WithValidation
(
href
)
:
href
,
:
href
,
as
:
asHref
&&
typeof
asHref
===
'object'
as
:
asHref
&&
typeof
asHref
===
'object'
?
format
(
asHref
)
?
format
WithValidation
(
asHref
)
:
asHref
:
asHref
}
}
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment