News for hackers. Stuff that matters.
Re:Don't touch my clipboard by smichel17 on Monday February 17, 2020 @09:23PM (#22352893)
The wrongest thing about this, from my perspective, is that my browser fires off a js 'copy' event when I press control-c. There are times when I've found it helpful that a browser can copy text to my clipboard when I click a button, but I can't think of a single time when I want a site to react to my attempt to copy text off if it.

Is there any way to configure my user agent (Firefox) not to do this? A hack is ok.

Re:Don't touch my clipboard by Latty on Monday February 17, 2020 @11:19PM (#22353419)
Even that won't totally save you. While you can't do exactly this, you can sure get the fun experience of the user getting text they didn't expect in the clipboard when copying without any JS at all.

Text is highlighted on the page based on the code order, not as it appears on-screen (at least in Firefox and Chrome). If you throw elements off-screen with some CSS, you can create a big disparity between what the user thinks they are selecting and what they get when they copy/paste.

e.g: https://jsfiddle.net/Lbc5gsjm/

Not quite as neat as you can't put it dynamically around exactly what you select, but my favourite example of where you could get pretty malicious with it is a “plain text” link that injects a suffix to the hostname when you copy it.

Re:Don't touch my clipboard by chiefsucker on Tuesday February 18, 2020 @06:01AM (#22355051)
This works when you manually select the text, but it fails when you want to select by double-/triple-clicking. In this case the selection ends before the word “me”.

It’s awkward how easily one can break a basic functionality like select & copy.

Re:Don't touch my clipboard by Klathmon on Tuesday February 18, 2020 @11:37AM (#22357253)
There are plenty of other tricks that can be used and won't break the "triple click". I just threw together [1] as an example (and in that one, even if you don't triple click but accidentally select past the end of the line, you are also getting unexpected text).

Combine a few of those techniques with a fancy-looking text box that you are supposed to "click to copy" to get a command, and it becomes pretty easy to even write css-only "exploits" to put stuff in the clipboard!

[1] https://jsfiddle.net/gxosfn83/1/

Re:Don't touch my clipboard by zzo38computer on Tuesday February 18, 2020 @08:43PM (#22362367)
Yes, that is another problem, although it involves CSS. I suggested before to have a ARIA view, which would cause it to use ARIA properties rather than CSS to decide the rendering. I have not thought of your example though, but it is a feature I would like to have. (ARIA view would do other things too.)
Re:Don't touch my clipboard by cortesoft on Monday February 17, 2020 @09:45PM (#22353026)
I think the wrongest thing is that this is a clear attack vector... make a site with helpful Linux shortcuts, then replace every copy with "curl malicious script and run it, plus a newline to make it run immediately"
Re:Don't touch my clipboard by oefrha on Monday February 17, 2020 @09:47PM (#22353037)
> plus a newline to make it run immediately

I already mentioned this in another comment, but everyone should enable bracket paste mode in their shell to defend against this.

https://cirw.in/blog/bracketed-paste

Re:Don't touch my clipboard by umvi on Monday February 17, 2020 @11:44PM (#22353518)
I always just type "#" first before pasting... That way it is just a comment I can inspect before running
Re:Don't touch my clipboard by hackmiester on Tuesday February 18, 2020 @12:06AM (#22353611)
Here, paste this into your terminal:

    echo "hello"
    echo "lol" ; sudo sl -rf /
(In case it's not obvious, the # trick will not help you.)
Re:Don't touch my clipboard by umvi on Tuesday February 18, 2020 @12:52PM (#22358214)
True, the comment trick does not work for multi-line pastes
Re:Don't touch my clipboard by jtbayly on Tuesday February 18, 2020 @08:17AM (#22355768)
So... don’t paste it in my terminal?
Re:Don't touch my clipboard by hackmiester on Wednesday February 19, 2020 @04:29PM (#22370291)
heh, yes. Admittedly, immediately after posting, I did change it from 'rm', which would not be a good idea to run, to 'sl', which ... well, try it (or `man sl`), it's fun.
Re:Don't touch my clipboard by dredmorbius on Tuesday February 18, 2020 @03:33AM (#22354414)
If you're using Readline (most Bash or similar shells do), then C-x C-e will invoke the editor, and you can paste the output into that.

Pray for no escape, bang, or control sequences.

(Alternatively: "r! cat <paste> <ctrl>-D" will read into a vim session.)

Examine the output, trim the unwanted / dangerous bits, and run (or save to a file/script).

Since I run what are ... generously ... considered "bash one-liners" all the time, that key sequence is locked into my muscle memory. It's a convenient way to invoke an editor immediately and run code from it.

Re:Don't touch my clipboard by majewsky on Tuesday February 18, 2020 @09:56AM (#22356466)
> :r! cat <paste> <ctrl>-D

Alternatively, call a utility that can read the clipboard directly. For Xorg:

  :r! xsel -b
For Wayland:

  :r! wl-paste
Re:Don't touch my clipboard by dredmorbius on Tuesday February 18, 2020 @10:11AM (#22356572)
That depends on whether or not the clipboard is directly accessible or not. When invoking an editor remotely via SSH, for example, it's not. Though there's often the ability to past into the controlling terminal locally.

I do this frequently on Android via Termux, ssh'ing to remote hosts.

Re:Don't touch my clipboard by chrisweekly on Monday February 17, 2020 @11:57PM (#22353563)
Thanks for the safety net tip. I might be more methodical than most, but in copying commands from external sources (ie, ~anything other than my own code or notes), I paste into a local buffer to examine and confirm contents. I do this as both as a means to generate documentation as well as for hygiene per se -- and it occasionally helps to sanity check my intention and prevent a footgun.
Re:Don't touch my clipboard by oefrha on Tuesday February 18, 2020 @12:10AM (#22353630)
Bracketed paste is basically a local buffer (that won’t be executed unless you explicitly do so).
Re:Don't touch my clipboard by dylan604 on Monday February 17, 2020 @10:42PM (#22353270)
Thanks for this. I had never heard of it before, but will definitely be playing around with it.
Re:Don't touch my clipboard by Latty on Monday February 17, 2020 @11:29PM (#22353457)
As I also noted in a sibling comment, you don't even need JS to do that: https://jsfiddle.net/eaL153uz/
Re:Don't touch my clipboard by lukastyrychtr on Tuesday February 18, 2020 @02:00AM (#22354046)
I might start to be grateful for the screen reading software - these tricks fortunately don't work when you use NVDA or Orca, because they have its own virtual buffer and they intercept the copy command soon enough that the browser has no idea about it.
Re:Don't touch my clipboard by bloopernova on Tuesday February 18, 2020 @10:53AM (#22356885)
I always paste into a text document to examine what's there. Then copy from that and paste.
Re:Don't touch my clipboard by kevin_thibedeau on Monday February 17, 2020 @11:38PM (#22353488)
Under X11, Firefox sets the primary selection correctly and middle-click paste is unaffected. This site also renders fine with JS blocked. That is a useful default to prevent this kind of crap from interfering with you.

What is infuriating is CSS that prevents text selection in the first place. That insult to usability should have never been adopted by browsers.

Re:Don't touch my clipboard by franga2000 on Tuesday February 18, 2020 @12:58PM (#22358309)
> That insult to usability should have never been adopted by browsers

As a web developer, it's kind of funny seeing people exclaim "this feature should not exist" after seeing a few abuses of it, even though they probably benefit from it every day without realizing it. Overriding copying is needed for wysiwyg editors and selection blocking is needed for many kinds of drag&drop interfaces and also editors.

Instead of complaining that useful features exist because they can be misused, maybe complain about the misuse itself - maybe even to the people actually misusing them. Shoot them an email and they might actually do something about it. Most of these misuses hurt accessibility (screen readers, etc.), so if nothing else, they'll see it as a way to reach more customers.

Re:Don't touch my clipboard by jhasse on Tuesday February 18, 2020 @02:42AM (#22354217)
It can be useful. For example I don't want to select the line nunbers next to the code.
Re:Don't touch my clipboard by maple3142 on Monday February 17, 2020 @11:14PM (#22353397)
> I can't think of a single time when I want a site to react to my attempt to copy text off if it.

I think it is needed for some complex web app to handle copying non-text content. Such as images in wysiwyg editor, Google Sheets/Slides...

Re:Don't touch my clipboard by na85 on Monday February 17, 2020 @11:23PM (#22353432)
So trade those apps not working for immunity from JavaScript clipboard hijacking?

I'd be 110% fine with that trade and nothing of value to me would be lost.

Is it possible in Firefox? Anyone know?

Re:Don't touch my clipboard by dwaltrip on Tuesday February 18, 2020 @12:25AM (#22353703)
Millions of people use applications with these kinds of features. A few more examples: the Scratch educational programming tool, website builders such as Webflow, diagram editors, image editors, etc. The list goes on and on.

The browser is no longer just a document viewer... That ship has sailed, and overall it is a good thing.

We can mitigate the risk of clipboard hijacking without burning down the house. By the way, I would guess that this is a minor risk in the grand scheme of things, as it seems that the worst risks are for technical individuals people copying programmatic commands (e.g. software engineers). For others, it is a real yet minor annoyance.

Perhaps there could be an opt-in setting for allowing a site to modify the default content that is copied from a selection?

Re:Don't touch my clipboard by wtallis on Tuesday February 18, 2020 @07:45AM (#22355556)
> The browser is no longer just a document viewer... That ship has sailed, and overall it is a good thing.

No, it really isn't. Web apps and documents using the same underlying technologies doesn't mean they have to be accessed through a single frontend that provides the worst of both worlds.

Re:Don't touch my clipboard by dwaltrip on Tuesday February 18, 2020 @07:24PM (#22361882)
> Worst of both worlds

I don't think that is remotely the case.

Re:Don't touch my clipboard by Polylactic_acid on Tuesday February 18, 2020 @01:24AM (#22353912)
A decent workaround would be to have 2 clipboards. The regular untouched one and the special one. Then when you paste, apps which only take plain text will grab the regular one and apps which accept formatted copying will grab the special clipboard but also provide a "paste as plain text" so the user gets what they want every time.
Re:Don't touch my clipboard by Freak_NL on Tuesday February 18, 2020 @02:21AM (#22354143)
You don't have two clipboards? It's been the default on *nix OSes for ages. Ctrl+C/Ctrl+V works, but there is also the select/middle-mouse clipboard. Great for crap websites that hijack one.
Re:Don't touch my clipboard by ken on Tuesday February 18, 2020 @09:06AM (#22356123)
It sounds like you're conflating two orthogonal concepts: multiple named clipboard locations, and multiple data types on one clipboard. Both already exist, and are how clipboards on major platforms have worked for decades.

The Javascript interface isn't aware of these distinctions, though I'm not sure I want it to. Web apps like this that abuse one plain text clipboard will abuse multiple richly typed clipboards, too.

Re:Don't touch my clipboard by oneeyedpigeon on Tuesday February 18, 2020 @03:40AM (#22354442)
Those apps are surely already doing that - I don't think there's a way of copying arbitrary structured data to the web's abstraction of the clipboard. It doesn't address the problem the op raised - that browsers can sniff the copy event.
Re:Don't touch my clipboard by dredmorbius on Tuesday February 18, 2020 @03:42AM (#22354451)
There are already (at least) two clipboards in many platforms.

X11/Xorg has the primary and secondary selections.

MacOS has ... whatever it's got.

A key problem with this is that the feature is covert, latent, poorly discoverable, and causes unexpected behaviours. Even presumably advanced users (myself, you, other HN readers) are poorly aware of this. Imagine trying to explain to your nontechnical Aunt Tilly or Uncle Kamlesh about these "different clipboards which treat what you've copied differently and access through this funky interface"?

Re:Don't touch my clipboard by maple3142 on Monday February 17, 2020 @11:26PM (#22353444)
If you really don't mind it will break some websites, then you really can disable it in Firefox.

https://news.ycombinator.com/item?id=22352929

Re:Don't touch my clipboard by epse on Tuesday February 18, 2020 @01:26AM (#22353922)
Mind you that this breaks a LOT of things. Even stupid stuff like any textbox on Facebook will be broken.
Re:Don't touch my clipboard by TuringTest on Tuesday February 18, 2020 @05:38AM (#22354961)
That sounds like a net positive, allowing you to tell apart proper websites from privacy nightmares :-P
Re:Don't touch my clipboard by triceratops on Tuesday February 18, 2020 @01:39AM (#22353961)
Maybe let users grant clipboard permissions, as they currently do for location, microphone, webcam or notifications?
Re:Don't touch my clipboard by bzbarsky on Tuesday February 18, 2020 @10:29AM (#22356714)
Re:Don't touch my clipboard by bzbarsky on Tuesday February 18, 2020 @10:27AM (#22356697)
There's the "dom.event.clipboardevents.enabled" preference, which you can set to false in about:config.

I don't know whether that's planned to stick around or whether it was added when the clipboard event support was still experimental and will be removed at some point. But I suspect the former, for precisely the reasons in this thread.

Note that disabling these events will likely break "smart" copy/paste in things like Google Sheets, which is one reason it's not disabled by default....

Re:Don't touch my clipboard by zzo38computer on Tuesday February 18, 2020 @08:45PM (#22362379)
OK, it looks like I have already disabled that (although I forgot about it until I looked now).
Re:Don't touch my clipboard by 0zymandiass on Monday February 17, 2020 @09:28PM (#22352929)
about:config -> dom.event.clipboardevents.enabled = false

You can't do it through a user agent, though

Re:Don't touch my clipboard by ebg13 on Monday February 17, 2020 @09:31PM (#22352943)
I'm pretty sure this breaks copying from google docs.
Re:Don't touch my clipboard by 0zymandiass on Monday February 17, 2020 @09:36PM (#22352974)
I can highlight and ctrl+c without issue in their Writer clone. Never tried any others.

I've only ever seen that setting fix sites that try to prevent you from copying.

Re:Don't touch my clipboard by josephg on Monday February 17, 2020 @09:48PM (#22353045)
I’ve used clipboard events for legitimate uses - for example I use them in this pseudo Turing sandpit / visual programming system to move, copy and export content: https://steam.dance/ (you can hold shift for a selection box and once selected you can copy, etc).

But I wouldn’t be surprised if stuff like this is the exception rather than the rule. I feel like most news sites and blogs are improved with JavaScript disabled.

Re:Don't touch my clipboard by 0zymandiass on Monday February 17, 2020 @09:55PM (#22353067)
You're getting far enough into JS knowledge that I don't have.

Do you actually need clipboard events, or just dom.event.contextmenu.enabled for that?

Re:Don't touch my clipboard by zamalek on Monday February 17, 2020 @11:20PM (#22353423)
Put another way, on a website like draw.io, you need to be able to hook into copy to allow lassoing and copying a set of shapes (and have them remain editable when pasted).

The entire ask (stopping the copyright, and allow clipboard integration) be achieved by allowing pages to augment the clipboard data, which can already be done, but not replace it. Pages would still need to be able to hook paste.

Re:Don't touch my clipboard by smichel17 on Tuesday February 25, 2020 @11:11AM (#22414309)
Being able to hook paste is a much more reasonable ask. If you have js enabled, they're going to be able to access the content after paste anyway, so it's reasonable to allow notification that a paste has occurred.
Re:Don't touch my clipboard by Carpetsmoker on Tuesday February 18, 2020 @02:12AM (#22354101)
Be aware that some websites will completely break when pasting when this is disabled (e.g. Twitter, Facebook, probably more).
Re:Don't touch my clipboard by pbhjpbhj on Tuesday February 18, 2020 @03:45PM (#22360211)
Pasting is completely broken on Facebook for me anyway, if I paste at the start of a line it deletes the whole comment box. Originally I thought it was related to this config setting but toggling it made no difference.

So many lost rants ... ;o)

Re:Don't touch my clipboard by ori_b on Monday February 17, 2020 @09:52PM (#22353063)
You can turn off JavaScript. This has the added benefit of disabling most tracking and advertising.
Re:Don't touch my clipboard by patrec on Tuesday February 18, 2020 @07:15AM (#22355373)
Protip: in Safari you can assign a keyboard shortcut to toggle javascript (Preferences -> Keyboard -> Shortcuts -> App Shortcuts -> [+] -> Application: "Safari" Menu Title: "Disable JavaScript" Shortcut: "Cmd+Shift+J". >90% of websites immediately become massively more pleasant and you can quickly toggle it on when actually needed.

For mobile devices, easiest way is to use two different browsers (one with JS disabled).

Re:Don't touch my clipboard by wtmt on Tuesday February 18, 2020 @08:43AM (#22355967)
I presume this toggle applies to all windows and tabs. As an example, if some page uses a meta refresh tag to periodically refresh it and also has JS (which wasn’t loaded before), the JS would get loaded at a future point when it’s enabled through this option (though it may be for some other tab in focus that the user enabled it for)?

A per tab setting that remembers the JS enabled state would be very useful, with the default being JS disabled.

Re:Don't touch my clipboard by patrec on Tuesday February 18, 2020 @08:53AM (#22356038)
Yeah, it's global, and unfortunately I believe that Apple has gimped extensions so something like NoScript is no longer possible. If you need granularity or whitelisting of domains, I'm afraid you need another browser. On the glass half full side, being a global switch means it's less mental overhead – you have a single additional modal bit instead of O(tabs).
Re:Don't touch my clipboard by Polylactic_acid on Tuesday February 18, 2020 @01:25AM (#22353914)
As well as disabling most of the internet. It makes more sense to turn off the bad apis individually and block known tracking domains.
Re:Don't touch my clipboard by dredmorbius on Tuesday February 18, 2020 @03:49AM (#22354477)
We've been through the blacklisting argument numerous times: spam, viruses, adware, malware, spyware, browser adblocking, etc.

It ... ultimately doesn't scale. The black-hat namespaces are too large, and treating as TOFU eventually proves untenable.

Advanced features -- anything beyond rendering basic HTML, and I'd be quite prepared to argue for a very limited subset of that -- should be expressly prohibited unless enabled.

The trick is to make enabling reasonably painless and consequence-free (e.g., enable into a sandbox). There are still the problems of users gratuitously enabling anything and everything (especially when prompted through website notifications, pop-ups, phishing and vishing social engineering attacks, etc.), as well as the problem of largely invisible second and higher-order effects.

But the key is to cut down on the effectiveness of such methods, to impose costs on websites for employing them, and to make black-hat attacks through these too expensive by reducing the herd susceptibility to the attacks (the unbocking would have to occur on a one-at-a-time case-by-case basis, e.g., it's expensive and has limited scalability).

I don't think we'll actually see this for another 5-10 years (my usual sane-suggestion take-up lead time, it seems), but It Would Certainly Be Nice To See.

Re:Don't touch my clipboard by wtmt on Tuesday February 18, 2020 @08:48AM (#22356005)
I’ve recently been experimenting with having JS off by default on Brave. Many sites that I don’t regularly use or have an account/relationship with are much more pleasant without JS. There may be some visual breakage in some cases, but I’m there for the main content, which is mostly fine.

I had experimented with NoScript long ago, but found it a bit more cumbersome at that point because (then, before the uBlock days) I couldn’t really judge which scripts were necessary and which weren’t. I’m going to try it again.

One big plus with disabling JS is that all those ad blocker blockers and other annoying popups just don’t even appear, and that adds to a better experience.

Re:Don't touch my clipboard by tjpnz on Tuesday February 18, 2020 @10:10AM (#22356567)
>disabling most of the internet

I have JS blocked on some domains because of popups/other annoyances which make getting to the content a hassle. In those cases it's the exact opposite.

Re:Don't touch my clipboard by ori_b on Tuesday February 18, 2020 @08:35AM (#22355902)
Currently, it's a relatively small and spammy portion of it.
Re:Don't touch my clipboard by nicoburns on Tuesday February 18, 2020 @04:18AM (#22354596)
> but I can't think of a single time when I want a site to react to my attempt to copy text off if it.

It's useful for things like rich-text editors (e.g. google docs), where the text is never on screen as text that you are able to copy in the first place.

Re:Don't touch my clipboard by wink on Tuesday February 18, 2020 @09:23AM (#22356234)
What about the X clipboard, e.g. just selecting text and then being able to paste with middleclick - did you check that as well? I suppose it should circumvent that?
Re:Don't touch my clipboard by franga2000 on Tuesday February 18, 2020 @12:15PM (#22357679)
I suspect a complex WYSIWYG editor like Google Docs benefits from this feature since the selection behaviour is heavily customised (and it has to be in order to support most of its features). The browser can't comprehend most of the things you might be selecting and copying from such a document, so it makes sense to replace the clipboard with a more accurate representation.
Re:Don't touch my clipboard by unnouinceput on Tuesday February 18, 2020 @06:51AM (#22355262)
Use reader mode, no JS is active there, and you can copy the code quite nicely. I always do this when I see this behaviour
Re:Don't touch my clipboard by hashkb on Monday February 17, 2020 @10:26PM (#22353198)
> I can't think of a single time when I want a site to react to my attempt to copy text off if it

Perhaps in a rich text editor I want to copy as plain text; or perhaps I want to make a Markdown editor that you can select+copy the Markdown but then paste HTML. Just brainstorming.

Re:Don't touch my clipboard by joquarky on Tuesday February 18, 2020 @06:17PM (#22361461)
If you use Greasemonkey or Tampermonkey, you can add the following to run before any other scripts:

    document.addEventListener('copy', 
        function(e){ e.stopImmediatePropagation() }
    );
Re:Don't touch my clipboard by Dylan16807 on Monday February 17, 2020 @09:42PM (#22353005)
> I can't think of a single time when I want a site to react to my attempt to copy text off if it.

Any time you're not interacting with HTML text is a time it could be useful.

Re:Don't touch my clipboard by codetrotter on Monday February 17, 2020 @09:50PM (#22353054)
Specifically, things like copying a cell of data in Google Sheets for example.
Re:Don't touch my clipboard by dredmorbius on Tuesday February 18, 2020 @03:50AM (#22354480)
Copying from HTML tables is already sane.
Re:Don't touch my clipboard by codetrotter on Tuesday February 18, 2020 @09:47PM (#22362662)
Google sheets is not just a HTML table though. And when you click on a cell, you select the cell itself, rather than having to highlight the cell contents. In order for copy paste to work as intended, this makes it necessary for Google Sheets to intercept Ctrl-C and Ctrl-V.
Re:Don't touch my clipboard by pierrec on Monday February 17, 2020 @09:53PM (#22353065)
In that case there should be no actual text selected. The problem only occurs when there is text visibly selected and you press your copy shortcut. In those cases the only correct behavior is to copy the selected text.
Re:Don't touch my clipboard by simpss on Tuesday February 18, 2020 @04:12AM (#22354570)
about:config dom.event.clipboardevents.enabled = false and you're good.