Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,14 @@ wd_cc_library(
srcs = ["data-url.c++"],
hdrs = ["data-url.h"],
implementation_deps = [
":encoding",
"//src/workerd/util:strings",
],
visibility = ["//visibility:public"],
deps = [
"//src/workerd/jsg:url",
"//src/workerd/util:mimetype",
"@capnp-cpp//src/kj",
"@simdutf",
],
)

Expand Down
47 changes: 43 additions & 4 deletions src/workerd/api/data-url.c++
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
#include "data-url.h"

#include <workerd/api/encoding.h>
#include "simdutf.h"

#include <workerd/util/strings.h>

#include <kj/encoding.h>
#include <kj/vector.h>

namespace workerd::api {
namespace {

kj::Array<kj::byte> decodeDataUrlBase64(kj::ArrayPtr<const kj::byte> input) {
kj::Vector<char> filtered(input.size());

// DataUrl historically ignored kj::decodeBase64().hadErrors. That decoder skips bytes outside
// the base64 alphabet, including padding, and decodes the remaining characters. Preserve that
// permissive behavior before passing the input to simdutf's stricter decoder.
for (kj::byte c: input) {
if (isAlpha(c) || isDigit(c) || c == '+' || c == '/') {
filtered.add(static_cast<char>(c));
}
}

auto base64 = filtered.asPtr();
// KJ drops a final lone base64 character because it cannot produce a complete byte. simdutf
// rejects inputs whose length is 1 modulo 4, so trim that character to preserve KJ's result.
if (base64.size() % 4 == 1) {
base64 = base64.first(base64.size() - 1);
}

if (base64.size() == 0) {
return nullptr;
}

auto size = simdutf::maximal_binary_length_from_base64(base64.begin(), base64.size());
auto decoded = kj::heapArray<kj::byte>(size);
// The default loose tail handling preserves KJ's acceptance of unpadded two- and three-character
// tails and non-zero unused bits.
auto result = simdutf::base64_to_binary(
base64.begin(), base64.size(), decoded.asChars().begin(), simdutf::base64_default);
if (result.error != simdutf::SUCCESS || result.count == 0) {
return nullptr;
}
KJ_ASSERT(result.count <= size);
return decoded.first(result.count).attach(kj::mv(decoded));
}

} // namespace

kj::Maybe<DataUrl> DataUrl::tryParse(kj::StringPtr url) {
KJ_IF_SOME(url, jsg::Url::tryParse(url)) {
Expand Down Expand Up @@ -53,8 +93,7 @@ kj::Maybe<DataUrl> DataUrl::from(const jsg::Url& url) {
kj::Array<kj::byte> decoded = nullptr;
if (isBase64(unparsed)) {
unparsed = unparsed.first(KJ_ASSERT_NONNULL(unparsed.findLast(';')));
decoded =
kj::decodeBase64(stripInnerWhitespace(jsg::Url::percentDecode(data.asBytes())).asChars());
decoded = decodeDataUrlBase64(jsg::Url::percentDecode(data.asBytes()));
} else {
decoded = jsg::Url::percentDecode(data.asBytes());
}
Expand Down
12 changes: 8 additions & 4 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -928,17 +928,21 @@ void ServiceWorkerGlobalScope::fuzzilli(jsg::Lock& js, jsg::Arguments<jsg::Value
#endif

jsg::JsString ServiceWorkerGlobalScope::atob(jsg::Lock& js, kj::String data) {
auto decoded = kj::decodeBase64(data.asArray());
auto size = simdutf::maximal_binary_length_from_base64(data.begin(), data.size());
auto decoded = kj::heapArray<kj::byte>(size);
auto result = simdutf::base64_to_binary(
data.begin(), data.size(), decoded.asChars().begin(), simdutf::base64_default);

JSG_REQUIRE(!decoded.hadErrors, DOMInvalidCharacterError,
JSG_REQUIRE(result.error == simdutf::SUCCESS, DOMInvalidCharacterError,
"atob() called with invalid base64-encoded data. (Only whitespace, '+', '/', alphanumeric "
"ASCII, and up to two terminal '=' signs when the input data length is divisible by 4 are "
"allowed.)");

// Similar to btoa() taking a v8::Value, we return a v8::String directly, as this allows us to
// construct a string from the non-nul-terminated array returned from decodeBase64(). This avoids
// construct a string from the non-nul-terminated array returned from base64_to_binary(). This avoids
// making a copy purely to append a nul byte.
return js.str(decoded.asBytes());
KJ_ASSERT(result.count <= size);
return js.str(decoded.first(result.count));
}

void ServiceWorkerGlobalScope::queueMicrotask(jsg::Lock& js, jsg::Function<void()> task) {
Expand Down
Loading