You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BPE build() panics on load for a crafted tokenizer.json (merge concatenation exceeds the scratch buffer) #2198
Summary. A crafted tokenizer.json with a BPE model causes a panic during Tokenizer::from_file / from_str, before any tokenization runs. The BPE builder writes a merge concatenation into a scratch buffer sized
for the longest single vocab token; a merge whose two tokens concatenate to something longer than any single vocab
entry overflows the slice and panics.
Details. In tokenizers/src/models/bpe/model.rs, BpeBuilder::build:
Both a and b are individually in the vocab, so each is <= max_len, but the write spans [a.len() .. a.len()+b_len], up to ~2*max_len, which exceeds the buffer whenever the merged token is longer than the longest
single vocab entry. The write happens before the "is the concatenation in the vocab" check. Two ways to trip it:
a slice-index-out-of-bounds panic at the write (fires in debug and release, since Rust always bounds-checks slice
ranges), and a usize underflow at b.len() - prefix_len when continuing_subword_prefix is longer than b.
Reproduction (tokenizers Python binding):
importtokenizers, jsoncrafted= {"version":"1.0","model":{"type":"BPE","vocab":{"aa":0,"bb":1},"merges":[["aa","bb"]]}}
tokenizers.Tokenizer.from_str(json.dumps(crafted))
# -> PanicException: range end index 4 out of range for slice of length 2 (at models/bpe/model.rs:267)
(A well-formed tokenizer never trips this because the merged token "aabb" is itself a vocab entry, so max_len
covers it; the crash requires a merge whose result is absent from / larger than the vocab.)
Impact. Loading an untrusted tokenizer.json (e.g. from a model repository) crashes the tokenizer load before
inference — a denial of service. Native (Rust) consumers abort the process; the Python binding raises a catchable PanicException.
Suggested fix. Validate the pair length before the write (size the buffer to 2*max_len, or check merge_len <= buffer.len() and return a build error), and guard b.len() - prefix_len against prefix_len > b.len(); ideally
move the write after the vocab-membership check so a malformed merge returns a TokenizerBuilderError instead of
panicking.
Sorry, I don't see any value in this issue. Why would I want to load an untrusted tokenizer.json?
Did not see this reply. Because security is important and it's very easy to utilize untrusted artifacts for a normal user. It's not a matter of someone wanting to, it's a matter of accident, malicious intent, etc. I would take these security concerns much more seriously.
Activity
github-actions commentedon Sep 20, 2026
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
ArthurZucker commentedon Sep 20, 2026
Sorry, I don't see any value in this issue. Why would I want to load an untrusted tokenizer.json?
professor-moody commentedon Sep 20, 2026
Did not see this reply. Because security is important and it's very easy to utilize untrusted artifacts for a normal user. It's not a matter of someone wanting to, it's a matter of accident, malicious intent, etc. I would take these security concerns much more seriously.