-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: bind Ollama to localhost with authenticated reverse proxy #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ericksoa
wants to merge
5
commits into
main
Choose a base branch
from
fix/ollama-localhost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−26
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f06796f
fix(security): bind Ollama to localhost with authenticated proxy
ericksoa 96f248c
test: add E2E test for Ollama auth proxy as parallel CI job
ericksoa a205395
merge: resolve onboard.js conflict with env-name-only credential form
ericksoa 068cde3
fix: address CodeRabbit review feedback on Ollama proxy
ericksoa d18684f
fix: mark ollama-auth-proxy.js as executable (has shebang)
ericksoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/usr/bin/env node | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * Authenticated reverse proxy for Ollama. | ||
| * | ||
| * Ollama has no built-in authentication. This proxy sits in front of it, | ||
| * validating a Bearer token before forwarding requests. Ollama binds to | ||
| * 127.0.0.1 (localhost only) while the proxy listens on 0.0.0.0 so the | ||
| * OpenShell gateway (running in a container) can reach it. | ||
| * | ||
| * Env: | ||
| * OLLAMA_PROXY_TOKEN — required, the Bearer token to validate | ||
| * OLLAMA_PROXY_PORT — listen port (default: 11435) | ||
| * OLLAMA_BACKEND_PORT — Ollama port on localhost (default: 11434) | ||
| */ | ||
|
|
||
| const crypto = require("crypto"); | ||
| const http = require("http"); | ||
|
|
||
| const TOKEN = process.env.OLLAMA_PROXY_TOKEN; | ||
| if (!TOKEN) { | ||
| console.error("OLLAMA_PROXY_TOKEN required"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const LISTEN_PORT = parseInt(process.env.OLLAMA_PROXY_PORT || "11435", 10); | ||
| const BACKEND_PORT = parseInt(process.env.OLLAMA_BACKEND_PORT || "11434", 10); | ||
|
|
||
| const server = http.createServer((clientReq, clientRes) => { | ||
| const auth = clientReq.headers.authorization; | ||
| // Allow unauthenticated health checks (model list only, not inference) | ||
| const isHealthCheck = clientReq.method === "GET" && clientReq.url === "/api/tags"; | ||
| const expected = `Bearer ${TOKEN}`; | ||
| const tokenMatch = auth && auth.length === expected.length && | ||
| crypto.timingSafeEqual(Buffer.from(auth), Buffer.from(expected)); | ||
| if (!isHealthCheck && !tokenMatch) { | ||
| clientRes.writeHead(401, { "Content-Type": "text/plain" }); | ||
| clientRes.end("Unauthorized"); | ||
| return; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Strip the auth header before forwarding to Ollama | ||
| const headers = { ...clientReq.headers }; | ||
| delete headers.authorization; | ||
| delete headers.host; | ||
|
|
||
| const proxyReq = http.request( | ||
| { | ||
| hostname: "127.0.0.1", | ||
| port: BACKEND_PORT, | ||
| path: clientReq.url, | ||
| method: clientReq.method, | ||
| headers, | ||
| }, | ||
| (proxyRes) => { | ||
| clientRes.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
| proxyRes.pipe(clientRes); | ||
| }, | ||
| ); | ||
|
|
||
| proxyReq.on("error", (err) => { | ||
| clientRes.writeHead(502, { "Content-Type": "text/plain" }); | ||
| clientRes.end(`Ollama backend error: ${err.message}`); | ||
| }); | ||
|
|
||
| clientReq.pipe(proxyReq); | ||
| }); | ||
|
|
||
| server.listen(LISTEN_PORT, "0.0.0.0", () => { | ||
| console.log(` Ollama auth proxy listening on 0.0.0.0:${LISTEN_PORT} → 127.0.0.1:${BACKEND_PORT}`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't rotate the proxy token unless the new proxy actually owns
:11435.This helper always generates a new token and backgrounds a new proxy, but it never checks whether
:11435is already occupied or whether the child bound successfully. Since Step 1 only cleans up the OpenShell gateway, rerunning onboarding with Ollama can leave the old proxy serving the old token whileollama-localgets updated to the new one, which breaks later requests from the sandbox.Suggested hardening
function startOllamaAuthProxy() { const crypto = require("crypto"); - ollamaProxyToken = crypto.randomBytes(24).toString("hex"); + if (runCapture("curl -sf http://127.0.0.1:11435/api/tags 2>/dev/null", { ignoreError: true })) { + console.error(" Ollama auth proxy is already running on port 11435. Stop it before rerunning onboard."); + process.exit(1); + } + const token = crypto.randomBytes(24).toString("hex"); run( - `OLLAMA_PROXY_TOKEN=${shellQuote(ollamaProxyToken)} ` + + `OLLAMA_PROXY_TOKEN=${shellQuote(token)} ` + `node "${SCRIPTS}/ollama-auth-proxy.js" > /dev/null 2>&1 &`, { ignoreError: true }, ); sleep(1); + if (!runCapture("curl -sf http://127.0.0.1:11435/api/tags 2>/dev/null", { ignoreError: true })) { + console.error(" Ollama auth proxy failed to start on port 11435."); + process.exit(1); + } + ollamaProxyToken = token; }🤖 Prompt for AI Agents