Skip to content

Commit 14db9f2

Browse files
FDrag0nfengmk2
authored andcommitted
fix: handle upper case protocol like HTTP or HTTPS (koajs#1805)
Co-authored-by: fengmk2 <[email protected]>
1 parent 69932e3 commit 14db9f2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

__tests__/response/redirect.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ describe('ctx.redirect(url)', () => {
2020
assert.strictEqual(ctx.status, 302)
2121
})
2222

23+
it('should formatting url before redirect', () => {
24+
const ctx = context()
25+
ctx.redirect('HTTP://google.com\\@apple.com')
26+
assert.strictEqual(ctx.response.header.location, 'http://google.com/@apple.com')
27+
assert.strictEqual(ctx.status, 302)
28+
})
29+
2330
it('should auto fix not encode url', done => {
2431
const app = new Koa()
2532

lib/response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ module.exports = {
266266
redirect (url, alt) {
267267
// location
268268
if (url === 'back') url = this.ctx.get('Referrer') || alt || '/'
269-
if (url.startsWith('https://') || url.startsWith('http://')) {
269+
if (/^https?:\/\//i.test(url)) {
270270
// formatting url again avoid security escapes
271271
url = new URL(url).toString()
272272
}

0 commit comments

Comments
 (0)