Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions apps/content/docs/plugins/method-override.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ sidebar:

## How It Works

HTML forms only support the GET and POST methods. When a POST request carries a `method` query parameter with an allowed value, the plugin routes the request as if it used that method and removes the parameter before input decoding. Values outside the allowed list are ignored and the request is processed as a regular POST.
HTML forms only support the GET and POST methods. When a POST request carries a `_method` query parameter with an allowed value, the plugin routes the request as if it used that method and removes the parameter before input decoding. Values outside the allowed list are ignored and the request is processed as a regular POST.

```html
<form method="post" action="/api/todos/1?method=DELETE">
<form method="post" action="/api/todos/1?_method=DELETE">
<button>Delete todo</button>
</form>
```
Expand All @@ -27,9 +27,9 @@ const handler = new OpenAPIHandler(router, {
/**
* The query parameter carrying the override method.
*
* @default 'method'
* @default '_method'
*/
param: 'method',
param: '_method',

/**
* The methods a POST request may be overridden to.
Expand Down
60 changes: 48 additions & 12 deletions packages/server/src/plugins/method-override.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('methodOverrideHandlerPlugin', () => {
it('ignores non-POST requests', async () => {
const { result, next, nextResult } = await invokeInterceptor({
method: 'GET',
url: '/todos/1?method=DELETE',
url: '/todos/1?_method=DELETE',
})

expect(next).toHaveBeenCalledExactlyOnceWith()
Expand All @@ -69,7 +69,7 @@ describe('methodOverrideHandlerPlugin', () => {
it('overrides the method and strips the param', async () => {
const { result, next, nextResult } = await invokeInterceptor({
method: 'POST',
url: '/todos/1?method=DELETE',
url: '/todos/1?_method=DELETE',
})

expect(next).toHaveBeenCalledExactlyOnceWith(
Expand All @@ -81,7 +81,7 @@ describe('methodOverrideHandlerPlugin', () => {
})

it('matches the override value case-insensitively', async () => {
const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=delete' })
const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?_method=delete' })

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
Expand All @@ -93,7 +93,7 @@ describe('methodOverrideHandlerPlugin', () => {
it('uses the last occurrence when the param is repeated and strips all of them', async () => {
const { next } = await invokeInterceptor({
method: 'POST',
url: '/todos/1?method=PUT&method=DELETE',
url: '/todos/1?_method=PUT&_method=DELETE',
})

expect(next).toHaveBeenCalledExactlyOnceWith(
Expand All @@ -104,7 +104,7 @@ describe('methodOverrideHandlerPlugin', () => {
})

it('preserves other query params and the hash', async () => {
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?x=1&method=DELETE&y=2#h' })
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?x=1&_method=DELETE&y=2#h' })

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
Expand All @@ -118,7 +118,7 @@ describe('methodOverrideHandlerPlugin', () => {
async (method) => {
const { result, next, nextResult } = await invokeInterceptor({
method: 'POST',
url: `/todos/1?method=${method}`,
url: `/todos/1?_method=${method}`,
})

expect(next).toHaveBeenCalledExactlyOnceWith()
Expand All @@ -128,20 +128,56 @@ describe('methodOverrideHandlerPlugin', () => {

it('honors a custom param name', async () => {
const { next } = await invokeInterceptor(
{ method: 'POST', url: '/todos/1?_method=DELETE&method=PUT' },
{ param: '_method' },
{ method: 'POST', url: '/todos/1?method=DELETE&_method=PUT' },
{ param: 'method' },
)

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
request: expect.objectContaining({ method: 'DELETE', url: '/todos/1?method=PUT' }),
request: expect.objectContaining({ method: 'DELETE', url: '/todos/1?_method=PUT' }),
}),
)
})

it('does not re-encode the remaining query string', async () => {
const { next } = await invokeInterceptor({
method: 'POST',
url: '/a?q=a%20b+c&_method=DELETE&redirect=/x?y=1,2&e=%7E*&flag&&k=&?_method=keep#h',
})

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
request: expect.objectContaining({
method: 'DELETE',
url: '/a?q=a%20b+c&redirect=/x?y=1,2&e=%7E*&flag&&k=&?_method=keep#h',
}),
}),
)
})

it('keeps a remaining empty query component', async () => {
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?_method=DELETE&' })

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
request: expect.objectContaining({ method: 'DELETE', url: '/a?' }),
}),
)
})

it('matches an encoded param name', async () => {
const { next } = await invokeInterceptor({ method: 'POST', url: '/a?%5Fmethod=DELETE&x=1' })

expect(next).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
request: expect.objectContaining({ method: 'DELETE', url: '/a?x=1' }),
}),
)
})

it('honors custom allowed methods', async () => {
const { next } = await invokeInterceptor(
{ method: 'POST', url: '/todos/1?method=GET' },
{ method: 'POST', url: '/todos/1?_method=GET' },
{ methods: ['get'] },
)

Expand All @@ -152,7 +188,7 @@ describe('methodOverrideHandlerPlugin', () => {
)

const { next: next2 } = await invokeInterceptor(
{ method: 'POST', url: '/todos/1?method=DELETE' },
{ method: 'POST', url: '/todos/1?_method=DELETE' },
{ methods: ['GET'] },
)

Expand All @@ -171,7 +207,7 @@ describe('methodOverrideHandlerPlugin', () => {
)

const { matched, response } = await handler.handle(
new Request('https://example.com/ping?method=DELETE', {
new Request('https://example.com/ping?_method=DELETE', {
method: 'POST',
headers: {
'content-type': 'application/json',
Expand Down
13 changes: 9 additions & 4 deletions packages/server/src/plugins/method-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface MethodOverrideHandlerPluginOptions {
/**
* The query parameter carrying the override method.
*
* @default 'method'
* @default '_method'
*/
param?: string | undefined

Expand Down Expand Up @@ -47,7 +47,7 @@ export class MethodOverrideHandlerPlugin<T extends Context> implements StandardH
private readonly methods: ReadonlySet<string>

constructor(options: MethodOverrideHandlerPluginOptions = {}) {
this.param = options.param ?? 'method'
this.param = options.param ?? '_method'
this.methods = new Set(
(options.methods ?? ['PUT', 'PATCH', 'DELETE']).map((method) => method.toUpperCase()),
)
Expand Down Expand Up @@ -78,8 +78,13 @@ export class MethodOverrideHandlerPlugin<T extends Context> implements StandardH
return next()
}

params.delete(this.param)
const url = `${pathname}${params.size ? `?${params}` : ''}${hash ?? ''}` as StandardUrl
// Drop only the override param, leaving the rest of the query untouched.
const rest = search!
.slice(1)
.split('&')
.filter((pair) => new URLSearchParams(`?${pair}`).keys().next().value !== this.param)
const url =
`${pathname}${rest.length ? `?${rest.join('&')}` : ''}${hash ?? ''}` as StandardUrl

return next({
...interceptorOptions,
Expand Down
8 changes: 4 additions & 4 deletions tests/openapi/method-override.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {
vi.clearAllMocks()
})

it('routes a POST with ?method=DELETE to the DELETE procedure with clean input', async () => {
it('routes a POST with ?_method=DELETE to the DELETE procedure with clean input', async () => {
const { matched, response } = await handler.handle(
new Request('https://example.com/todos/1?method=DELETE', {
new Request('https://example.com/todos/1?_method=DELETE', {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {

it('does not leak the override param into detailed query input', async () => {
const { matched, response } = await handler.handle(
new Request('https://example.com/articles/1?method=PUT&tag=news', {
new Request('https://example.com/articles/1?_method=PUT&tag=news', {
method: 'POST',
headers: {
'content-type': 'application/json',
Expand All @@ -95,7 +95,7 @@ describe('methodOverrideHandlerPlugin with OpenAPIHandler', () => {

it('silently ignores a disallowed override value', async () => {
const { matched } = await handler.handle(
new Request('https://example.com/todos/1?method=FOO', {
new Request('https://example.com/todos/1?_method=FOO', {
method: 'POST',
}),
)
Expand Down
Loading