Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jack-kerouac
Copy link
Contributor

Disclaimer: When checking for security vulnerabilities, Claude Code found one for me. This PR fixes it. I let it walk me through the implications and the fix of this pull request. I am an absolute newbie in ESP32 programming and haven't written C in ages. Despite that, I considered opening this PR to be worthwhile, given that the problem description and fix that Claude Code explained to me sounded plausible. Following is the description of the actual problem and change.


This fixes a critical off-by-one buffer overflow in the HTTP request
handler that could lead to authentication bypass, information disclosure,
or crashes.

The vulnerability occurred in two places:

  1. Line 4643: The buffer filling loop allowed writing up to index 251
    (252 bytes) without leaving room for null terminator
  2. Line 4704: Null terminator was added without bounds checking, writing
    at index 252 which is out of bounds

Attack scenario:

  • Sending exactly 252 bytes would overflow into bPlaceInBuffer variable
  • This would reset the buffer index to 0 and create an unterminated string
  • Subsequent string operations (strcpy, strncmp) would read beyond buffer
  • Could bypass PASSKEY authentication or leak adjacent memory

Fix:

  • Limit buffer filling to MaxArrayElement-1 to reserve space for null
  • Add bounds checking before writing null terminator
  • Ensure string is always properly null-terminated

This prevents any writes beyond the buffer bounds and eliminates the
authentication bypass vector.

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

This fixes a critical off-by-one buffer overflow in the HTTP request
handler that could lead to authentication bypass, information disclosure,
or crashes.

The vulnerability occurred in two places:
1. Line 4643: The buffer filling loop allowed writing up to index 251
   (252 bytes) without leaving room for null terminator
2. Line 4704: Null terminator was added without bounds checking, writing
   at index 252 which is out of bounds

Attack scenario:
- Sending exactly 252 bytes would overflow into bPlaceInBuffer variable
- This would reset the buffer index to 0 and create an unterminated string
- Subsequent string operations (strcpy, strncmp) would read beyond buffer
- Could bypass PASSKEY authentication or leak adjacent memory

Fix:
- Limit buffer filling to MaxArrayElement-1 to reserve space for null
- Add bounds checking before writing null terminator
- Ensure string is always properly null-terminated

This prevents any writes beyond the buffer bounds and eliminates the
authentication bypass vector.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@DE-cr
Copy link
Contributor

DE-cr commented Oct 16, 2025

I would say that either of the two change locations suggested by Claude is sufficient, but having both changed won't hurt.
This should be addressed, though, to prevent buffer overflows!

I'd use the opportunity to clean up the code a bit: MaxArrayElement to me suggests that that value would be a valid index for cLineBuffer[], so I would use:

char cLineBuffer[MaxArrayElement+1]; // line 4517: "+1" added

// cLineBuffer[bPlaceInBuffer++]=0; // line 4704: why "++", anyway? remove, and instead add the following:

memset(cLineBuffer,0,sizeof(cLineBuffer)); // add after line 4520: initialize cLineBuffer at start of loop()

...or just use this PR as is ... and leave MaxArrayElement named somewhat inappropriately, but save some CPU cycles in each loop() call by using a simple assignment of a '\0' to terminate the string in cLineBuffer[] instead of initializing that array each time.

@DE-cr
Copy link
Contributor

DE-cr commented Oct 17, 2025

P.s. Actually, I would just change this here:

char cLineBuffer[MaxArrayElement+1]; // line 4517: "+1" added

jack-kerouac and others added 3 commits October 22, 2025 13:27
Add +1 to cLineBuffer array size to prevent out-of-bounds write.
The loop can write up to MaxArrayElement (252) characters at indices
0-251, then writes null terminator at index 252. Original size of
MaxArrayElement caused buffer overflow when writing the terminator.

This minimal fix (suggested by code review) uses MaxArrayElement+1
instead of hardcoding the size, maintaining consistency with the
MaxArrayElement constant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
The post-increment operator on bPlaceInBuffer after null termination serves
no purpose since the variable is reset to 0 at the start of each request
loop (line 4618). This change improves code clarity with no functional impact.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@jack-kerouac
Copy link
Contributor Author

Thanks for the thorough review, @DE-cr! I've updated the PR to use the minimal fix you suggested.

Changes made:

  1. Reverted the previous approach with bounds checking
  2. Applied the simple +1 fix: char cLineBuffer[MaxArrayElement+1];
  3. Bonus cleanup: removed the unnecessary post-increment operator when null-terminating

The array now has 253 bytes (indices 0-252), so the null terminator at index 252 is always valid.

Ready for another look when you have a chance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants