Search code examples
pythonpython-3.xgnomegnome-terminalxclip

How to clear both clipboards securely in Gnome, from Python?


Gnome desktop has 2 clipboards, the X.org (saves every selection) and the legacy one (CTRL+C). I am writing a simple python script to clear both clipboards, securely preferably, since it may be done after copy-pasting a password.

The code that I have seen over here is this:

# empty X.org clipboard
os.system("xclip -i /dev/null")  

# empty GNOME clipboard
os.system("touch blank")  
os.system("xclip -selection clipboard blank")  

Unfortunately this code creates a file named blank for some reason, so we have to remove it:

os.remove("blank")

However the main problem is that by calling both of these scripts, it leaves the xclip process open, even after I close the terminal.

So we have 2 problems with this option:

1) It creates a blank file, which seems like a flawed method to me

2) It leaves a process open, which could be a security hole.

I also know about this method:

 os.system("echo "" | xclip -selection clipboard")  # empty clipboard

However this one leaves a \n newline character in the clipboard, so I would not call this method effective either.

So how to do it properly then?


Solution

  • I have figured out:

    #CLIPBOARD cleaner
    subprocess.run(["xsel","-bc"])
    
    #PRIMARY cleaner
    subprocess.run(["xsel","-c"])
    

    This one cleans both buffers, and leaves no zombie processes at all. Thanks for everyone who suggested some of them.