Skip to content

Commit e848733

Browse files
steveluscherfanatid
authored andcommitted
Prohibit char codes that would overflow the BASE_MAP
1 parent dd4c308 commit e848733

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

.github/workflows/main_ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master
7+
- v4.x
78
pull_request:
89

910
jobs:
@@ -36,4 +37,4 @@ jobs:
3637
node-version: 12
3738
registry-url: https://registry.npmjs.org/
3839
- run: npm i
39-
- run: npm run gitdiff
40+
- run: npm run gitdiff

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ function base (ALPHABET) {
8181
var b256 = new Uint8Array(size)
8282
// Process the characters.
8383
while (source[psz]) {
84+
// Find code of next character
85+
var charCode = source.charCodeAt(psz)
86+
// Base map can not be indexed using char code
87+
if (charCode > 255) { return }
8488
// Decode character
85-
var carry = BASE_MAP[source.charCodeAt(psz)]
89+
var carry = BASE_MAP[charCode]
8690
// Invalid character
8791
if (carry === 255) { return }
8892
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
@@ -100,8 +100,14 @@ function base (ALPHABET: string): base.BaseConverter {
100100

101101
// Process the characters.
102102
while (source[psz]) {
103+
// Find code of next character
104+
const charCode = source.charCodeAt(psz)
105+
106+
// Base map can not be indexed using char code
107+
if (charCode > 255) return
108+
103109
// Decode character
104-
let carry = BASE_MAP[source.charCodeAt(psz)]
110+
let carry = BASE_MAP[charCode]
105111

106112
// Invalid character
107113
if (carry === 255) return

0 commit comments

Comments
 (0)