fix: encode redirect url if not already encoded (#1384)
Same bug from express 76eaa326ee
This commit is contained in:
parent
817b498305
commit
54e8fab3e3
3 changed files with 22 additions and 1 deletions
|
@ -19,6 +19,7 @@ const extname = require('path').extname;
|
||||||
const vary = require('vary');
|
const vary = require('vary');
|
||||||
const only = require('only');
|
const only = require('only');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
const encodeUrl = require('encodeurl');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prototype.
|
* Prototype.
|
||||||
|
@ -260,7 +261,7 @@ module.exports = {
|
||||||
redirect(url, alt) {
|
redirect(url, alt) {
|
||||||
// location
|
// location
|
||||||
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
|
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
|
||||||
this.set('Location', url);
|
this.set('Location', encodeUrl(url));
|
||||||
|
|
||||||
// status
|
// status
|
||||||
if (!statuses.redirect[this.status]) this.status = 302;
|
if (!statuses.redirect[this.status]) this.status = 302;
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
"delegates": "^1.0.0",
|
"delegates": "^1.0.0",
|
||||||
"depd": "^1.1.2",
|
"depd": "^1.1.2",
|
||||||
"destroy": "^1.0.4",
|
"destroy": "^1.0.4",
|
||||||
|
"encodeurl": "^1.0.2",
|
||||||
"error-inject": "^1.0.0",
|
"error-inject": "^1.0.0",
|
||||||
"escape-html": "^1.0.3",
|
"escape-html": "^1.0.3",
|
||||||
"fresh": "~0.5.2",
|
"fresh": "~0.5.2",
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const request = require('supertest');
|
||||||
const context = require('../helpers/context');
|
const context = require('../helpers/context');
|
||||||
|
const Koa = require('../..');
|
||||||
|
|
||||||
describe('ctx.redirect(url)', () => {
|
describe('ctx.redirect(url)', () => {
|
||||||
it('should redirect to the given url', () => {
|
it('should redirect to the given url', () => {
|
||||||
|
@ -12,6 +14,23 @@ describe('ctx.redirect(url)', () => {
|
||||||
assert.equal(ctx.status, 302);
|
assert.equal(ctx.status, 302);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should auto fix not encode url', done => {
|
||||||
|
const app = new Koa();
|
||||||
|
|
||||||
|
app.use(ctx => {
|
||||||
|
ctx.redirect('http://google.com/😓');
|
||||||
|
});
|
||||||
|
|
||||||
|
request(app.callback())
|
||||||
|
.get('/')
|
||||||
|
.end((err, res) => {
|
||||||
|
if (err) return done(err);
|
||||||
|
assert.equal(res.status, 302);
|
||||||
|
assert.equal(res.headers.location, 'http://google.com/%F0%9F%98%93');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('with "back"', () => {
|
describe('with "back"', () => {
|
||||||
it('should redirect to Referrer', () => {
|
it('should redirect to Referrer', () => {
|
||||||
const ctx = context();
|
const ctx = context();
|
||||||
|
|
Loading…
Reference in a new issue