Skip to content

Commit 12435f9

Browse files
committed
test permissions
1 parent d2c9f66 commit 12435f9

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

ext/node/polyfills/internal_binding/cares_wrap.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ export function getaddrinfo(
7878
// REF: https://nodejs.org/api/dns.html#dns_supported_getaddrinfo_flags
7979

8080
(async () => {
81+
let error = 0;
8182
try {
8283
addresses.push(...await op_getaddrinfo(hostname, req.port || undefined));
83-
} catch {
84-
// pass
84+
if (addresses.length === 0) {
85+
error = codeMap.get("EAI_NODATA")!;
86+
}
87+
} catch (e) {
88+
if (e instanceof Deno.errors.NotCapable) {
89+
error = codeMap.get("EPERM")!;
90+
} else {
91+
error = codeMap.get("EAI_NODATA")!;
92+
}
8593
}
8694

87-
const error = addresses.length ? 0 : codeMap.get("EAI_NODATA")!;
88-
8995
// TODO(cmorten): needs work
9096
// REF: https://github.com/nodejs/node/blob/master/src/cares_wrap.cc#L1444
9197
if (!verbatim) {

tests/unit_node/http_test.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import net, { Socket } from "node:net";
1515
import fs from "node:fs";
1616
import { text } from "node:stream/consumers";
1717

18-
import { assert, assertEquals, fail } from "@std/assert";
18+
import { assert, assertEquals, assertStringIncludes, fail } from "@std/assert";
1919
import { assertSpyCalls, spy } from "@std/testing/mock";
2020
import { fromFileUrl, relative } from "@std/path";
2121
import { retry } from "@std/async/retry";
@@ -1916,3 +1916,31 @@ Deno.test("[node/http] supports proxy http request", async () => {
19161916
await promise;
19171917
await server.finished;
19181918
});
1919+
1920+
Deno.test("[node/http] `request` requires net permission to host and port", {
1921+
permissions: { net: ["localhost:4545"] },
1922+
}, async () => {
1923+
const { promise, resolve } = Promise.withResolvers<void>();
1924+
http.request("http://localhost:4545/echo.ts", async (res) => {
1925+
assertEquals(res.statusCode, 200);
1926+
assertStringIncludes(await text(res), "function echo(");
1927+
resolve();
1928+
}).end();
1929+
await promise;
1930+
});
1931+
1932+
Deno.test(
1933+
"[node/http] `request` errors with EPERM error when permission is not granted",
1934+
{ permissions: { net: ["localhost:4321"] } }, // wrong permission
1935+
async () => {
1936+
const { promise, resolve } = Promise.withResolvers<void>();
1937+
http.request("http://localhost:4545/echo.ts", async (res) => {})
1938+
.on("error", (e) => {
1939+
assertEquals(e.message, "getaddrinfo EPERM localhost");
1940+
// deno-lint-ignore no-explicit-any
1941+
assertEquals((e as any).code, "EPERM");
1942+
resolve();
1943+
}).end();
1944+
await promise;
1945+
},
1946+
);

0 commit comments

Comments
 (0)