Skip to content
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

Closed

Conversation

skyline75489
Copy link
Collaborator

Summary of the Pull Request

This adds "bracketed paste mode" to the Windows Terminal.

References

PR Checklist

  • Closes bracketed paste in conhost #395
  • CLA signed. If not, go over here and sign the CLA
  • Tests added/passed
  • Documentation updated. If checked, please file a pull request on our docs repo and link it here: #xxx
  • Schema updated.
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Manually tested.

Unit testing TBD.

@ghost ghost added Area-VT Virtual Terminal sequence support Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conhost For issues in the Console codebase labels Sep 2, 2020
#include "precomp.h"
#include "inc/PasteConverter.h"

using namespace Microsoft::Console::Utils;
Copy link
Collaborator Author

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?

Copy link
Contributor

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*/)
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

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' ||
Copy link
Collaborator Author

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)

Copy link
Contributor

@WSLUser WSLUser Sep 2, 2020

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

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

Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@DHowett
Copy link
Member

DHowett commented Sep 2, 2020

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.

@WSLUser
Copy link
Contributor

WSLUser commented Sep 2, 2020

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.

@DHowett
Copy link
Member

DHowett commented Sep 2, 2020

@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

@skyline75489
Copy link
Collaborator Author

It was also my first thought, to add something like PasteEvent to make bracketed paste happen in both conhost and WT. Then the complexity of it just stoped me from actually implementing it. So I changed my mind to make a minimium but working one only for WT.

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 skyline75489 changed the title Add initial support for bracketed paste mode Add initial support for paste filtering and bracketed paste mode Sep 3, 2020
@eyalz800
Copy link

eyalz800 commented Oct 3, 2020

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

@skyline75489
Copy link
Collaborator Author

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

@asford
Copy link

asford commented Oct 14, 2020

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

@microsoft-github-updates microsoft-github-updates bot changed the base branch from master to main October 22, 2020 00:27
@musm
Copy link

musm commented Nov 7, 2020

@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?

@skyline75489
Copy link
Collaborator Author

skyline75489 commented Nov 8, 2020 via email

@skyline75489
Copy link
Collaborator Author

Closed in favor of #8840 .

ghost pushed a commit that referenced this pull request Jan 22, 2021
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 #395
Supersedes #7508
@skyline75489 skyline75489 deleted the chesterliu/dev/bracketed-paste branch January 25, 2021 02:57
mpela81 pushed a commit to mpela81/terminal that referenced this pull request Jan 28, 2021
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
ghost pushed a commit that referenced this pull request Feb 8, 2021
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.
@skyline75489
Copy link
Collaborator Author

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! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-VT Virtual Terminal sequence support Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bracketed paste in conhost
7 participants