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

Ouverte Adaptée aux débutants
#9,700 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

@bhuvan-somisetty y travaille déjà.

Depuis le 5/11/2026.

  • #9701 par @bhuvan-somisetty — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
83/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
c

Piste de recherche

Commencez par short_channel_id_from_str() dans bitcoin/short_channel_id.c et suivez short_channel_id_dir_from_str(), qui délègue à cette fonction. Vérifiez comment le parseur actuel traite les exemples invalides fournis, puis assurez-vous que l’analyse rejette les entrées mal formées tout en acceptant le format décimal spécifié à trois composants. Le travail est terminé lorsque les exemples comportant des caractères finaux, des valeurs négatives, des signes ou des espaces sont rejetés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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).

Langage dominant
C
Étoiles
3.1k
Forks
1k
Merge moyen
4 j 21 h
PR mergées (30 j)
21

Préparer son environnement

  • Fournit un Dockerfile ou un fichier Docker Compose
  • Propose un modèle de pull request
  • Aucun guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de ElementsProject/lightning

Toutes les issues de ElementsProject/lightning

Issues similaires

Plus d'issues C

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.