-
-
Notifications
You must be signed in to change notification settings - Fork 574
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
fix(req): correct c.req.param
decodes invalid percent strings
#3573
fix(req): correct c.req.param
decodes invalid percent strings
#3573
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3573 +/- ##
==========================================
- Coverage 94.71% 94.71% -0.01%
==========================================
Files 157 158 +1
Lines 9539 9537 -2
Branches 2774 2794 +20
==========================================
- Hits 9035 9033 -2
Misses 504 504 ☔ View full report in Codecov by Sentry. |
Hi @usualoma ! Can you review this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yusukebe Thank you! I agree with this approach.
tryDecodeURIComponent
This is a pure function that does not require 'this', so I think it is better to make it an independent function rather than an object property. I think that would also improve performance.
Also, even if we merge this pull request, I think the minified result will be smaller if we use decodeURIComponent_
. However, it's only a few characters, so it may not matter.
diff --git a/src/request.ts b/src/request.ts
index 92ebc771..6239ebac 100644
--- a/src/request.ts
+++ b/src/request.ts
@@ -12,7 +12,7 @@ import type {
import { parseBody } from './utils/body'
import type { BodyData, ParseBodyOptions } from './utils/body'
import type { Simplify, UnionToIntersection } from './utils/types'
-import { getQueryParam, getQueryParams, tryDecode } from './utils/url'
+import { decodeURIComponent_, getQueryParam, getQueryParams, tryDecode } from './utils/url'
type Body = {
json: any
@@ -23,6 +23,8 @@ type Body = {
}
type BodyCache = Partial<Body & { parsedBody: BodyData }>
+const tryDecodeURIComponent = (str: string) => tryDecode(str, decodeURIComponent_)
+
export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
/**
* `.raw` can get the raw Request object.
@@ -91,12 +93,10 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
return key ? this.getDecodedParam(key) : this.getAllDecodedParams()
}
- #tryDecodeURIComponent = (str: string) => tryDecode(str, decodeURIComponent)
-
private getDecodedParam(key: string): string | undefined {
const paramKey = this.#matchResult[0][this.routeIndex][1][key]
const param = this.getParamValue(paramKey)
- return param ? this.#tryDecodeURIComponent(param) : undefined
+ return param ? tryDecodeURIComponent(param) : undefined
}
private getAllDecodedParams(): Record<string, string> {
@@ -106,7 +106,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
for (const key of keys) {
const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key])
if (value && typeof value === 'string') {
- decoded[key] = this.#tryDecodeURIComponent(value)
+ decoded[key] = tryDecodeURIComponent(value)
}
}
@usualoma Thanks! Agree all. Can you review this again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yusukebe Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good
Thanks! Merging now. |
Fixes #3550
In the current behavior, the
c.req.param()
will throw the error when decoding invalid percent encoding or invalid UTF-8 sequence.These behaviors are expected in the current version implementation; they are tested:
https://github.com/honojs/hono/compare/fix/request-dont-throw-error-with-percent-strings?expand=1#diff-8ac13809c9886e994d1db33943de82df4d6c5d88b73fd0270c0189804ff565c2L772-L814
However, according to #3550, throwing the error will stop the application procedure, and it can accept malicious requests. I think it should not throw the error. This should be fixed, though we should rewrite the test code.
The correct behavior
How to handle the invalid path parameter strings. One way is throwing
400 Bad Request
, but as I commented, it should follow the decode method of thegetPath()
to resolve the URL path:hono/src/utils/url.ts
Line 112 in 0a99bd3
So, the result will be like the following:
In this PR, I used the same logic of
tryDecodeURI
fortryDecodeURIComponent
.The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code