Skip to content

Commit

Permalink
fix: handle upper case protocol like HTTP or HTTPS (#1805)
Browse files Browse the repository at this point in the history
Co-authored-by: fengmk2 <suqian.yf@antgroup.com>
  • Loading branch information
FDrag0n and fengmk2 authored Mar 21, 2024
1 parent 435534a commit 185e701
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions __tests__/response/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('ctx.redirect(url)', () => {
assert.strictEqual(ctx.status, 302)
})

it('should formatting url before redirect', () => {
const ctx = context()
ctx.redirect('HTTP://google.com\\@apple.com')
assert.strictEqual(ctx.response.header.location, 'http://google.com/@apple.com')
assert.strictEqual(ctx.status, 302)
})

it('should auto fix not encode url', done => {
const app = new Koa()

Expand Down
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ module.exports = {
redirect (url, alt) {
// location
if (url === 'back') url = this.ctx.get('Referrer') || alt || '/'
if (url.startsWith('https://') || url.startsWith('http://')) {
if (/^https?:\/\//i.test(url)) {
// formatting url again avoid security escapes
url = new URL(url).toString()
}
Expand Down

0 comments on commit 185e701

Please sign in to comment.