🛡️ Sentinel: [CRITICAL] Fix DNS rebinding vulnerability in local API server#143
🛡️ Sentinel: [CRITICAL] Fix DNS rebinding vulnerability in local API server#143Dexploarer wants to merge 1 commit intomainfrom
Conversation
Validates the `Host` header in `src/api/server.ts` to ensure that requests to the API server when running without an authentication token (default local mode) must originate from a legitimate loopback address (`localhost`, `127.0.0.1`, etc.). This prevents DNS rebinding attacks where malicious sites could access the local API via the browser. The `isLoopbackBindHost` function is enhanced to strictly validate IP addresses using `net.isIP` and reject subdomains (e.g., `127.0.0.1.attacker.com`).
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
| if (ipType === 6) { | ||
| return ( | ||
| normalized === "::1" || | ||
| normalized === "0:0:0:0:0:0:0:1" || | ||
| normalized === "::ffff:127.0.0.1" | ||
| ); | ||
| } |
There was a problem hiding this comment.
Limited IPv6 Loopback Detection
The IPv6 loopback check only allows three specific forms: ::1, 0:0:0:0:0:0:0:1, and ::ffff:127.0.0.1. However, there are other valid representations of the IPv6 loopback address (e.g., with leading zeros, or compressed forms). Consider using a more comprehensive check, such as:
if (ipType === 6) {
return net.isIP(normalized) === 6 && normalized.replace(/^0+:/g, ':') === '::1';
}Or use a library that normalizes IPv6 addresses for comparison.
Recommendation:
Expand the IPv6 loopback check to cover all valid representations of the loopback address.
Summary of ChangesHello @Dexploarer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical DNS Rebinding vulnerability in the local API server. By implementing stricter host header validation and introducing a security check for unauthenticated localhost access, it significantly enhances the server's resilience against malicious domain binding attempts, ensuring that implicit trust assumptions for loopback interfaces are not bypassed. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a critical DNS rebinding vulnerability by implementing a stricter isLoopbackBindHost check and enforcing it on unauthenticated API requests. The logic now correctly uses net.isIP to differentiate between IP addresses and hostnames, preventing bypasses. The addition of unit tests in test/unit/loopback-check.test.ts is a great step to ensure the fix is robust. I have one suggestion regarding code duplication in the new test file to improve long-term maintainability.
| function isLoopbackBindHost(host: string): boolean { | ||
| let normalized = host.trim().toLowerCase(); | ||
|
|
||
| if (!normalized) return false; | ||
|
|
||
| // Allow users to provide full URLs by mistake (e.g. http://localhost:2138) | ||
| if (normalized.startsWith("http://") || normalized.startsWith("https://")) { | ||
| try { | ||
| const parsed = new URL(normalized); | ||
| normalized = parsed.hostname.toLowerCase(); | ||
| } catch { | ||
| // Fall through and parse as raw host value. | ||
| } | ||
| } | ||
|
|
||
| // Strip IPv6 brackets | ||
| const bracketedIpv6 = /^\[([^\]]+)\](?::\d+)?$/.exec(normalized); | ||
| if (bracketedIpv6?.[1]) { | ||
| normalized = bracketedIpv6[1]; | ||
| } else { | ||
| // Strip port from IPv4 or hostname | ||
| const singleColonHostPort = /^([^:]+):(\d+)$/.exec(normalized); | ||
| if (singleColonHostPort?.[1]) { | ||
| normalized = singleColonHostPort[1]; | ||
| } | ||
| } | ||
|
|
||
| // Check localhost | ||
| if (normalized === "localhost") return true; | ||
|
|
||
| // Check IPs | ||
| const ipType = net.isIP(normalized); | ||
| if (ipType === 4) { | ||
| return normalized.startsWith("127."); | ||
| } | ||
| if (ipType === 6) { | ||
| return ( | ||
| normalized === "::1" || | ||
| normalized === "0:0:0:0:0:0:0:1" || | ||
| normalized === "::ffff:127.0.0.1" | ||
| ); | ||
| } | ||
|
|
||
| return false; | ||
| } |
There was a problem hiding this comment.
While it's good to have isolated tests for this critical security function, duplicating the isLoopbackBindHost function here creates a maintenance risk. If the original function in src/api/server.ts is updated in the future, this test copy might become outdated, leading to a discrepancy between what's tested and what's running in production.
To improve maintainability and ensure you're always testing the actual implementation, I recommend exporting isLoopbackBindHost from src/api/server.ts and importing it directly into this test file.
Vulnerability: The API server allowed unauthenticated access when bound to localhost (default behavior) but failed to validate the
Hostheader. This made it vulnerable to DNS Rebinding attacks, where an attacker could bind a malicious domain (e.g.,127.0.0.1.attacker.com) to127.0.0.1and bypass the localhost-only implicit trust assumption via a victim's browser.Fix:
isLoopbackBindHostinsrc/api/server.tsto use strict IP validation (net.isIP) and reject hostname-based loopback bypasses (like127.x.x.x.domain.com).handleRequestthat enforcesisLoopbackBindHost(req.headers.host)ifMILADY_API_TOKENis unset.Verification:
Verified using
test/unit/loopback-check.test.ts(created and run locally) to confirm strict validation logic blocks127.0.0.1.attacker.comandattacker.comwhile allowinglocalhostand127.0.0.1. The integration was verified viatest-loopback.tsscript logic.PR created automatically by Jules for task 1749293786095963265 started by @Dexploarer