Skip to content

Commit cf93b62

Browse files
Jake ChampionJakeChampion
authored andcommitted
feat: add event.client.tlsClientCertificate
1 parent 3d87cb2 commit cf93b62

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

runtime/js-compute-runtime/builtins/client-info.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ bool ClientInfo::tls_client_hello_get(JSContext *cx, unsigned argc, JS::Value *v
150150
return true;
151151
}
152152

153+
bool ClientInfo::tls_client_certificate_get(JSContext *cx, unsigned argc, JS::Value *vp) {
154+
METHOD_HEADER(0);
155+
156+
auto res = HttpReq::http_req_downstream_tls_raw_client_certificate();
157+
if (auto *err = res.to_err()) {
158+
HANDLE_ERROR(cx, *err);
159+
return false;
160+
}
161+
HostBytes cert = std::move(res.unwrap());
162+
163+
JS::RootedObject buffer(cx, JS::NewArrayBufferWithContents(cx, cert.len, cert.ptr.get()));
164+
if (!buffer) {
165+
// We can be here if the array buffer was too large -- if that was the case then a
166+
// JSMSG_BAD_ARRAY_LENGTH will have been created.
167+
return false;
168+
}
169+
170+
// `cert` is now owned by `buffer`
171+
static_cast<void>(cert.ptr.release());
172+
173+
args.rval().setObject(*buffer);
174+
return true;
175+
}
176+
153177
bool ClientInfo::tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp) {
154178
METHOD_HEADER(0);
155179

@@ -183,6 +207,7 @@ const JSPropertySpec ClientInfo::properties[] = {
183207
JS_PSG("geo", geo_get, JSPROP_ENUMERATE),
184208
JS_PSG("tlsCipherOpensslName", tls_cipher_openssl_name_get, JSPROP_ENUMERATE),
185209
JS_PSG("tlsProtocol", tls_protocol_get, JSPROP_ENUMERATE),
210+
JS_PSG("tlsClientCertificate", tls_client_certificate_get, JSPROP_ENUMERATE),
186211
JS_PSG("tlsClientHello", tls_client_hello_get, JSPROP_ENUMERATE),
187212
JS_PS_END,
188213
};

runtime/js-compute-runtime/builtins/client-info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ClientInfo final : public BuiltinNoConstructor<ClientInfo> {
1111
static bool tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::Value *vp);
1212
static bool tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp);
1313
static bool tls_client_hello_get(JSContext *cx, unsigned argc, JS::Value *vp);
14+
static bool tls_client_certificate_get(JSContext *cx, unsigned argc, JS::Value *vp);
1415

1516
public:
1617
static constexpr const char *class_name = "FetchEvent";

runtime/js-compute-runtime/fastly-world/fastly_world_adapter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ bool fastly_http_req_downstream_tls_protocol(fastly_world_string_t *ret, fastly_
200200
fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret->ptr), 32, &ret->len), err);
201201
}
202202

203+
bool fastly_http_req_downstream_tls_raw_client_certificate(fastly_world_list_u8_t *ret,
204+
fastly_error_t *err) {
205+
auto default_size = 4096;
206+
ret->ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));
207+
auto status = fastly::req_downstream_tls_raw_client_certificate(reinterpret_cast<char *>(ret->ptr), default_size, &ret->len);
208+
if (status == FASTLY_ERROR_BUFFER_LEN) {
209+
cabi_realloc(ret->ptr, default_size, 4, ret->len);
210+
status = fastly::req_downstream_tls_raw_client_certificate(reinterpret_cast<char *>(ret->ptr), ret->len, &ret->len);
211+
}
212+
return convert_result(status, err);
213+
}
203214
bool fastly_http_req_downstream_tls_client_hello(fastly_world_list_u8_t *ret, fastly_error_t *err) {
204215
auto default_size = 512;
205216
ret->ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));

runtime/js-compute-runtime/host_interface/fastly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ int req_downstream_tls_protocol(char *ret, size_t ret_len, size_t *nwritten);
158158
WASM_IMPORT("fastly_http_req", "downstream_tls_client_hello")
159159
int req_downstream_tls_client_hello(char *ret, size_t ret_len, size_t *nwritten);
160160

161+
WASM_IMPORT("fastly_http_req", "downstream_tls_raw_client_certificate")
162+
int req_downstream_tls_raw_client_certificate(char *ret, size_t ret_len, size_t *nwritten);
161163
WASM_IMPORT("fastly_http_req", "new")
162164
int req_new(fastly_request_handle_t *req_handle_out);
163165

runtime/js-compute-runtime/host_interface/host_api.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,21 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_client_hello() {
546546

547547
return res;
548548
}
549+
550+
// http-req-downstream-tls-raw-client-certificate: func() -> result<list<u8>, error>
551+
Result<HostBytes> HttpReq::http_req_downstream_tls_raw_client_certificate() {
552+
Result<HostBytes> res;
553+
554+
fastly_world_list_u8_t ret;
555+
fastly_error_t err;
556+
if (!fastly_http_req_downstream_tls_raw_client_certificate(&ret, &err)) {
557+
res.emplace_err(err);
558+
} else {
559+
res.emplace(ret);
560+
}
561+
562+
return res;
563+
}
549564
bool HttpReq::is_valid() const { return this->handle != HttpReq::invalid; }
550565

551566
Result<fastly_http_version_t> HttpReq::get_version() const {

runtime/js-compute-runtime/host_interface/host_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class HttpReq final : public HttpBase {
261261

262262
static Result<HostBytes> http_req_downstream_tls_client_hello();
263263

264+
static Result<HostBytes> http_req_downstream_tls_raw_client_certificate();
265+
264266
/// Send this request synchronously, and wait for the response.
265267
Result<Response> send(HttpBody body, std::string_view backend);
266268

0 commit comments

Comments
 (0)