From c49ee26fad61041a5408c9d63590850b2367be69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=8Bnh=20L=C3=AA?= Date: Mon, 23 Nov 2026 16:31:08 +0700 Subject: [PATCH 1/4] fix(server): default method override param to _method and keep the query string intact --- apps/content/docs/plugins/method-override.mdx | 8 +-- .../src/plugins/method-override.test.ts | 60 +++++++++++++++---- .../server/src/plugins/method-override.ts | 13 ++-- tests/openapi/method-override.test.ts | 8 +-- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/apps/content/docs/plugins/method-override.mdx b/apps/content/docs/plugins/method-override.mdx index 53ff11e84..c44bfa92d 100644 --- a/apps/content/docs/plugins/method-override.mdx +++ b/apps/content/docs/plugins/method-override.mdx @@ -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 -
+
``` @@ -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. diff --git a/packages/server/src/plugins/method-override.test.ts b/packages/server/src/plugins/method-override.test.ts index 21f4bc63a..e53c1bf55 100644 --- a/packages/server/src/plugins/method-override.test.ts +++ b/packages/server/src/plugins/method-override.test.ts @@ -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() @@ -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( @@ -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({ @@ -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( @@ -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({ @@ -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() @@ -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' }), + }), + ) + }) + + it('ignores a `method` param by default', async () => { + const { result, next, nextResult } = await invokeInterceptor({ + method: 'POST', + url: '/todos/1?method=DELETE', + }) + + expect(next).toHaveBeenCalledExactlyOnceWith() + expect(result).toBe(nextResult) + }) + + 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=#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=#h', + }), + }), ) + }) + + 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: '/todos/1?method=PUT' }), + 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'] }, ) @@ -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'] }, ) @@ -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', diff --git a/packages/server/src/plugins/method-override.ts b/packages/server/src/plugins/method-override.ts index 5324fadab..ac050772b 100644 --- a/packages/server/src/plugins/method-override.ts +++ b/packages/server/src/plugins/method-override.ts @@ -13,7 +13,7 @@ export interface MethodOverrideHandlerPluginOptions { /** * The query parameter carrying the override method. * - * @default 'method' + * @default '_method' */ param?: string | undefined @@ -47,7 +47,7 @@ export class MethodOverrideHandlerPlugin implements StandardH private readonly methods: ReadonlySet 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()), ) @@ -78,8 +78,13 @@ export class MethodOverrideHandlerPlugin 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) + .join('&') + const url = `${pathname}${rest ? `?${rest}` : ''}${hash ?? ''}` as StandardUrl return next({ ...interceptorOptions, diff --git a/tests/openapi/method-override.test.ts b/tests/openapi/method-override.test.ts index 14121787f..628b6cf5b 100644 --- a/tests/openapi/method-override.test.ts +++ b/tests/openapi/method-override.test.ts @@ -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', @@ -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', @@ -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', }), ) From 93aad08e8a6e0b4eeb3993acfba87fe975d70a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=8Bnh=20L=C3=AA?= Date: Mon, 23 Nov 2026 16:42:46 +0700 Subject: [PATCH 2/4] test(server): drop redundant method override default test --- packages/server/src/plugins/method-override.test.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/server/src/plugins/method-override.test.ts b/packages/server/src/plugins/method-override.test.ts index e53c1bf55..adf486a8a 100644 --- a/packages/server/src/plugins/method-override.test.ts +++ b/packages/server/src/plugins/method-override.test.ts @@ -139,16 +139,6 @@ describe('methodOverrideHandlerPlugin', () => { ) }) - it('ignores a `method` param by default', async () => { - const { result, next, nextResult } = await invokeInterceptor({ - method: 'POST', - url: '/todos/1?method=DELETE', - }) - - expect(next).toHaveBeenCalledExactlyOnceWith() - expect(result).toBe(nextResult) - }) - it('does not re-encode the remaining query string', async () => { const { next } = await invokeInterceptor({ method: 'POST', From 8e049b7a944bd48349fe4853449816fa7f50c4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=8Bnh=20L=C3=AA?= Date: Mon, 23 Nov 2026 16:53:58 +0700 Subject: [PATCH 3/4] fix(server): keep query params whose name starts with a literal ? --- packages/server/src/plugins/method-override.test.ts | 4 ++-- packages/server/src/plugins/method-override.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/server/src/plugins/method-override.test.ts b/packages/server/src/plugins/method-override.test.ts index adf486a8a..7bea0e86c 100644 --- a/packages/server/src/plugins/method-override.test.ts +++ b/packages/server/src/plugins/method-override.test.ts @@ -142,14 +142,14 @@ describe('methodOverrideHandlerPlugin', () => { 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=#h', + 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=#h', + url: '/a?q=a%20b+c&redirect=/x?y=1,2&e=%7E*&flag&&k=&?_method=keep#h', }), }), ) diff --git a/packages/server/src/plugins/method-override.ts b/packages/server/src/plugins/method-override.ts index ac050772b..f5f750851 100644 --- a/packages/server/src/plugins/method-override.ts +++ b/packages/server/src/plugins/method-override.ts @@ -82,7 +82,7 @@ export class MethodOverrideHandlerPlugin implements StandardH const rest = search! .slice(1) .split('&') - .filter((pair) => new URLSearchParams(pair).keys().next().value !== this.param) + .filter((pair) => new URLSearchParams(`?${pair}`).keys().next().value !== this.param) .join('&') const url = `${pathname}${rest ? `?${rest}` : ''}${hash ?? ''}` as StandardUrl From 59fad65d1233ccaa411b2d4ad04bcca1d51d2e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=8Bnh=20L=C3=AA?= Date: Mon, 23 Nov 2026 16:59:01 +0700 Subject: [PATCH 4/4] fix(server): keep a remaining empty query component after method override --- packages/server/src/plugins/method-override.test.ts | 10 ++++++++++ packages/server/src/plugins/method-override.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/server/src/plugins/method-override.test.ts b/packages/server/src/plugins/method-override.test.ts index 7bea0e86c..30c600e7d 100644 --- a/packages/server/src/plugins/method-override.test.ts +++ b/packages/server/src/plugins/method-override.test.ts @@ -155,6 +155,16 @@ describe('methodOverrideHandlerPlugin', () => { ) }) + 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' }) diff --git a/packages/server/src/plugins/method-override.ts b/packages/server/src/plugins/method-override.ts index f5f750851..e845ff409 100644 --- a/packages/server/src/plugins/method-override.ts +++ b/packages/server/src/plugins/method-override.ts @@ -83,8 +83,8 @@ export class MethodOverrideHandlerPlugin implements StandardH .slice(1) .split('&') .filter((pair) => new URLSearchParams(`?${pair}`).keys().next().value !== this.param) - .join('&') - const url = `${pathname}${rest ? `?${rest}` : ''}${hash ?? ''}` as StandardUrl + const url = + `${pathname}${rest.length ? `?${rest.join('&')}` : ''}${hash ?? ''}` as StandardUrl return next({ ...interceptorOptions,