Skip to content

Commit

Permalink
fix: handle upper case protocol like HTTP or HTTPS (#1806)
Browse files Browse the repository at this point in the history
pick from #1805

Co-authored-by: FDrag0n <34733637+FDrag0n@users.noreply.github.com>
  • Loading branch information
fengmk2 and FDrag0n committed Mar 21, 2024
1 parent 549455d commit 94e8def
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 @@ -21,6 +21,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/okoK');
assert.strictEqual(ctx.response.header.location, 'http://google.com/@apple.coM/okoK');
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 @@ -261,7 +261,7 @@ module.exports = {
redirect(url, alt) {
// location
if ('back' === url) 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 94e8def

Please sign in to comment.