Skip to content

Commit

Permalink
test permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Feb 7, 2025
1 parent d2c9f66 commit 12435f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 10 additions & 4 deletions ext/node/polyfills/internal_binding/cares_wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@ export function getaddrinfo(
// REF: https://nodejs.org/api/dns.html#dns_supported_getaddrinfo_flags

(async () => {
let error = 0;
try {
addresses.push(...await op_getaddrinfo(hostname, req.port || undefined));
} catch {
// pass
if (addresses.length === 0) {
error = codeMap.get("EAI_NODATA")!;
}
} catch (e) {
if (e instanceof Deno.errors.NotCapable) {
error = codeMap.get("EPERM")!;
} else {
error = codeMap.get("EAI_NODATA")!;
}
}

const error = addresses.length ? 0 : codeMap.get("EAI_NODATA")!;

// TODO(cmorten): needs work
// REF: https://github.com/nodejs/node/blob/master/src/cares_wrap.cc#L1444
if (!verbatim) {
Expand Down
30 changes: 29 additions & 1 deletion tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import net, { Socket } from "node:net";
import fs from "node:fs";
import { text } from "node:stream/consumers";

import { assert, assertEquals, fail } from "@std/assert";
import { assert, assertEquals, assertStringIncludes, fail } from "@std/assert";
import { assertSpyCalls, spy } from "@std/testing/mock";
import { fromFileUrl, relative } from "@std/path";
import { retry } from "@std/async/retry";
Expand Down Expand Up @@ -1916,3 +1916,31 @@ Deno.test("[node/http] supports proxy http request", async () => {
await promise;
await server.finished;
});

Deno.test("[node/http] `request` requires net permission to host and port", {
permissions: { net: ["localhost:4545"] },
}, async () => {
const { promise, resolve } = Promise.withResolvers<void>();
http.request("http://localhost:4545/echo.ts", async (res) => {
assertEquals(res.statusCode, 200);
assertStringIncludes(await text(res), "function echo(");
resolve();
}).end();
await promise;
});

Deno.test(
"[node/http] `request` errors with EPERM error when permission is not granted",
{ permissions: { net: ["localhost:4321"] } }, // wrong permission
async () => {
const { promise, resolve } = Promise.withResolvers<void>();
http.request("http://localhost:4545/echo.ts", async (res) => {})
.on("error", (e) => {
assertEquals(e.message, "getaddrinfo EPERM localhost");
// deno-lint-ignore no-explicit-any
assertEquals((e as any).code, "EPERM");
resolve();
}).end();
await promise;
},
);

0 comments on commit 12435f9

Please sign in to comment.