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 committed Mar 21, 2024
1 parent 549455d commit d5a681b
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);
})

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (21.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (21.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Missing semicolon

Check failure on line 29 in __tests__/response/redirect.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Missing semicolon

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 d5a681b

Please sign in to comment.