Skip to content

Commit 7c83abe

Browse files
committed
gh-160187: Reject ISO-2022 designations into the wrong G set
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.
1 parent ab5187c commit 7c83abe

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎Lib/test/test_codecencodings_iso2022.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@ 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 its own plane
30+
(b'\x1B.A\x1BNi', 'strict', '\xe9'),
31+
(b'\x1B$(C\x30\x21\x1B(B', 'strict', '\uac00'),
32+
(b'ab\x1B(Adef', 'strict', None),
33+
(b'ab\x1B(Fdef', 'strict', None),
34+
(b'ab\x1B.J\x1BNdef', 'strict', None),
35+
(b'ab\x1B.Bdef', 'strict', None),
36+
(b'ab\x1B$)Cdef', 'strict', None),
2537
)
2638

2739
class Test_ISO2022_JP3(multibytecodec_support.TestBase, unittest.TestCase):
@@ -75,6 +87,12 @@ class Test_ISO2022_KR(multibytecodec_support.TestBase, unittest.TestCase):
7587
tstring = multibytecodec_support.load_teststring('iso2022_kr')
7688
codectests = COMMON_CODEC_TESTS + (
7789
(b'ab\x1BNdef', 'replace', 'ab\x1BNdef'),
90+
# KS X 1001 can only be designated to G1
91+
(b'\x1B$)C\x0E\x30\x21\x0Fdef', 'strict', '\uac00def'),
92+
(b'ab\x1B$(Cdefg', 'strict', None),
93+
(b'ab\x1B$Cdefg', 'strict', None),
94+
(b'ab\x1B)Bdefg', 'strict', None),
95+
(b'ab\x1B$(Cdefg', 'replace', 'ab\ufffddefg'),
7896
)
7997

8098
# 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
@@ -382,17 +382,22 @@ iso2022processesc(const MultibyteCodec *codec, MultibyteCodec_State *state,
382382
return esclen;
383383
}
384384

385-
/* raise error when the charset is not designated for this encoding */
385+
/* raise error when the charset is not designated to this plane for
386+
* this encoding (ASCII is only ever designated to G0) */
386387
if (charset != CHARSET_ASCII) {
387388
const struct iso2022_designation *dsg;
388389

389390
for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++) {
390-
if (dsg->mark == charset)
391+
if (dsg->mark == charset && dsg->plane == designation) {
391392
break;
393+
}
392394
}
393395
if (!dsg->mark)
394396
return esclen;
395397
}
398+
else if (designation != 0) {
399+
return esclen;
400+
}
396401

397402
STATE_SETG(designation, charset);
398403
*inleft -= esclen;

0 commit comments

Comments
 (0)