From 9baad9bf5b8bc41a0b9ef1d1dd9ae59ae38e9b96 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 10 Sep 2024 10:21:10 +0900 Subject: [PATCH 1/3] fix(ext/node): show original Deno error instead of UNKNOWN error --- ext/node/polyfills/internal_binding/stream_wrap.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/node/polyfills/internal_binding/stream_wrap.ts b/ext/node/polyfills/internal_binding/stream_wrap.ts index dc30bfdfe0a4ef..8a54722119b7bd 100644 --- a/ext/node/polyfills/internal_binding/stream_wrap.ts +++ b/ext/node/polyfills/internal_binding/stream_wrap.ts @@ -38,6 +38,7 @@ import { TextEncoder } from "ext:deno_web/08_text_encoding.js"; import { Buffer } from "node:buffer"; import { notImplemented } from "ext:deno_node/_utils.ts"; import { HandleWrap } from "ext:deno_node/internal_binding/handle_wrap.ts"; +import { ownerSymbol } from "ext:deno_node/internal/async_hooks.ts"; import { AsyncWrap, providerType, @@ -343,7 +344,8 @@ export class LibuvStreamWrap extends HandleWrap { ) { nread = codeMap.get("ECONNRESET")!; } else { - nread = codeMap.get("UNKNOWN")!; + this[ownerSymbol].destroy(e); + return } } From 5afc6fabe47619db9fcc8afddd264b79d421aabe Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 10 Sep 2024 13:33:26 +0900 Subject: [PATCH 2/3] add tests --- tests/unit/test_util.ts | 2 +- tests/unit_node/tls_test.ts | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_util.ts b/tests/unit/test_util.ts index 3f41b90a285029..45859bfaa7bd2b 100644 --- a/tests/unit/test_util.ts +++ b/tests/unit/test_util.ts @@ -34,7 +34,7 @@ export function pathToAbsoluteFileUrl(path: string): URL { return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`); } -export function execCode(code: string): Promise { +export function execCode(code: string): Promise { return execCode2(code).finished(); } diff --git a/tests/unit_node/tls_test.ts b/tests/unit_node/tls_test.ts index 6033efa31268b9..c15ae847537699 100644 --- a/tests/unit_node/tls_test.ts +++ b/tests/unit_node/tls_test.ts @@ -1,11 +1,12 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { assertEquals, assertInstanceOf } from "@std/assert"; +import { assertEquals, assertStringIncludes, assertInstanceOf } from "@std/assert"; import { delay } from "@std/async/delay"; import { fromFileUrl, join } from "@std/path"; import * as tls from "node:tls"; import * as net from "node:net"; import * as stream from "node:stream"; +import { execCode } from "../unit/test_util.ts"; const tlsTestdataDir = fromFileUrl( new URL("../testdata/tls", import.meta.url), @@ -191,3 +192,21 @@ Deno.test("tlssocket._handle._parentWrap is set", () => { ._parentWrap; assertInstanceOf(parentWrap, stream.PassThrough); }); + +Deno.test("tls.connect() throws InvalidData when there's error in certificate", async () => { + // Uses execCode to avoid `--unsafely-ignore-certificate-errors` option applied + const [status, output] = await execCode(` + import tls from "node:tls"; + const conn = tls.connect({ + host: "localhost", + port: 4557, + }); + + conn.on("error", (err) => { + console.log(err); + }); + `); + + assertEquals(status, 0); + assertStringIncludes(output, "InvalidData: invalid peer certificate: UnknownIssuer"); +}); From 9651bf40a47759c8d8d9cec8ae362c16a8ccc57d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 10 Sep 2024 13:54:37 +0900 Subject: [PATCH 3/3] fmt --- ext/node/polyfills/internal_binding/stream_wrap.ts | 2 +- tests/unit/test_util.ts | 2 +- tests/unit_node/tls_test.ts | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ext/node/polyfills/internal_binding/stream_wrap.ts b/ext/node/polyfills/internal_binding/stream_wrap.ts index 8a54722119b7bd..7aea83d6f5899c 100644 --- a/ext/node/polyfills/internal_binding/stream_wrap.ts +++ b/ext/node/polyfills/internal_binding/stream_wrap.ts @@ -345,7 +345,7 @@ export class LibuvStreamWrap extends HandleWrap { nread = codeMap.get("ECONNRESET")!; } else { this[ownerSymbol].destroy(e); - return + return; } } diff --git a/tests/unit/test_util.ts b/tests/unit/test_util.ts index 45859bfaa7bd2b..3f41b90a285029 100644 --- a/tests/unit/test_util.ts +++ b/tests/unit/test_util.ts @@ -34,7 +34,7 @@ export function pathToAbsoluteFileUrl(path: string): URL { return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`); } -export function execCode(code: string): Promise { +export function execCode(code: string): Promise { return execCode2(code).finished(); } diff --git a/tests/unit_node/tls_test.ts b/tests/unit_node/tls_test.ts index c15ae847537699..f7660da1e5a296 100644 --- a/tests/unit_node/tls_test.ts +++ b/tests/unit_node/tls_test.ts @@ -1,6 +1,10 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { assertEquals, assertStringIncludes, assertInstanceOf } from "@std/assert"; +import { + assertEquals, + assertInstanceOf, + assertStringIncludes, +} from "@std/assert"; import { delay } from "@std/async/delay"; import { fromFileUrl, join } from "@std/path"; import * as tls from "node:tls"; @@ -208,5 +212,8 @@ Deno.test("tls.connect() throws InvalidData when there's error in certificate", `); assertEquals(status, 0); - assertStringIncludes(output, "InvalidData: invalid peer certificate: UnknownIssuer"); + assertStringIncludes( + output, + "InvalidData: invalid peer certificate: UnknownIssuer", + ); });