Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ module.exports = {
*/

get origin () {
return this.req.headers.origin || null
if (!this.host) return null
return `${this.protocol}://${this.host}`
},

/**
Expand Down
22 changes: 15 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ module.exports = {
redirect (url) {
if (/^https?:\/\//i.test(url)) {
// formatting url again avoid security escapes
url = new URL(url).toString()
try {
url = new URL(url).toString()
} catch {
// invalid URL, encode as-is
}
}
this.set('Location', encodeUrl(url))

Expand Down Expand Up @@ -338,11 +342,15 @@ module.exports = {
back (alt) {
const referrer = this.ctx.get('Referrer')
if (referrer) {
// referrer is an absolute URL, check if it's the same origin
const url = new URL(referrer, this.ctx.href)
if (url.host === this.ctx.host) {
this.redirect(referrer)
return
try {
// referrer is an absolute URL, check if it's the same origin
const url = new URL(referrer, this.ctx.href)
if (url.host === this.ctx.host) {
this.redirect(referrer)
return
}
} catch {
// invalid URL, fall through to alt
}
}

Expand Down Expand Up @@ -657,4 +665,4 @@ module.exports = {
/* istanbul ignore else */
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect
}
}
Loading