Skip to content

Commit

Permalink
fix(ssl) fix ssl shutdown (oven-sh#12492)
Browse files Browse the repository at this point in the history
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
  • Loading branch information
cirospaciari and Jarred-Sumner authored Jul 11, 2024
1 parent 5f7b96b commit 4c87406
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/bun-usockets/src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1740,15 +1740,20 @@ void us_internal_ssl_socket_shutdown(struct us_internal_ssl_socket_t *s) {
loop_ssl_data->ssl_socket = &s->s;

loop_ssl_data->msg_more = 0;

// sets SSL_SENT_SHUTDOWN no matter what (not actually true if error!)
int ret = SSL_shutdown(s->ssl);
if (ret == 0) {
ret = SSL_shutdown(s->ssl);
}

if (ret < 0) {
if (SSL_in_init(s->ssl) || SSL_get_quiet_shutdown(s->ssl)) {
// when SSL_in_init or quiet shutdown in BoringSSL, we call shutdown
// directly
us_socket_shutdown(0, &s->s);
return;
}

if (ret < 0) {
int err = SSL_get_error(s->ssl, ret);
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
// clear
Expand Down
1 change: 0 additions & 1 deletion src/deps/boringssl.translated.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19162,7 +19162,6 @@ pub const SSL_CTX = opaque {
if (auto_crypto_buffer_pool == null) auto_crypto_buffer_pool = CRYPTO_BUFFER_POOL_new();
SSL_CTX_set0_buffer_pool(ctx, auto_crypto_buffer_pool);
_ = SSL_CTX_set_cipher_list(ctx, SSL_DEFAULT_CIPHER_LIST);
SSL_CTX_set_quiet_shutdown(ctx, 1);
}

pub inline fn setCustomVerify(this: *SSL_CTX, cb: ?VerifyCallback) void {
Expand Down
33 changes: 33 additions & 0 deletions test/js/web/fetch/fetch.tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,36 @@ it("fetch should respect rejectUnauthorized env", async () => {
expect(exitCode2).toBe(1);
});
});

it("fetch timeout works on tls", async () => {
using server = Bun.serve({
tls: cert1,
hostname: "localhost",
port: 0,
rejectUnauthorized: false,
async fetch() {
async function* body() {
yield "Hello, ";
await Bun.sleep(500); // should only take 200ms
yield "World!";
}
return new Response(body);
},
});
const start = performance.now();
const TIMEOUT = 200;
const THRESHOLD = 100;

try {
await fetch(server.url, {
signal: AbortSignal.timeout(TIMEOUT),
tls: { ca: cert1.cert },
}).then(res => res.text());
} catch (e) {
expect(e.name).toBe("TimeoutError");
} finally {
const total = performance.now() - start;
expect(total).toBeGreaterThan(TIMEOUT - THRESHOLD);
expect(total).toBeLessThan(TIMEOUT + THRESHOLD);
}
});

0 comments on commit 4c87406

Please sign in to comment.