fix: sanitize sixel previews (#2542)
* fix sixel filter * improve sixel filter * simplify sixel byte filter * cleanup
This commit is contained in:
1 parent
7bbdfebded
commit
0cf8d597db
2 files changed
+14
-6
No files matched your search
@@ -526,12 +526,15 @@ func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool,
|
|||||||
currState = stateNormal
|
currState = stateNormal
|
||||||
}
|
}
|
||||||
case stateSixel:
|
case stateSixel:
|
||||||
// Accept printable bytes (0x20-0x7E) and ESC inside the
|
// Inside the DCS frame, accept only printable bytes. Tolerate
|
||||||
// DCS frame. Reject everything else (C0, DEL, 0x80+).
|
// '\r'/'\n' (some encoders insert them for readability) by
|
||||||
|
// silently dropping them. Anything else aborts the frame so
|
||||||
|
// that an attacker cannot smuggle CSI/OSC/DCS through it.
|
||||||
switch {
|
switch {
|
||||||
case b == '\033':
|
case b == '\033':
|
||||||
buf.WriteByte(b)
|
buf.WriteByte(b)
|
||||||
currState = stateSixelEsc
|
currState = stateSixelEsc
|
||||||
|
case b == '\r' || b == '\n':
|
||||||
case b >= 0x20 && b <= 0x7E:
|
case b >= 0x20 && b <= 0x7E:
|
||||||
buf.WriteByte(b)
|
buf.WriteByte(b)
|
||||||
default:
|
default:
|
||||||
@@ -539,13 +542,14 @@ func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool,
|
|||||||
currState = stateNormal
|
currState = stateNormal
|
||||||
}
|
}
|
||||||
case stateSixelEsc:
|
case stateSixelEsc:
|
||||||
buf.WriteByte(b)
|
|
||||||
if b == '\\' {
|
if b == '\\' {
|
||||||
|
buf.WriteByte(b)
|
||||||
flush(true)
|
flush(true)
|
||||||
sixel = true
|
sixel = true
|
||||||
currState = stateNormal
|
currState = stateNormal
|
||||||
} else {
|
} else {
|
||||||
currState = stateSixel
|
buf.Reset()
|
||||||
|
currState = stateNormal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-2
@@ -530,8 +530,8 @@ func TestReadLines(t *testing.T) {
|
|||||||
{"\033P\033\\", 10, []string{"\033P\033\\"}, false, true},
|
{"\033P\033\\", 10, []string{"\033P\033\\"}, false, true},
|
||||||
{"\033Pq\"1;1;1;1#0@\033\\", 10, []string{"\033Pq\"1;1;1;1#0@\033\\"}, false, true},
|
{"\033Pq\"1;1;1;1#0@\033\\", 10, []string{"\033Pq\"1;1;1;1#0@\033\\"}, false, true},
|
||||||
{"\033P\000\033\\", 10, []string{"\033\\"}, false, false},
|
{"\033P\000\033\\", 10, []string{"\033\\"}, false, false},
|
||||||
{"\033P\n\033\\", 10, []string{"\033\\"}, false, false},
|
{"\033P\n\033\\", 10, []string{"\033P\033\\"}, false, true},
|
||||||
{"\033P\r\n\033\\", 10, []string{"", "\033\\"}, false, false},
|
{"\033P\r\n\033\\", 10, []string{"\033P\033\\"}, false, true},
|
||||||
{"\033P\033\\\033P\033\\", 10, []string{"\033P\033\\", "\033P\033\\"}, false, true},
|
{"\033P\033\\\033P\033\\", 10, []string{"\033P\033\\", "\033P\033\\"}, false, true},
|
||||||
{"foo\033P\033\\bar", 10, []string{"foo", "\033P\033\\", "bar"}, false, true},
|
{"foo\033P\033\\bar", 10, []string{"foo", "\033P\033\\", "bar"}, false, true},
|
||||||
{"foo\033P\033\\bar\033P\033\\baz", 10, []string{"foo", "\033P\033\\", "bar", "\033P\033\\", "baz"}, false, true},
|
{"foo\033P\033\\bar\033P\033\\baz", 10, []string{"foo", "\033P\033\\", "bar", "\033P\033\\", "baz"}, false, true},
|
||||||
@@ -541,6 +541,10 @@ func TestReadLines(t *testing.T) {
|
|||||||
{"foo\nbar\nbaz", 3, []string{"foo", "bar", "baz"}, false, false},
|
{"foo\nbar\nbaz", 3, []string{"foo", "bar", "baz"}, false, false},
|
||||||
{"foo\nbar\nbaz\n", 3, []string{"foo", "bar", "baz"}, false, false},
|
{"foo\nbar\nbaz\n", 3, []string{"foo", "bar", "baz"}, false, false},
|
||||||
{"foo\nbar\033P\033\\", 3, []string{"foo", "bar", "\033P\033\\"}, false, true},
|
{"foo\nbar\033P\033\\", 3, []string{"foo", "bar", "\033P\033\\"}, false, true},
|
||||||
|
// Inside the DCS body, ESC must be followed by '\\' (ST) for the
|
||||||
|
// frame to be accepted. Any other byte aborts, so an attacker
|
||||||
|
// cannot embed CSI/OSC/nested-DCS through the sixel path.
|
||||||
|
{"\033P\033]52;c;x\033\\", 10, []string{"52;c;x\033\\"}, false, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
Reference in new issue
Block a user