Skip to content

Commit 9842796

Browse files
[3.15] gh-160187: Reject ISO-2022 designations into the wrong G set (GH-160188) (#161064)
gh-160187: Reject ISO-2022 designations into the wrong G set (GH-160188) The ISO-2022 decoders checked only that a designated charset belonged to the encoding, not that it went to the code element the encoding assigns it to. ANT-2026-TH3IZKXQ (cherry picked from commit 5f28e31) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
1 parent 89fe8f2 commit 9842796

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎Lib/test/test_codecencodings_iso2022.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ class Test_ISO2022_JP(multibytecodec_support.TestBase, unittest.TestCase):
1515
tstring = multibytecodec_support.load_teststring('iso2022_jp')
1616
codectests = COMMON_CODEC_TESTS + (
1717
(b'ab\x1BNdef', 'replace', 'ab\x1BNdef'),
18+
# there is no G1 in this encoding
19+
(b'ab\x1B$)Bdef', 'strict', None),
20+
(b'ab\x1B)Jdef', 'strict', None),
21+
(b'ab\x1B)Bdef', 'strict', None),
1822
)
1923

2024
class Test_ISO2022_JP2(multibytecodec_support.TestBase, unittest.TestCase):
2125
encoding = 'iso2022_jp_2'
2226
tstring = multibytecodec_support.load_teststring('iso2022_jp')
2327
codectests = COMMON_CODEC_TESTS + (
2428
(b'ab\x1BNdef', 'replace', 'abdef'),
29+
# a charset can only be designated to the G set (G0, G1 or G2)
30+
# that RFC 1554 assigns it
31+
(b'\x1B.A\x1BNi', 'strict', '\xe9'),
32+
(b'\x1B$(C\x30\x21\x1B(B', 'strict', '\uac00'),
33+
(b'ab\x1B(Adef', 'strict', None),
34+
(b'ab\x1B(Fdef', 'strict', None),
35+
(b'ab\x1B.J\x1BNdef', 'strict', None),
36+
(b'ab\x1B.Bdef', 'strict', None),
37+
(b'ab\x1B$)Cdef', 'strict', None),
2538
)
2639

2740
class Test_ISO2022_JP3(multibytecodec_support.TestBase, unittest.TestCase):
@@ -75,6 +88,12 @@ class Test_ISO2022_KR(multibytecodec_support.TestBase, unittest.TestCase):
7588
tstring = multibytecodec_support.load_teststring('iso2022_kr')
7689
codectests = COMMON_CODEC_TESTS + (
7790
(b'ab\x1BNdef', 'replace', 'ab\x1BNdef'),
91+
# KS X 1001 can only be designated to G1
92+
(b'\x1B$)C\x0E\x30\x21\x0Fdef', 'strict', '\uac00def'),
93+
(b'ab\x1B$(Cdefg', 'strict', None),
94+
(b'ab\x1B$Cdefg', 'strict', None),
95+
(b'ab\x1B)Bdefg', 'strict', None),
96+
(b'ab\x1B$(Cdefg', 'replace', 'ab\ufffddefg'),
7897
)
7998

8099
# iso2022_kr.txt cannot be used to test "chunk coding": the escape
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix the ISO-2022 decoders (``iso2022_jp``, ``iso2022_kr`` and related)
2+
accepting escape sequences that designate a character set to the wrong code
3+
element (G0, G1 or G2). Such sequences now raise :exc:`UnicodeDecodeError`
4+
instead of producing wrong text or :exc:`RuntimeError`.

‎Modules/cjkcodecs/_codecs_iso2022.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,22 @@ iso2022processesc(const MultibyteCodec *codec, MultibyteCodec_State *state,
379379
return esclen;
380380
}
381381

382-
/* raise error when the charset is not designated for this encoding */
382+
/* raise error when the charset is not designated to this plane for
383+
* this encoding (ASCII is only ever designated to G0) */
383384
if (charset != CHARSET_ASCII) {
384385
const struct iso2022_designation *dsg;
385386

386387
for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++) {
387-
if (dsg->mark == charset)
388+
if (dsg->mark == charset && dsg->plane == designation) {
388389
break;
390+
}
389391
}
390392
if (!dsg->mark)
391393
return esclen;
392394
}
395+
else if (designation != 0) {
396+
return esclen;
397+
}
393398

394399
STATE_SETG(designation, charset);
395400
*inleft -= esclen;

0 commit comments

Comments
 (0)