koa-cash 5.0.0 → 5.0.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.
Files changed (2) hide show
  1. package/index.js +13 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -155,12 +155,22 @@ module.exports = function (options) {
155
155
  };
156
156
 
157
157
  //
158
- // if the content-type was `text` or `text/plain` then don't cache
158
+ // if the content-type was `text` or started with `text/plain` then
159
+ // check if the path extension is a known text/plain extension;
160
+ // if it has an extension that is NOT in the list, null the type
159
161
  // (since it's likely cache poisoning or the default Koa `text` being used)
160
162
  //
161
- if (obj.type === 'text' || obj.type === 'text/plain') {
163
+ // NOTE: we use `startsWith` for `text/plain` to handle charset variations
164
+ // (e.g. `text/plain; charset=utf-8` which is Koa's default)
165
+ //
166
+ // NOTE: `path.extname()` returns the extension with a leading dot
167
+ // (e.g. `.txt`) but `TXT_EXTENSIONS` stores extensions without
168
+ // the dot (e.g. `txt`), so we strip the dot with `slice(1)`
169
+ //
170
+ if (obj.type === 'text' || obj.type?.startsWith('text/plain')) {
162
171
  const ext = path.extname(ctx.path);
163
- if (ext && !TXT_EXTENSIONS.has(ext.toLowerCase())) obj.type = null;
172
+ if (ext && !TXT_EXTENSIONS.has(ext.slice(1).toLowerCase()))
173
+ obj.type = null;
164
174
  }
165
175
 
166
176
  const { fresh } = ctx.request;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koa-cash",
3
3
  "description": "HTTP response caching for Koa. HTTP response caching for Koa. Supports Redis, in-memory store, and more!",
4
- "version": "5.0.0",
4
+ "version": "5.0.2",
5
5
  "author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/koajs/cash/issues",