Skip to content

node:dns: report the c-ares status of resolve*, query* and reverse as is - #47587

Closed
robobun wants to merge 4 commits into
mainfrom
robobun/7726329e/dns-resolve-enodata
Closed

robobun wants to merge 4 commits into
mainfrom
robobun/7726329e/dns-resolve-enodata

Conversation

@robobun

@robobun robobun commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #47578, fixes #37314

Problem

  • dns.resolve6() (and every other resolve*) rejects a NODATA answer (NOERROR with an empty answer section) with queryAaaa ENOTFOUND host. Node rejects it with queryAaaa ENODATA host (dns.NODATA). A malformed name (a..b) gets ENOTFOUND too, where Node reports queryA EBADNAME a..b. Libraries such as the AWS DAX client branch on these codes to tell "no AAAA record" from "no such name".
  • The cause is c_ares::Error::get (src/cares_sys/c_ares.rs:1826). It folds ARES_ENODATA and ARES_ENONAME into ENOTFOUND for every c-ares callback. Error::code folds EBADNAME the same way. Node applies that fold only to the getaddrinfo and getnameinfo codes behind dns.lookup and dns.lookupService (lib/internal/errors.js, dnsException). For resolve*, query* and reverse it reports the c-ares status as is (cares_wrap.cc, ToErrorCodeString).

Fix

  • Error::get and Error::code now map the c-ares status 1:1. A NODATA answer is ENODATA and a malformed name is EBADNAME, for resolve* and reverse, like Node.
  • The fold moves into one named function, Error::for_lookup. The three lookup producers apply it: Error::init_eai (libc, system and libuv backends), the ares_getaddrinfo callback (the c-ares backend, the default on Linux) and the ares_getnameinfo callback (lookupService). dns.lookup, dns.lookupService, fetch() and Bun.connect keep ENOTFOUND on every backend.
  • throwIfEmpty in src/js/node/dns.ts, the fallback for an empty lookup result, reports ENOTFOUND instead of an ENODATA that Node's lookup never emits.
  • Verified: test/js/node/dns/node-dns.test.js (four new tests against a local UDP DNS server, stock bun fails two). Also test/js/node/dns/, test/js/bun/dns/, socket-dns-error.test.ts, dgram.test.ts, udp_socket.test.ts, websocket/error-event.test.ts and the node test-dns.js, test-c-ares.js, test-net-dns-error.js, test-http-dns-error.js files.

Background

  • c-ares reports a query result as an ARES_* status. Error::get turns it into the c_ares::Error enum, and src/runtime/dns_jsc/cares_jsc.rs builds the DNSException from Error::code (without the DNS_ prefix), errno and label.
  • Node has two DNS paths. dns.lookup and dns.lookupService call getaddrinfo(3) and getnameinfo(3) through libuv and map EAI_NODATA and EAI_NONAME to ENOTFOUND. resolve* and reverse call c-ares and report its status unchanged. Bun's lookup can also use ares_getaddrinfo, and its lookupService uses ares_getnameinfo, so the fold has to apply there too.
  • Considered deleting the fold in Error::get alone: the c-ares lookup backend would then report ENODATA and EBADNAME from dns.lookup, which Node never does. Moving the fold to the lookup entry points keeps one rule in one place. node:dns: report locally-rejected malformed names as EBADNAME, not ENOTFOUND #35591 fixes the EBADNAME half with the fold in the lookup request handler. This PR covers that case with the same test input, so node:dns: report locally-rejected malformed names as EBADNAME, not ENOTFOUND #35591 can close.
  • Draft Replace c-ares with a DNS resolver in safe Rust #47076 replaces c-ares and lists "ENODATA is reported as ENOTFOUND" as an unchanged difference. This PR fixes it on main. The new tests describe the expected codes for that rewrite too: the fold belongs to the lookup request kinds, not to every answer.

Downsides

  • Code that matched err.code === "ENOTFOUND" from resolve* or reverse for a NODATA answer or a malformed name now sees ENODATA or EBADNAME. That is the Node behaviour. dns.lookup, lookupService, fetch() and Bun.connect are unchanged.
  • Per call: nothing new on the success path. The error path gains one match on the enum. No allocation, no syscall.
Notes

no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/dns/node-dns.test.js

Error::get folded ARES_ENODATA and ARES_ENONAME into ENOTFOUND for every
c-ares caller, and Error::code folded EBADNAME the same way. Node applies
that fold only to getaddrinfo and getnameinfo, which back dns.lookup and
dns.lookupService. A NODATA answer to resolve6() or reverse() now rejects
with ENODATA and a malformed name with EBADNAME, like Node. The fold moves
to Error::for_lookup, which init_eai and the ares_getaddrinfo and
ares_getnameinfo callbacks apply.
@robobun

robobun commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 8:57 PM PT - Oct 31st, 2026

✅ @robobun, your commit dd9aa5351eefe7eed804dcefbb0b1ff7077845a0 passed in Build #136260! 🎉


🧪   To try this PR locally:

bunx bun-pr 47587

That installs a local version of the PR into your bun-47587 executable, so you can run:

bun-47587 --bun

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 7915f09 and dd9aa53.

Files selected for processing: 2
  • src/cares_sys/c_ares.rs
  • test/js/node/dns/node-dns.test.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

Resolver operations preserve NODATA and malformed-name statuses. Lookup operations map selected statuses to ENOTFOUND. Empty Node DNS results now report ENOTFOUND. New tests cover resolver and lookup error reporting.

Changes

DNS status handling

Layer / File(s) Summary
c-ares status mapping
src/cares_sys/c_ares.rs
Error::get preserves c-ares statuses. Lookup callbacks and getaddrinfo errors apply lookup-specific mapping. Error::code reports distinct codes for EBADNAME and ENONAME.
Node DNS results
src/js/node/dns.ts, test/js/node/dns/node-dns.test.js
Empty results now produce ENOTFOUND. Tests cover NODATA, NXDOMAIN, malformed names, and lookup errors.

Possibly related issues

  • oven-sh/bun#44777 — The resolver status mapping changes address the ENODATA behavior. The cache-TTL issue described there is not addressed.

Suggested reviewers: dylan-conway

Severity of issue fixed: Medium

Merge Risk: Minimal · up to dd9aa

No actionable merge-blocking issue is established. The DNS changes appear mergeable subject to normal build and test checks, including the deferred DNS tests.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly identifies the main change: preserving c-ares status codes for resolve*, query*, and reverse operations.
Description check Passed The description explains the problem, implementation, expected behavior, testing, compatibility effects, and known limitations. It does not use the exact template headings, but it provides the require…
Linked Issues check Passed The PR meets both active linked coding objectives. For #47578, Error::get preserves ARES_ENODATA, so resolve* reports ENODATA for NODATA answers. For #37314, c-ares EBADNAME remains distinct…
Out of Scope Changes check Passed The changes stay within the linked DNS error-mapping scope. The lookup mapping, throwIfEmpty correction, and focused DNS tests support the requested behavior. No unrelated product change is shown in…
Full details: Description check

Explanation

The description explains the problem, implementation, expected behavior, testing, compatibility effects, and known limitations. It does not use the exact template headings, but it provides the required information in equivalent sections.

Full details: Linked Issues check

Explanation

The PR meets both active linked coding objectives. For #47578, Error::get preserves ARES_ENODATA, so resolve* reports ENODATA for NODATA answers. For #37314, c-ares EBADNAME remains distinct, so dns.resolve4 reports EBADNAME for malformed names. The tests cover these cases. Error::for_lookup preserves ENOTFOUND for lookup paths.

Full details: Out of Scope Changes check

Explanation

The changes stay within the linked DNS error-mapping scope. The lookup mapping, throwIfEmpty correction, and focused DNS tests support the requested behavior. No unrelated product change is shown in the reviewed changes.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the inline findings, I also checked the remaining Error::get callers (hostent/reply/any thunks, ares_get_servers_ports/set_servers) — all are resolve*/reverse or channel-config paths where the raw c-ares status is the intended Node behavior — and confirmed no runtime code branches on the folded variants: every Error::ENOTFOUND reference in src/runtime constructs the error for null/invalid-hostname rejections rather than matching on it, and ws.ts's code === "ENOTFOUND" check is fed by the socket connect path, which still folds through init_eai.

Extended reasoning...

The change moves the ENODATA/ENONAME/EBADNAME-to-ENOTFOUND fold out of c_ares::Error::get into a new for_lookup applied only by the getaddrinfo/getnameinfo thunks and init_eai, fixes the code() string table, and rewrites throwIfEmpty in src/js/node/dns.ts; it touches no security-sensitive surface. The source change is small and consistent across the c-ares, libc and libuv backends, but the inline findings about the new tests (one depends on the host's real resolver, one assertion never reaches the fold it claims to cover) mean a human should still look before merging.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread test/js/node/dns/node-dns.test.js Resolved
Comment thread test/js/node/dns/node-dns.test.js Resolved
Comment thread test/js/node/dns/node-dns.test.js Resolved
Comment thread src/js/node/dns.ts Resolved
…ver call

The child test used a..b, which lookup rejects before any query, and a
node:dns lookup that asks the machine's resolver. A non-ASCII label makes
c-ares itself return EBADNAME. The resolve* tests run on Windows too.
Comment thread src/cares_sys/c_ares.rs Resolved
Comment thread src/cares_sys/c_ares.rs Resolved
Comment thread src/cares_sys/c_ares.rs Resolved
Comment thread src/cares_sys/c_ares.rs Resolved
Comment thread src/cares_sys/c_ares.rs Resolved
Comment thread src/cares_sys/c_ares.rs Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The follow-up commits address what I raised earlier (the child test no longer calls the host resolver, the ä.test lookup now passes is_valid_hostname and reaches ares_getaddrinfo, and the resolve* describe runs on Windows), and this pass found no new bugs. Because resolve*/reverse now surface ENODATA/EBADNAME where they used to report ENOTFOUND, a maintainer should still sign off on the user-visible code change.

What was reviewed:

  • Every Error::get caller: the hostent/reply/any thunks (resolve*, reverse) stay unfolded; only the addrinfo and nameinfo thunks plus init_eai fold, and all init_eai callers (dns.rs, FetchTasklet, socket_body, udp_socket) are lookup paths.
  • init_eai on both platforms: the Windows UV_EAI_NODATA/NONAME arms were previously dead behind the early return and now feed .map(for_lookup); the POSIX SOCKTYPE/glibc early returns never produce a folded variant.
  • Existing suite assertions on DNS_ENOTFOUND (test/js/bun/dns/resolve-dns.test.ts) are all lookup-path and keep passing; no test asserted the old No records found/ENODATA from throwIfEmpty.
Extended reasoning...

The change touches src/cares_sys/c_ares.rs (moves the ENODATA/ENONAME/EBADNAME to ENOTFOUND fold out of the shared Error::get into a new Error::for_lookup applied by the getaddrinfo/getnameinfo thunks and init_eai, and renames the DNS_ code strings for EBADNAME and ENONAME), src/js/node/dns.ts (throwIfEmpty reports ENOTFOUND/errno 4), and adds a hermetic local UDP DNS fixture with four tests in test/js/node/dns/node-dns.test.js. It touches no security-sensitive surface. The code addresses the prior inline findings and the sibling-site audit turned up nothing, but the change alters observable error codes of node:dns resolve*/reverse for existing callers and I could not build or run the Rust/test suite here, so a human maintainer should weigh the compat change.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-31.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant