opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]:[v2] add no password flag for "opencode2 serve"

Open Victozee26 opened this issue 3 months ago • 9 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

V2 serve has auth by default has of V1 which doesn't

so my proposal is to add flags for no authentication or no authentication by default

add

--no-auth 

or

--no-password

for

opencode serve

Victozee26 avatar Aug 17 '26 10:08 Victozee26

some plugins like https://acode.app/plugin/com.victozee.opencode

uses serve

Victozee26 avatar Aug 17 '26 10:08 Victozee26

+1 really tired of trying to use the web UI and having to copy+paste the password. The browser won't remember it for some reason

Zerquix18 avatar Sep 21 '26 09:09 Zerquix18

It appears you can start with serve --service which does not print out a password, but still requires one (aka requests get a 401 status). https://github.com/anomalyco/opencode/pull/46270 contains a possible fix

dekarl avatar Sep 21 '26 09:09 dekarl

Thanks for the suggestion, @Victozee26! You're asking for a way to run opencode serve in v2 without authentication, such as a --no-auth / --no-password flag, or no auth by default as in v1.

In v2 the server always requires a password; this is intentional. If none is set, it generates a random one when it starts and prints it. Turning auth off has security implications, especially when the server is reachable from other machines, so maintainers will need to weigh it up. They'll consider the request.

In the meantime, you can avoid a new random password on every run by setting a fixed one through an environment variable:

OPENCODE_SERVER_PASSWORD=your-password opencode serve

Clients then use that same password (username opencode for HTTP Basic auth).

opencodejr[bot] avatar Oct 28 '26 20:10 opencodejr[bot]

Some of us run this service at home on a local network with no other users, so auth is pointless. And we use a VPN to connect to home in order to access it. Please give a way to disable auth.

sxflynn avatar Oct 29 '26 10:10 sxflynn

Some of us run this service at home on a local network with no other users, so auth is pointless. And we use a VPN to connect to home in order to access it. Please give a way to disable auth.

Same, running opencode server on Tailscale with other teammates and there is no reason to have a password, opencode server is not available on public internet, so entering it in web ui only becomes a nuisance.

Martinsos avatar Nov 16 '26 22:11 Martinsos

Great regression OpenCode team, upgraded to 2.3.0 from 1.9.5 just to get hit with an annoying password for a service hosted on my local network.

I set it to "a". I'm so happy that the one other person in my household won't hack my OpenCode web server thanks to this secure password.

sprakes avatar Nov 17 '26 18:11 sprakes

TL;DR — V2 always requires a password. To stop the browser asking for it, pass the credential alongside the request instead:

  1. In the URL: open http://host:port/?auth_token=<base64("opencode:<password>")>. Convenient for a bookmark, but it only covers that page load and is not persisted.
  2. In an HTTP header: have a reverse proxy add Authorization: Basic <base64("opencode:<password>")> to every request it forwards to the server. This is the approach I use: Authelia handles the actual sign-in, and OpenCode's password is supplied by the proxy, so the browser is never prompted for it a second time.

Both assume a fixed password (OPENCODE_PASSWORD=… for opencode serve, or opencode service set password … for the managed service). With the default generated password the value changes on every restart, and either workaround breaks. Note that neither disables authentication: in the header approach the proxy's own auth becomes the only gate in front of the API.


Workaround: no password prompt behind a reverse proxy

V2 requires a password by design. Three things are worth knowing before working around it:

  • The username is always opencode. It is not configurable and OPENCODE_SERVER_USERNAME is no longer supported — docs. Only the password can change.
  • With no password set, the server generates a random one at every start and prints it (server password <random>). So OPENCODE_PASSWORD="" is treated as "unset", not as "no auth" — docs, code in packages/cli/src/server-process.ts:91-97; the server also rejects an empty password in packages/server/src/process.ts:56-57.
  • In --service mode (opencode serve --service, the default 49374 listener on Linux) the environment variable is ignored: the password is stored in the service config and stays the same (opencode service get password / opencode service set password <value>).

That leaves two problems people usually conflate, and they have separate fixes:

  1. Stop the password changing. Set a fixed one. The web UI stores the server + credential in its browser state, so once you sign in it survives restarts — it only looks like "the browser won't remember it" when a new random password is generated on every start.
  2. Stop the prompt entirely. Put a proxy in front that authenticates on your behalf.

How the proxy trick works

The password is plain HTTP Basic auth: username opencode, password whatever the server uses. Any client can therefore authorize by sending:

Authorization: Basic base64("opencode:<password>")

A reverse proxy adds that header to every upstream request. The browser never receives a 401, so the web UI never shows its sign-in screen. The app shell is served before API auth (packages/cli/src/services/web-ui.ts); it is the /api/* 401s that trigger the password dialog, which is why injecting the header for /api/* is what removes it.

Access control then lives entirely in the proxy — Authelia, oauth2-proxy, mTLS, basic auth, or just a VPN interface.

Requirements and caveats

  • The proxy's auth is now the only gate. opencode's API is equivalent to shell access on the host (PTY, file writes, agents/tools). Do not leave /api/ outside your proxy's auth "to avoid JSON errors" — that publishes a remote shell.
  • Use a fixed password, or the injected header goes stale on the next restart.
  • Bind opencode to loopback / firewall it so the proxy is the only entry point. The password still protects a direct connection, but the point is to keep one door.
  • base64 is encoding, not encryption. The credential is only as safe as the proxy config.
  • Header inheritance gotcha: nginx does not inherit proxy_set_header into a location that declares its own. Adding the header only to location / while keeping a separate location /api/ is the classic "UI loads but the app still asks for a password" bug.

Example (generic nginx)

# Base64 of "opencode:<password>"; compute with:
#   printf 'opencode:%s' '<password>' | base64
location / {
    auth_request /internal/authz;          # your Authelia / oauth2-proxy check

    proxy_pass http://127.0.0.1:4096;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header Authorization "Basic b3BlbmNvZGU6eW91ci1wYXNzd29yZA==";

    # WebSockets (PTY, MCP) and the SSE event stream at /api/event
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_buffering off;
    proxy_read_timeout 86400s;
}

Keep it as one location / when both paths need the same auth: every path then gets the same check and the same injected credential, and there is nothing to forget.

nginx-proxy-manager

Paste this into the proxy host's Advanced tab. Your own auth (Authelia, oauth2-proxy, auth_basic, VPN) is left out on purpose — keep what you already use, and apply it to both locations. Replace the upstream, the base64 credential and the login URL.

# Upstream: 127.0.0.1:49374 is the managed service on Linux
# (4096 is the default for a plain `opencode serve`).
# Credential: base64("opencode:<password>")
#   printf 'opencode:%s' '<password>' | base64

# API, SSE and WebSockets
location /api/ {
    # Must stay authenticated. If you can, use the same check as `location /`.
    auth_request /internal/authz;
    error_page 401 = @api_unauthorized;

    proxy_pass http://127.0.0.1:49374;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Authorization "Basic b3BlbmNvZGU6eW91ci1wYXNzd29yZA==";
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_cache_bypass $http_upgrade;

    proxy_read_timeout 86400s;
    proxy_send_timeout 86400s;
}

# Web UI shell
location / {
    auth_request /internal/authz;

    # Unauthenticated navigations go to your login page
    error_page 401 =302 https://auth.example.com/?rd=$scheme://$http_host$request_uri;

    proxy_pass http://127.0.0.1:49374;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Authorization "Basic b3BlbmNvZGU6eW91ci1wYXNzd29yZA==";
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_cache_bypass $http_upgrade;

    proxy_read_timeout 86400s;
    proxy_send_timeout 86400s;
}

# API/XHR callers get a real 401, never an HTML login page,
# so fetch and the SSE stream never try to parse one.
location @api_unauthorized {
    default_type application/json;
    return 401 '{"_tag":"UnauthorizedError","message":"Authentication required"}';
}

Notes:

  • Repeat the Authorization line in every location. nginx does not inherit it into a location that sets its own proxy_set_header, and Advanced-tab locations count.
  • Connection $http_connection forwards the client's own value, which is correct for both WebSocket upgrades and ordinary SSE requests; a hardcoded Connection "upgrade" is not.
  • /api and /api/ themselves return 404 — there is no handler at the API root. Use /api/info to test: it must be 401 without your proxy session and 200 with it.
  • If the hostname is proxied through Cloudflare, consider DNS-only for it: Cloudflare closes idle connections after ~100s, which interrupts the SSE stream at /api/event and long-lived PTY sockets.

Less invasive alternatives

  • One-shot URL: http://host:port/?auth_token=<base64("opencode:<password>")> authorizes that page load; the app strips the token from the address bar. Handy for bookmarks, but it does not persist.
  • Pairing link: opencode pair prints a single-use link that sets a 30-day session cookie (it targets the managed service, not an arbitrary serve).
  • Fixed password alone stops the churn and lets the web UI's saved credential keep working; the prompt then appears only in a fresh browser profile.

None of this is a --no-auth flag; it makes the mandatory password invisible to the browser while keeping the API behind whatever auth the proxy already provides. One correction I made deliberately: your original /api/ block had no auth_request, which is what exposed the API when we tested it. The NPM example above keeps the header and the streaming settings from your config but puts the auth check back, with a JSON 401 for API callers. If you'd rather post your exact working block as-is, keep the header but add a warning that it leaves the API unauthenticated — that is a remote shell on the host, not just a read-only endpoint.

lstolcman avatar Nov 19 '26 00:11 lstolcman

hi, for what it’s worth, i went with the reverse proxy trick and it works flawlessly !

hugotms avatar Nov 19 '26 01:11 hugotms