-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[zod-openapi][security] Unexpected validation bypass when omitting content-type #891
Comments
I think in this case we'd want to return 400 if the request const me = createRoute({
request: {
body: { content: { 'application/json': { schema: z.null() } } },
},
}); Such that ^ request invoked with |
if (c.req.header('content-type')) {
if (isJSONContentType(c.req.header('content-type')!)) {
return await validator(c, next)
}
}
c.req.addValidatedData('json', {})
await next() Although as far i understand, in this code segment we ignore that malformed payload. Idk what security issues might come from this? It's not like we can send a payload & skip the validation for it... Any request sent like this would most likely result in a 500 error due to referenced values being It's still an unpredicted behavior and something that I believe should be addressed. @yusukebe pls ping me if you have any thoughts on this and I can make a PR to resolve this if needed. The reason I believe it's unpredicted is typing: in the endpoint when call |
Hi @tobiasdcl I'm sorry for putting in that change without notice. In this case, you can force validation by adding const route = createRoute({
method: 'POST',
path: '/book',
request: {
body: {
required: true, // <=== add
content: {
'application/json': {
schema: z.object({
title: z.string().startsWith('[TITLE]').min(10).max(30)
})
}
}
}
}
}) |
I would challenge this statement. Depending on the business logic having
I agree with that statement and that was exactly my concern. E.g. expecting a validated enum value and instead getting
Thanks that is great! Thanks for the super quick response ❤️ |
Agreed. And I agree on the point that this should be the default |
Hey there,
starting from
"@hono/zod-openapi": "0.15.2"
the body validation is skipped if the content-type is not matched - ref PR.Personally I was very surprised by this as this means you can effectively bypass validations and invoke the handler as long as you just omit the content-type.
I was also not able to find any mentioning of this in the docs. I think this can be a significant security risk as it causes unexpected behaviour in the handler which might expect a validated payload.
I would argue that the mention PR should be reverted and the handler should fail if the content-type is not matched, but at least this should be clearly documented and have been marked as a breaking change.
Reproduction
With
"@hono/zod-openapi": "0.15.1"
you got the following behaviour - which I would expect:starting from
"@hono/zod-openapi": "0.15.2"
the handler is invoked if the content-type is omitted:as a mitigation the content-type can be enforced by validating the incoming
content-type
header.Thanks!
The text was updated successfully, but these errors were encountered: