Skip to content

Commit 2705ddd

Browse files
committed
[backport 3.x] Prohibit char codes that would overflow the BASE_MAP
1 parent 3d43c0e commit 2705ddd

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ function base (ALPHABET) {
7878
var b256 = new Uint8Array(size)
7979
// Process the characters.
8080
while (psz < source.length) {
81+
// Find code of next character
82+
var charCode = source.charCodeAt(psz)
83+
// Base map can not be indexed using char code
84+
if (charCode > 255) { return }
8185
// Decode character
82-
var carry = BASE_MAP[source.charCodeAt(psz)]
86+
var carry = BASE_MAP[charCode]
8387
// Invalid character
8488
if (carry === 255) { return }
8589
var i = 0

test/fixtures.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@
660660
"alphabet": "0123456789fabcdef",
661661
"description": "poorly formed alphabet",
662662
"exception": "^TypeError: f is ambiguous$"
663+
},
664+
{
665+
"alphabet": "base58",
666+
"description": "character whose code exceeds the highest index of base map (>=256)",
667+
"exception": "^Error: Non-base58 character$",
668+
"string": "\u1000"
663669
}
664670
]
665671
}

ts_src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,14 @@ function base (ALPHABET: string): base.BaseConverter {
9898

9999
// Process the characters.
100100
while (psz < source.length) {
101+
// Find code of next character
102+
const charCode = source.charCodeAt(psz)
103+
104+
// Base map can not be indexed using char code
105+
if (charCode > 255) return
106+
101107
// Decode character
102-
let carry = BASE_MAP[source.charCodeAt(psz)]
108+
let carry = BASE_MAP[charCode]
103109

104110
// Invalid character
105111
if (carry === 255) return

0 commit comments

Comments
 (0)