koa 2.16.1 → 2.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/response.js +20 -1
- package/package.json +5 -1
package/lib/response.js
CHANGED
|
@@ -240,6 +240,22 @@ module.exports = {
|
|
|
240
240
|
vary(this.res, field);
|
|
241
241
|
},
|
|
242
242
|
|
|
243
|
+
_getBackReferrer() {
|
|
244
|
+
const referrer = this.ctx.get('Referrer');
|
|
245
|
+
if (referrer) {
|
|
246
|
+
// referrer is a relative path
|
|
247
|
+
if (referrer.startsWith('/')) {
|
|
248
|
+
return referrer;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// referrer is an absolute URL, check if it's the same origin
|
|
252
|
+
const url = new URL(referrer, this.ctx.href);
|
|
253
|
+
if (url.host === this.ctx.host) {
|
|
254
|
+
return referrer;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
|
|
243
259
|
/**
|
|
244
260
|
* Perform a 302 redirect to `url`.
|
|
245
261
|
*
|
|
@@ -261,7 +277,10 @@ module.exports = {
|
|
|
261
277
|
|
|
262
278
|
redirect(url, alt) {
|
|
263
279
|
// location
|
|
264
|
-
if ('back' === url)
|
|
280
|
+
if ('back' === url) {
|
|
281
|
+
url = this._getBackReferrer() || alt || '/';
|
|
282
|
+
}
|
|
283
|
+
|
|
265
284
|
if (/^https?:\/\//i.test(url)) {
|
|
266
285
|
// formatting url again avoid security escapes
|
|
267
286
|
url = new URL(url).toString();
|