Skip to content

Commit 94e8def

Browse files
fengmk2FDrag0n
andauthored
fix: handle upper case protocol like HTTP or HTTPS (#1806)
pick from #1805 Co-authored-by: FDrag0n <[email protected]>
1 parent 549455d commit 94e8def

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

__tests__/response/redirect.js

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ describe('ctx.redirect(url)', () => {
2121
assert.strictEqual(ctx.status, 302);
2222
});
2323

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

lib/response.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ module.exports = {
261261
redirect(url, alt) {
262262
// location
263263
if ('back' === url) url = this.ctx.get('Referrer') || alt || '/';
264-
if (url.startsWith('https://') || url.startsWith('http://')) {
264+
if (/^https?:\/\//i.test(url)) {
265265
// formatting url again avoid security escapes
266266
url = new URL(url).toString();
267267
}

0 commit comments

Comments
 (0)