-
Notifications
You must be signed in to change notification settings - Fork 456
Strip Unicode Cf characters in PrintableString
#4593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tnull
wants to merge
2
commits into
lightningdevkit:main
Choose a base branch
from
tnull:2026-05-printable-string-bidi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,14 +31,50 @@ impl<'a> fmt::Display for PrintableString<'a> { | |
| fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { | ||
| use core::fmt::Write; | ||
| for c in self.0.chars() { | ||
| let c = if c.is_control() { core::char::REPLACEMENT_CHARACTER } else { c }; | ||
| let c = if c.is_control() || is_format_char(c) { | ||
| core::char::REPLACEMENT_CHARACTER | ||
| } else { | ||
| c | ||
| }; | ||
| f.write_char(c)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| // Codepoints in Unicode general category `Cf` (Format), per Unicode 16.0. These are not matched | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per some claude exploring of this patch, this should also be valid for Unicode 17.0. Any reason to not specify the latest here? |
||
| // by `char::is_control` (which only covers `Cc`), but include the bidirectional override / isolate | ||
| // controls (e.g. U+202E RLO) and zero-width characters behind the "Trojan Source" attack family | ||
| // (CVE-2021-42574), where an attacker-supplied string renders to a human reader as something other | ||
| // than its byte content. Strip them alongside `Cc` characters when sanitising untrusted input. | ||
| fn is_format_char(c: char) -> bool { | ||
| matches!( | ||
| c as u32, | ||
| 0x00AD | ||
| | 0x0600..=0x0605 | ||
| | 0x061C | ||
| | 0x06DD | ||
| | 0x070F | ||
| | 0x0890..=0x0891 | ||
| | 0x08E2 | ||
| | 0x180E | ||
| | 0x200B..=0x200F | ||
| | 0x202A..=0x202E | ||
| | 0x2060..=0x2064 | ||
| | 0x2066..=0x206F | ||
| | 0xFEFF | ||
| | 0xFFF9..=0xFFFB | ||
| | 0x110BD | ||
| | 0x110CD | ||
| | 0x13430..=0x1343F | ||
| | 0x1BCA0..=0x1BCA3 | ||
|
tnull marked this conversation as resolved.
|
||
| | 0x1D173..=0x1D17A | ||
| | 0xE0001 | ||
| | 0xE0020..=0xE007F | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::PrintableString; | ||
|
|
@@ -50,4 +86,27 @@ mod tests { | |
| "I \u{1F496} LDK!\u{FFFD}\u{26A1}", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sanitizes_unicode_bidi_override_characters() { | ||
| // U+202E RIGHT-TO-LEFT OVERRIDE and friends are Unicode general category | ||
| // `Cf` (Format), not `Cc` (Control). They enable "Trojan Source" / | ||
| // bidi-spoofing attacks where an attacker-supplied string (e.g. a node | ||
| // alias gossiped from a peer) renders to a human reader as something | ||
| // other than its byte content. `PrintableString` is the sanitiser used | ||
| // for exactly these untrusted strings, so it must replace them. | ||
| let rendered = format!("{}", PrintableString("safe\u{202E}cipsxe.exe")); | ||
| assert!( | ||
| !rendered.contains('\u{202E}'), | ||
| "PrintableString left a U+202E RLO override in its output: {:?}", | ||
| rendered | ||
| ); | ||
|
|
||
| // U+13440 is in the Egyptian Hieroglyph Format Controls block, but its | ||
| // general category is `Mn`, not `Cf`, so the `Cf` range ends at U+1343F. | ||
| assert_eq!( | ||
| format!("{}", PrintableString("x\u{1343F}y\u{13440}z")), | ||
| "x\u{FFFD}y\u{13440}z" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.