koa 2.16.0 → 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.
@@ -257,10 +257,9 @@ function respond(ctx) {
257
257
  // allow bypassing koa
258
258
  if (false === ctx.respond) return;
259
259
 
260
- const res = ctx.res;
261
-
262
- if (!ctx.writable) return res.end();
260
+ if (!ctx.writable) return;
263
261
 
262
+ const res = ctx.res;
264
263
  let body = ctx.body;
265
264
  const code = ctx.status;
266
265
 
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) url = this.ctx.get('Referrer') || alt || '/';
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();
@@ -275,7 +294,7 @@ module.exports = {
275
294
  if (this.ctx.accepts('html')) {
276
295
  url = escape(url);
277
296
  this.type = 'text/html; charset=utf-8';
278
- this.body = `Redirecting to <a href="${url}">${url}</a>.`;
297
+ this.body = `Redirecting to ${url}.`;
279
298
  return;
280
299
  }
281
300
 
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "koa",
3
- "version": "2.16.0",
3
+ "version": "2.16.2",
4
+ "publishConfig": {
5
+ "access": "public",
6
+ "tag": "latest-2"
7
+ },
4
8
  "description": "Koa web app framework",
5
9
  "main": "lib/application.js",
6
10
  "exports": {