Skip to content

Commit 84fb6a2

Browse files
author
Jake Champion
committed
fix: When using implicit backends with https protocol, use the hostname for the sni hostname value to match fetch behaviour in browsers and other runtimes
1 parent f51e875 commit 84fb6a2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

runtime/js-compute-runtime/builtins/backend.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,20 @@ bool Backend::set_host_override(JSContext *cx, JSObject *backend,
883883
return true;
884884
}
885885

886+
bool Backend::set_sni_hostname(JSContext *cx, JSObject *backend, JS::HandleValue sniHostname_val) {
887+
auto sniHostname = JS::ToString(cx, sniHostname_val);
888+
if (!sniHostname) {
889+
return false;
890+
}
891+
892+
if (JS_GetStringLength(sniHostname) == 0) {
893+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BACKEND_SNI_HOSTNAME_EMPTY);
894+
return false;
895+
}
896+
JS::SetReservedSlot(backend, Backend::Slots::SniHostname, JS::StringValue(sniHostname));
897+
return true;
898+
}
899+
886900
/// Timeouts for backends must be less than 2^32 milliseconds, or
887901
/// about a month and a half.
888902
bool Backend::set_timeout_slot(JSContext *cx, JSObject *backend, JS::HandleValue value,
@@ -1001,6 +1015,11 @@ JSObject *Backend::create(JSContext *cx, JS::HandleObject request) {
10011015

10021016
auto use_ssl = origin.rfind("https://", 0) == 0;
10031017
JS::SetReservedSlot(backend, Backend::Slots::UseSsl, JS::BooleanValue(use_ssl));
1018+
if (use_ssl) {
1019+
if (!Backend::set_sni_hostname(cx, backend, name)) {
1020+
return nullptr;
1021+
}
1022+
}
10041023

10051024
auto result = Backend::register_dynamic_backend(cx, backend);
10061025
if (result.isErr()) {

runtime/js-compute-runtime/builtins/backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Backend : public BuiltinImpl<Backend> {
4444
static bool set_timeout_slot(JSContext *cx, JSObject *backend, JS::HandleValue value,
4545
Backend::Slots slot, std::string property_name);
4646
static bool set_host_override(JSContext *cx, JSObject *backend, JS::HandleValue hostOverride_val);
47+
static bool set_sni_hostname(JSContext *cx, JSObject *backend, JS::HandleValue sniHostname_val);
4748
static bool set_name(JSContext *cx, JSObject *backend, JS::HandleValue name_val);
4849
static bool toString(JSContext *cx, unsigned argc, JS::Value *vp);
4950
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);

0 commit comments

Comments
 (0)