Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/base64/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,6 @@ pub fn encode_url_safe(dest: &mut [u8], source: &[u8]) -> usize {
simdutf::base64::encode(source, dest, true)
}

/// `encode_url_safe` into a freshly-allocated `Vec<u8>` sized exactly via
/// `simdutf_encode_len_url_safe` (simdutf computes the exact no-padding length, so
/// the trailing `truncate` is a no-op kept for symmetry with `encode_alloc`).
pub fn simdutf_encode_url_safe_alloc(source: &[u8]) -> Vec<u8> {
let len = simdutf_encode_len_url_safe(source.len());
let mut destination = vec![0u8; len];
let encoded_len = encode_url_safe(&mut destination, source);
destination.truncate(encoded_len);
destination
}

pub fn decode_len_upper_bound(len: usize) -> usize {
match zig_base64::STANDARD.decoder.calc_size_upper_bound(len) {
Ok(v) => v,
Expand Down
15 changes: 10 additions & 5 deletions src/runtime/node/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::jsc::{self, CallFrame, JSGlobalObject, JSValue, JsResult};
use bun_core::zig_string::Slice as ZigStringSlice;
use bun_core::{self, fmt as bun_fmt};
use bun_core::{WStr, ZStr, ZigString};
use bun_jsc::{SliceWithUnderlyingStringJsc as _, StringJsc as _, ZigStringJsc as _};
use bun_jsc::{SliceWithUnderlyingStringJsc as _, StringJsc as _};
use bun_paths::{MAX_PATH_BYTES, OSPathBuffer, OSPathSliceZ, PathBuffer, WPathBuffer};
use bun_sys::{self, Fd, Mode, O};

Expand Down Expand Up @@ -808,8 +808,6 @@ impl Encoding {
input.len(),
max_size,
);
// Stable Rust forbids const-generic arithmetic in array lengths, so
// we heap-allocate.
match self {
Self::Base64 => {
let encoded_len = bun_core::base64::encode_len(input);
Expand All @@ -823,8 +821,15 @@ impl Encoding {
encoded.transfer_to_js(global_object)
}
Self::Base64url => {
let buf = bun_base64::simdutf_encode_url_safe_alloc(input);
Ok(jsc::zig_string::ZigString::init(&buf).to_js(global_object))
let encoded_len = bun_base64::url_safe_encode_len(input);
let (mut encoded, bytes) =
bun_core::String::create_uninitialized_latin1(encoded_len);
if encoded.is_dead() {
return encoded.transfer_to_js(global_object);
}
let n = bun_base64::encode_url_safe(bytes, input);
debug_assert_eq!(n, encoded_len);
encoded.transfer_to_js(global_object)
}
Self::Hex => {
// The byte-by-byte `write!` formatting machinery is pathologically
Expand Down
22 changes: 22 additions & 0 deletions test/js/bun/util/base64-url-safe-encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@
base64UrlReference(new Uint8Array(raw)),
);
});

// Encoding::encode_with_max_size sizes the destination WTF string via
// url_safe_encode_len before encoding in place; cover every mod-3 digest
// length the crypto callers actually produce so a mis-sized buffer (trailing
// garbage or truncation) would show up here.
const digestCases = [
["md5", 16],
["sha1", 20],
["sha224", 28],
["sha256", 32],
["sha384", 48],
["sha512", 64],
["sha512-224", 28],
["sha512-256", 32],
] as const;
test.each(digestCases)("Hash.digest('base64url') is exact for %s (%d bytes)", (algo, size) => {
const raw = createHash(algo).update("bun").digest();
expect(raw.length).toBe(size);
const enc = createHash(algo).update("bun").digest("base64url");
expect(enc).toBe(base64UrlReference(raw));
expect(enc.length).toBe(Math.ceil((size * 4) / 3));
});

Check warning on line 107 in test/js/bun/util/base64-url-safe-encode.test.ts

View check run for this annotation

Claude / Claude Code Review

New test does not exercise the changed Rust code path

This test's comment says it covers `Encoding::encode_with_max_size` so a mis-sized buffer would show up here, but `createHash` from `node:crypto` is implemented in C++ (`JSHash.cpp` → `StringBytes::encode`) and never reaches the Rust `encode_with_max_size` arm this PR modifies — only `Bun.CryptoHasher`, `Bun.CSRF.generate`, and `Bun.randomUUIDv7` go through it. Switch to `new Bun.CryptoHasher(algo)` (or add it alongside) so the test actually exercises the in-place encode it claims to cover; the
Comment thread
robobun marked this conversation as resolved.
Resolved
});
Loading