-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Add initial support for paste filtering and bracketed paste mode #7508
Add initial support for paste filtering and bracketed paste mode #7508
Conversation
#include "precomp.h" | ||
#include "inc/PasteConverter.h" | ||
|
||
using namespace Microsoft::Console::Utils; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't honestly know where to put this class. Right now it is heavily VT-related. But in the future maybe other features will be added, making it less VT-related. So perhaps Types
is also OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep this VT-specific for now. It can be refactored later if need be.
// - enabled - true to enable, false to disable. | ||
// Return value: | ||
// True if handled successfully. False otherwise. | ||
bool AdaptDispatch::EnableBracketedPasteMode(const bool /*enabled*/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what to do with conhost. Those legacy code always scare me . Call we just leave it like this?
{ | ||
// For security reasons, control characters should be filtered. | ||
// Here ASCII control characters will be removed, except HT(0x09), LF(0x0a), CR(0x0d) and DEL(0x7F). | ||
converted.erase(std::remove_if(converted.begin(), converted.end(), [](wchar_t c) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I try to minimize the copy opertion so I choose erase
instead of regex_replace
. Performance-wise, I think this should be enough for most cases.
return false; | ||
} | ||
|
||
return c >= L'\x00' && c <= L'\x08' || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since security is my thing even more than dev stuff, I would agree all of these should be disabled by default. I would also agree we should probably put it behind a setting for certain control characters to be allowed such as BS and DEL with nice warning basically saying "you're on your own if you get hacked".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @WSLUser. That’s a great idea, to give users more options controlling paste behavior. In fact many terminals offer these kind of options so I figure a dedicated class for pasting is a must.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's some confusion here about what bracketed paste is actually meant to do. On most terminals I've tested, it doesn't do any additional filtering - it just adds the escape sequences at the beginning and end of the paste (that's the "bracketing").
While some terminals do filter out certain control characters, or convert them in some way, they do that all the time - not just when bracketed paste mode is enabled. So I think you're kind of mixing two different features here - paste filtering (which should have nothing to do with bracketed paste mode), and then the bracketed paste mode itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks now I realize that. It’s just we don’t have a setting for people to enable/disable filtering pasted content. So I think that can be added later with another flag SanitizeContent or something. Or should we just filter them all without letting people choose.
In my imagination there will be more flags coming in the future and do various kinds of things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand wanting to add support for paste filtering (although I would have expected that in a PR named something like "Add paste filtering"). I also understand leaving the option to enable or disable it for a follow-up PR. What I don't understand is why we want to tie this to bracketed paste - it's a completely separate concept.
If we want to make the paste filtering controllable via an escape sequence for some reason, then we should be inventing our own escape sequence for that - not repurposing an existing sequence. I don't know if I've just misunderstood the intention of this PR, but it make no sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I kind misunderstand the relation between filtering escape sequence and adding support for bracketed paste mode. I’ll try to make this better.
So, full disclosure: I tried to implement bracketed paste support in conhost before Terminal was even a thing. There's a number of interrelated issues here, and I'm not sure what percentage of problem space coverage I want for this. We decided to hold off on my initial implementation (https://github.com/microsoft/terminal/tree/dev/duhowett/bracketed_paste (wow: last updated "2 years ago")) because we're lacking a higher level abstraction for input types in conhost. There's a class that handles the translation of clipboard input into raw key events, but it does so at paste time instead of at read time. It can't necessarily know that the terminal is in VT input mode & that bracketed paste is turned on. We considered introducing a "ClipboardPasteEvent" that would be unspooled into individual input records on the input buffer when the event was read, and simultaneously we would move VT translation from queue insertion to queue removal to unify around a single input buffer paradigm. For various reasons -- some of which are documented in #4954 (comment) -- we never did this. I'm wondering if it makes sense to leverage our ability to do VT passthrough directly from an application to the terminal (and back) and keep the bracketed paste implementation in terminal only. |
I think I prefer it that way actually. Due to the nature of this particular feature, it makes more sense for it to be an app-specific feature for terminals utilizing passthrough mode. |
@WSLUser it bears understanding that conhost is a terminal as well, we just suppress the use of many of its terminal features when we run it inside WT |
It was also my first thought, to add something like This is a feature that I've been craving for in WT. It works smooth for iTerm2 on macOS and many terminals on Linux. |
@skyline75489 I don't know what stage you're in implementing this, but I built windows terminal with your commits and it works. I'm already using this. Just wanted to say thanks. |
@eyalz800 I'm glad my work helps. Unfortunately I think this PR is still in its very early stage. Besides this issue itself belongs to the "Console Backlog" category which means it will not be prioritized until more urgent issues are resolved. I will keep an eye on this one and try to push my work forward. But it's going to take some time. |
@skyline75489 Would you be open to splitting the PR into an initial pull for bracketed paste mode, and a follow-up for control-code filtering? Would that help this land faster? Bracketed paste alone would be an excellent usability improvement for Terminal. |
103ea8d
to
2fdf153
Compare
@skyline75489 Many thanks for the effort here. I do echo @asford as I think it would be great if we could at least get bracketed paste mode in. Perhaps following the suggestion to split the PR would help accelerate review and a merge in the repo code base? |
Thanks guys for your interest in this project. Right now we are on our way towards 1.5 release. I feel that bracketed paste might be able to land in 1.6 or 1.7. I understand your eagerness. I really do. I am the one who filed this PR, after all. Still this is a backlog issue which means we have more important things to work on, for example, settings UI. I will consider reconstructing this PR later.
Thanks again for your enthusiasm in this project.
获取 Outlook for Android<https://aka.ms/ghei36>
|
Closed in favor of #8840 . |
This adds the skeleton code for "bracketed paste mode" to the Windows Terminal. No actual functionality is implemented yet, just the wiring for handling DECSET/DECRST 2004. References microsoft#395 Supersedes microsoft#7508
This adds "paste filtering" & "bracketed paste mode" to the Windows Terminal. I've moved the paste handling code in `TerminalControl` to `Microsoft::Console::Util` to be able to easily test it, and the paste transformer from `TerminalControl` to `TerminalCore`. Supersedes #7508 References #395 (overall bracketed paste support request) Tests added. Manually tested.
FYI with #9034 merged the bracketed paste mode support in Windows Terminal should land in 1.7 preview. Thanks everyone for the support and suggestions. Cheers! 🎉 |
Summary of the Pull Request
This adds "bracketed paste mode" to the Windows Terminal.
References
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed
Manually tested.
Unit testing TBD.