fix(validator): prevent null pointer dereference in query validation#4706
Open
RoyRoki wants to merge 1 commit intohonojs:mainfrom
Open
fix(validator): prevent null pointer dereference in query validation#4706RoyRoki wants to merge 1 commit intohonojs:mainfrom
RoyRoki wants to merge 1 commit intohonojs:mainfrom
Conversation
The query validator was calling Object.entries(c.req.queries())
without null checking. When no query string is present,
c.req.queries() returns undefined (per getQueryParams in url.ts:231),
causing Object.entries(undefined) to throw a TypeError and crash
the application.
This fix adds a safe fallback:
```typescript
const queries = c.req.queries() || {}
Object.entries(queries).map(...)
```
Impact:
- Before: Application crash on requests without query strings
- After: Safe handling, empty object returned for missing queries
Attack scenario:
1. Attacker sends request without query params to validated endpoint
2. Application crashes with TypeError
3. DoS (Denial of Service) achieved
Severity: Critical (CWE-476: NULL Pointer Dereference)
Identified by: WhiteRose AI bug hunter (github.com/abhisheksunil2201/whiterose)
Bug ID: WR-011
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes critical null pointer dereference that causes application crashes when validating requests without query strings.
Bug Details
Problem
The query validator calls `Object.entries(c.req.queries())` without checking if the result is `undefined`.
When no query string is present in the request, `c.req.queries()` returns `undefined` (see `getQueryParams()` in `src/utils/url.ts:231`), causing `Object.entries(undefined)` to throw a TypeError and crash the application.
Vulnerable Code (Line 146)
```typescript
case 'query':
value = Object.fromEntries(
Object.entries(c.req.queries()).map(([k, v]) => { // ❌ Crashes if undefined
return v.length === 1 ? [k, v[0]] : [k, v]
})
)
break
```
Code Path to Crash
Attack Scenario
DoS (Denial of Service):
Example vulnerable endpoint:
```typescript
app.get('/search', validator('query', z.object({ q: z.string() })), (c) => {
// This crashes if request is: GET /search (no ?q=...)
})
```
Changes
Added null-safe fallback before calling `Object.entries()`:
```typescript
case 'query': {
const queries = c.req.queries() || {} // ✅ Safe fallback
value = Object.fromEntries(
Object.entries(queries).map(([k, v]) => {
return v.length === 1 ? [k, v[0]] : [k, v]
})
)
break
}
```
This ensures:
Security Impact
Before: Application crash (DoS) on requests without query parameters
After: Safe handling, validation proceeds normally
Testing
Related Code
The root cause is in `src/utils/url.ts:231`:
```typescript
export const getQueryParams = (url: string, key?: string) => {
const queryIndex = url.indexOf('?', 8)
if (queryIndex === -1) {
return undefined // ← Returns undefined when no query string
}
// ...
}
```
WhiteRose Report
This bug was automatically identified by WhiteRose's null-safety analysis pass, which traces data flow to find potential null/undefined dereferences.
🔗 Try it: github.com/shakecodeslikecray/whiterose
Related: This is PR #3 in a series addressing security vulnerabilities found by WhiteRose in Hono.