Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: unsafe methods not causing cache purge
Fixes bug introduced in #3733 where unsafe methods no longer make it to
the cache handler and cause a cache purge for an origin.

Also removes a duplicate test file.

Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
  • Loading branch information
flakey5 committed Oct 16, 2024
commit f2213801bde64193aca31a371f669150c8b3db39
10 changes: 2 additions & 8 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class CacheHandler extends DecoratorHandler {
*/
#store

/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheMethods}
*/
#methods

/**
* @type {import('../../types/dispatcher.d.ts').default.RequestOptions}
*/
Expand All @@ -42,14 +37,13 @@ class CacheHandler extends DecoratorHandler {
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
*/
constructor (opts, requestOptions, handler) {
const { store, methods } = opts
const { store } = opts

super(handler)

this.#store = store
this.#requestOptions = requestOptions
this.#handler = handler
this.#methods = methods
}

/**
Expand All @@ -75,7 +69,7 @@ class CacheHandler extends DecoratorHandler {
)

if (
!this.#methods.includes(this.#requestOptions.method) &&
!util.safeHTTPMethods.includes(this.#requestOptions.method) &&
statusCode >= 200 &&
statusCode <= 399
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = (opts = {}) => {

return dispatch => {
return (opts, handler) => {
if (!opts.origin || !methods.includes(opts.method)) {
if (!opts.origin || (opts.method in util.safeHTTPMethods && !methods.includes(opts.method))) {
Comment thread
flakey5 marked this conversation as resolved.
Outdated
// Not a method we want to cache or we don't have the origin, skip
return dispatch(opts, handler)
}
Expand Down
240 changes: 0 additions & 240 deletions test/cache-interceptor/interceptor.js

This file was deleted.

39 changes: 38 additions & 1 deletion test/interceptors/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { describe, test, after } = require('node:test')
const { strictEqual, notEqual, fail } = require('node:assert')
const { strictEqual, notEqual, fail, equal } = require('node:assert')
const { createServer } = require('node:http')
const { once } = require('node:events')
const FakeTimers = require('@sinonjs/fake-timers')
Expand Down Expand Up @@ -250,4 +250,41 @@ describe('Cache Interceptor', () => {
}
})
})

test('unsafe methods call the store\'s deleteByOrigin function', async () => {
const server = createServer((_, res) => {
res.end('asd')
}).listen(0)

after(() => server.close())
await once(server, 'listening')

let deleteByOriginCalled = false
const store = new cacheStores.MemoryCacheStore()

const originalDeleteByOrigin = store.deleteByOrigin.bind(store)
store.deleteByOrigin = (origin) => {
deleteByOriginCalled = true
originalDeleteByOrigin(origin)
}

const client = new Client(`http://localhost:${server.address().port}`)
.compose(interceptors.cache({ store }))

await client.request({
origin: 'localhost',
method: 'GET',
path: '/'
})

equal(deleteByOriginCalled, false)

await client.request({
origin: 'localhost',
method: 'DELETE',
path: '/'
})

equal(deleteByOriginCalled, true)
})
})