From 90c9e2c65bebd9210c417e309cccad03630c60f2 Mon Sep 17 00:00:00 2001 From: Tharre Date: Sat, 8 Apr 2023 23:23:28 +0200 Subject: Add bracketed paste mode escape sequence filtering Adapted from a patch by Jann Horn send to the suckless mailing list[0]. An example attack can be found here[1]. [0] https://lists.suckless.org/hackers/1711/15672.html [1] https://thejh.net/misc/website-terminal-copy-paste --- x.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/x.c b/x.c index 58616e3..0d86a71 100644 --- a/x.c +++ b/x.c @@ -524,7 +524,7 @@ selnotify(XEvent *e) { ulong nitems, ofs, rem; int format; - uchar *data, *last, *repl; + uchar *data, *last, *repl, *readpos; Atom type, incratom, property = None; incratom = XInternAtom(xw.dpy, "INCR", 0); @@ -589,9 +589,27 @@ selnotify(XEvent *e) *repl++ = '\r'; } + /* + * In bracketed paste mode, we mark the pasted data by adding + * escape sequences around it (see below), but we also want to + * prevent the pasted data from prematurely signaling an end + * of paste. Therefore, strip escape characters from the + * pasted data. + */ + if (IS_SET(MODE_BRCKTPASTE)) { + readpos = data; + repl = data; + while (readpos < last) { + if (*readpos != '\033') + *repl++ = *readpos; + readpos++; + } + last = repl; + } + if (IS_SET(MODE_BRCKTPASTE) && ofs == 0) ttywrite("\033[200~", 6, 0); - ttywrite((char *)data, nitems * format / 8, 1); + ttywrite((char *)data, last - data, 1); if (IS_SET(MODE_BRCKTPASTE) && rem == 0) ttywrite("\033[201~", 6, 0); XFree(data); -- cgit v1.2.3-70-g09d2