Sanitize paste content by filtering out control chars#8875
Sanitize paste content by filtering out control chars#8875skyline75489 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
@msftbot make sure @DHowett signs off on this |
|
Hello @zadjii-msft! Because you've given me some instructions on how to help merge this pull request, I'll be modifying my merge approach. Here's how I understand your requirements for merging this pull request:
If this doesn't seem right to you, you can tell me to cancel these instructions and use the auto-merge policy that has been configured for this repository. Try telling me "forget everything I just told you". |
zadjii-msft
left a comment
There was a problem hiding this comment.
This seems reasonable to me, but I want Dustin to take a look too, since he's the expert in that particular area.
DHowett
left a comment
There was a problem hiding this comment.
(making my NAK an actual "request changes")
|
I should probably mention that thanks to #8744, the potentially malicious content from the clipboard is being displayed in a dazzling dialog. This should prevent users from pasting it in most conditions. |
|
This PR will still help prevent attacks described in https://security.stackexchange.com/q/39118. Currently the dialog isn't showing invisible control characters. |
555bbe9 to
8fc7f70
Compare
| // and so we append our own \r | ||
| // - For security reasons, most control characters should be removed. | ||
|
|
||
| auto pred = [](wchar_t c) { |
There was a problem hiding this comment.
Inlining this in std::remove_if results in very weird indention using clang-format. Nesting this inside sanitize crashes clang-format(!). So I had to put it here.
| } | ||
|
|
||
| // All ASCII control characters will be removed except HT(0x09), LF(0x0a), | ||
| // CR(0x0d) and DEL(0x7F). |
There was a problem hiding this comment.
Removing CR and LF seems meaningless from a practical point of view. But generally I think this function should remove CR and LF had it not been in this particular context.
There was a problem hiding this comment.
(copied from #7508)
The best resource I can find is here https://security.stackexchange.com/a/52655. It suggest removing every control characters except:
- CR(0x0d) \r
- LF(0x0a) \n
- HT(0x09) \t,
- DEL(0x7F)
Then there's some other opinions in https://bugzilla.gnome.org/show_bug.cgi?id=753197, regarding removing:
- 0x80-0x9F
And here https://bugzilla.gnome.org/show_bug.cgi?id=794653, regarding removing:
- BS(0x08)
- DEL(0x7F)
|
I just want to say I do intentionally paste text with VT control sequences into the terminal, usually just for colors or other effects. So I'm not sure how I feel about |
|
I suspect you are in the minority here. Many other terminal emulators filter out control characters during bracketed paste, and an application shouldn't be expecting to receive raw SGRs in its input stream anyway. |
|
It's just that... every setting we add is another combinatorial factor in our testing, right? There's already |
|
Good point, possible usecases were testing string snippets for shell prompts (bash Since you said |
|
The code change in this file, and the policy we're instating here, implicates only paste. We're not breaking Win32 APIs willy-nilly. 😄 Also: PowerShell Core supports "`e" for escaping escape. |
|
I am no expert on security issue like this. I think this PR needs to be thoroughly reviewed. Feel free to tag this to 1.7 or future release. No need to rush this one. |
|
I think I should also add this to conhost. But I don't know conhost that much. I'll file a issue for conhost if this is merged. |
|
I appreciate this, but I do think that it needs some changes. I'm not a huge fan of the lambda filtering implementation; I think that it would be much better for us to have one loop that checks for As it is implemented in this PR, we scan the string to determine that it doesn't contain Same concern about the bracketed paste function. Why have a lambda? Lastly- I wonder if this should be inside TerminalCore so that the WPF control can use it. Needing the correct filtering for \r\n and for control sequences is not something unique to the TerminalControl! |
|
The main reason I used lambda is to minimize the modification to the existing code. Yea it does introduced extra scan loop. I can’t blame you for wanting this to run faster. I’ll try to refactor this later. |
|
Closed in favor of #9034 |


Summary of the Pull Request
Address the security issued mentioned in #395 (comment)
References
Supersedes #7508
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed