Skip to content

bitcoin/short_channel_id: short_channel_id_from_str accepts trailing garbage, negative numbers, and whitespace #9700

Open

Description

@bhuvan-somisetty

Issue and Steps to Reproduce

short_channel_id_from_str() in bitcoin/short_channel_id.c parses short channel ID strings using sscanf:

bool short_channel_id_from_str(const char *str, size_t strlen,
			       struct short_channel_id *dst)
{
	u32 blocknum, txnum;
	u16 outnum;

	if (sscanf(str, "%ux%ux%hu", &blocknum, &txnum, &outnum) == 3)
		return mk_short_channel_id(dst, blocknum, txnum, outnum);
	return false;
}

This implementation allows several invalid string formats to succeed:

  1. Trailing garbage / unparsed characters: sscanf does not check if the entire buffer length (strlen) is consumed. Strings such as "1x2x3garbage", "1x2x3/0", "1x2x3x4", or "1x2x3 4" successfully return true with valid dst filled from the initial match.
  2. Negative outnums: %hu accepts negative numbers (e.g. "-1"), parsing them as 65535 ((u16)-1). In mk_short_channel_id(), (outnum & 0xFFFF) != outnum evaluates to 65535 != 65535 which is false, so "1x2x-1" is accepted as valid channel output 65535.
  3. Leading / internal whitespace and signs: sscanf skips whitespace before numeric conversions and parses leading + signs, allowing strings like " 1x2x3", "1x 2x3", or "+1x2x3".
  4. short_channel_id_dir_from_str pollution: Because short_channel_id_dir_from_str delegates to short_channel_id_from_str(str, slash - str, ...), strings like "1x2x3garbage/0" are also parsed without error.

Steps to reproduce:

struct short_channel_id scid;
assert(short_channel_id_from_str("1x2x3garbage", strlen("1x2x3garbage"), &scid) == false); // Fails (returns true)
assert(short_channel_id_from_str("1x2x-1", strlen("1x2x-1"), &scid) == false);             // Fails (returns true)

Expected behavior:

Per BOLT 7, a short channel ID is uniquely represented by three decimal components delimited by 'x': <blocknum>x<txnum>x<outnum>. short_channel_id_from_str() should strictly parse the entire provided string and reject trailing garbage, negative values, signs, and whitespace.

getinfo output

N/A (observed in bitcoin/short_channel_id.c across all CLN versions).

Activity

  1. added a commit that references this issue on Nov 11, 2026
    c40008a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions