Repository navigation
bitcoin/short_channel_id: validate strict decimal format in short_channel_id_from_str - #9701
Open
bhuvan-somisetty wants to merge 1 commit into
Open
bhuvan-somisetty wants to merge 1 commit into
bhuvan-somisetty wants to merge 1 commit into
Conversation
Andezion
reviewed
Nov 11, 2026
| assert(!short_channel_id_from_str("x2x3", 4, &scid)); | ||
| assert(!short_channel_id_from_str("1xx3", 4, &scid)); | ||
|
|
||
| /* Out of range components */ |
Collaborator
There was a problem hiding this comment.
maybe we should add test cases for values above 2^32 and 2^16, and for a value above 2^64 (str_to_u64 must reject it)
| if (!x2) | ||
| return false; | ||
|
|
||
| if (memchr(x2 + 1, 'x', strlen - (x2 + 1 - str))) |
Collaborator
There was a problem hiding this comment.
the third memchr is not needed, str_to_u64() already fails on any character that is not a digit, including x. So 1x2x3x4 is rejected at line 64 anyway
…nnel_id_from_str
short_channel_id_from_str previously used sscanf with "%ux%ux%hu".
This accepted strings with trailing unparsed characters (e.g.
"1x2x3garbage", "1x2x3/0"), negative numbers (via %hu casting to 65535,
e.g. "1x2x-1"), signs ("+1x2x3"), and whitespace (" 1x2x3").
Switch to strict delimiter checking using memchr and strict decimal
parsing with str_to_u64, matching BOLT 7 and preventing invalid short
channel ID strings in RPC and gossip parsing.
Add comprehensive unit tests in bitcoin/test/run-short_channel_id.c.
Changelog-Fixed: bitcoin: reject invalid short_channel_id strings with trailing characters, signs, negative numbers, or whitespace
Fixes: ElementsProject#9700
Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
bhuvan-somisetty
force-pushed
the
fix-scid-parsing-validation
branch
from
November 11, 2026 20:03
cdfa10c to
c40008a
Compare
Contributor
Author
|
Thanks @Andezion! Removed the extra |
This branch has not been deployed
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.
Description
Fixes #9700
short_channel_id_from_strpreviously usedsscanf("%ux%ux%hu", &blocknum, &txnum, &outnum) == 3. This had several validation issues:sscanfdoes not verify that the full string buffer was consumed, silently accepting strings with trailing garbage (e.g."1x2x3garbage","1x2x3/0","1x2x3x4","1x2x3 4").%huaccepts negative numbers (e.g."-1"), converting them to unsigned 16-bit integers (65535), which passesmk_short_channel_id()bounds checks.sscanfaccepts leading and intermediate whitespace as well as+signs.short_channel_id_dir_from_strand JSON-RPC parameter parsing.This change:
sscanfwith strict parsing usingmemchr()for'x'delimiters andstr_to_u64()for strict decimal parsing of each segment.bitcoin/test/run-short_channel_id.ccovering canonical formats, edge values, negative numbers, signs, trailing characters, and invalid directions.Checklist
Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:
tools/lightning-downgrade(N/A, in-memory string parsing only).