Skip to content

Commit bb95c62

Browse files
gpsheadmiss-islington
authored andcommitted
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 c952392 commit bb95c62

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
@@ -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)