Ability to disable some bodyparser types completely #4458
ThisIsMissEm
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
I think what you are trying to do can be achieved with a custom middleware. Make sure to register it within the import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
export default class ContentTypeInspectorMiddleware {
async handle(ctx: HttpContext, next: NextFn) {
if (ctx.request.hasBody() && !!ctx.request.is(['multipart/form-data'])) {
return ctx.response.badRequest('Unsupported request body')
}
/**
* Call next method in the pipeline and return its output
*/
const output = await next()
return output
}
} router.use([
() => import('#middleware/content_type_inspector_middleware'), // 👈 before bodyparser
() => import('@adonisjs/core/bodyparser_middleware'),
]) This way you never even try to parse the body if it is disallowed |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, we're working on a server that doesn't need to process
multipart/form-data
requests at all, and as such, we'd like to be able to disable the multipart parsing entirely, and reject those requests with a400 Bad Request
or similar status code.Would it be possible to allow each of
form
,json
,multipart
,raw
to be set tofalse
in the configuration, rather than an object, and iffalse
then reject the request entirely? This would help improve our security posture by rejecting requests that we know we don't want to handle.Currently there doesn't seem to be anyway to disable processing of certain body types entirely, the only hacked work around is setting
types
to an empty array, which just has the request fall through.Beta Was this translation helpful? Give feedback.
All reactions