Skip to content

feat: bind to 127.0.0.1 by default instead of 0.0.0.0#2812

Open
bfirsh wants to merge 7 commits into
mainfrom
fix/bind-localhost-by-default
Open

feat: bind to 127.0.0.1 by default instead of 0.0.0.0#2812
bfirsh wants to merge 7 commits into
mainfrom
fix/bind-localhost-by-default

Conversation

@bfirsh

@bfirsh bfirsh commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Default port binding changed from 0.0.0.0 to 127.0.0.1 across all commands, preventing prediction endpoints from being accidentally exposed to the entire network during development.
  • Added --host flag to cog serve and host:port syntax to cog run -p so users can opt into binding to all interfaces when needed.

Changes

File Change
pkg/docker/command/command.go Added HostIP field to Port struct
pkg/docker/docker.go Uses port.HostIP (default 127.0.0.1) instead of hardcoded ""
pkg/docker/run.go GetHostPortForContainer now accepts a hostIP parameter
pkg/docker/run_test.go Updated tests + added new test cases for default and all-interfaces binding
pkg/cli/serve.go Added --host flag (default 127.0.0.1)
pkg/cli/run.go -p flag supports host:port syntax (e.g. -p 0.0.0.0:8888), defaults to 127.0.0.1
pkg/predict/predictor.go cog predict/cog train bind to 127.0.0.1

User-facing behavior

Command Before After
cog serve Binds to 0.0.0.0 Binds to 127.0.0.1. Use --host 0.0.0.0 to expose.
cog run -p 8888 Binds to 0.0.0.0:8888 Binds to 127.0.0.1:8888. Use -p 0.0.0.0:8888 to expose.
cog predict / cog train Binds to 0.0.0.0 Binds to 127.0.0.1

Test plan

  • All 853 Go unit tests pass
  • 0 lint issues

Port bindings now default to localhost-only, preventing prediction
endpoints from being exposed to the entire network during development.

- Add HostIP field to command.Port struct (defaults to 127.0.0.1)
- Add --host flag to cog serve (default 127.0.0.1, use 0.0.0.0 to expose)
- Support host:port syntax in cog run -p (e.g. -p 0.0.0.0:8888)
- Bind cog predict/train to 127.0.0.1
- Update GetHostPortForContainer to match configured host IP
@bfirsh bfirsh requested a review from a team as a code owner March 5, 2026 02:15
@michaeldwan michaeldwan added this to the 0.18.0 milestone Mar 5, 2026
@ask-bonk

ask-bonk Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@ask-bonk

ask-bonk Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

@anish-sahoo Bonk workflow failed. Check the logs for details.

View workflow run · To retry, trigger Bonk again.

@ask-bonk

ask-bonk Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Changes default port binding from 0.0.0.0 to 127.0.0.1 across Docker container operations, with a new --host flag for cog serve and HostIP support in the port abstraction.

Issues found:

  1. cog exec -p silently breaks backward compatibility. pkg/cli/exec.go creates command.Port without setting HostIP. Since docker.go now defaults empty HostIP to 127.0.0.1, cog exec -p 8888 now binds to localhost instead of all interfaces, with no escape hatch for users who need network exposure. Consider adding host:port parsing to the -p flag here, or an explicit --host flag for cog exec.

  2. Misleading URL when explicitly binding to all interfaces. pkg/cli/serve.go substitutes localhost when --host 0.0.0.0 is used, which hides the fact that the server is exposed to the entire network. The displayed URL should reflect the actual binding or include a warning.

@ask-bonk

ask-bonk Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

I'm Bonk, and I've done a quick review of your PR.

Summary: Changes default Docker port binding from 0.0.0.0 to 127.0.0.1, adds --host flag to cog serve, and threads HostIP through the port abstraction.

Issues (highest severity first):

  1. cog exec -p backward compatibility breakpkg/cli/exec.go:123 creates command.Port without HostIP, so it silently falls through to the new 127.0.0.1 default. Users who previously relied on cog exec -p 8888 being reachable from the network now have no escape hatch. Consider adding host:port syntax support or a --host flag to cog exec.

  2. Misleading serve URL when binding to all interfacespkg/cli/serve.go:173-176 displays http://localhost:port even when --host 0.0.0.0 is explicitly passed, which masks the security implication that the server is exposed to the entire network.

I posted the full review as a top-level PR comment because the batch review API returned a 422 (likely stale line numbers). You can view it here: #2812 (comment)

github run

@michaeldwan michaeldwan 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.

Review

Real security improvement — localhost-by-default is the right call, applied centrally at the bind site, with docs regenerated. One blocker and a few should-fix items below.

Blocker

cog exec -p lost its host:port override in the merge. Commit a4aec985 added host:port parsing (-p 0.0.0.0:8000) and a HostIP to pkg/cli/run.go. Main renamed cog runcog exec (ba48ff8e); during the merge (88bde6bd) git couldn't carry those changes into the renamed exec.go, so the current loop is the old strconv.Atoi(portString) with no HostIP and no host:port syntax. Because docker.go now defaults empty HostIP to 127.0.0.1, cog exec -p 8888 jupyter notebook silently became localhost-only with no escape hatch — contradicting the commit message's stated intent ("Support host:port syntax in cog run -p"). exec.go isn't in this diff, so this can't be an inline comment — please restore the host:port parsing from the original run.go diff and update the --publish help text.

Should-fix (inline below)

  • docker.go — the core HostIP propagation change has no unit test.
  • run.go — exact-string HostIP match is brittle; fall back to the single binding when only one exists.
  • command.go — extract a shared DefaultHostIP constant ("127.0.0.1" is duplicated in 5 sites).
  • serve.go — remote-Docker (DOCKER_HOST=tcp://…) users get silent breakage; no warning.

Nits (inline below)

  • serve.go:20 — package var named host is generic; serveHost/bindHost is clearer.
  • serve.go:174 — IPv6 --host values produce malformed display URLs (missing brackets); displayHost logic is also untested.
  • run_test.go — duplicated mock scaffolding, not table-driven; missing edge cases (custom IP, IPv6, inspect returning empty HostIP).

cog predict/cog train hardcoding 127.0.0.1 is intended (local-only) and correct. pkg/registry_testhelpers using 0.0.0.0 directly via nat.PortBinding is correctly unaffected.

Comment thread pkg/cli/serve.go Outdated
Comment thread pkg/cli/serve.go Outdated
Comment thread pkg/cli/serve.go Outdated
Comment thread pkg/docker/command/command.go Outdated
Comment thread pkg/docker/docker.go Outdated
Comment thread pkg/docker/run.go
Comment thread pkg/docker/run_test.go Outdated
- Restore host:port parsing for `cog exec -p` lost in the cog run→exec
  rename; support plain ports, IPv4, bare and bracketed IPv6, with port
  range and empty-host validation
- Extract shared command.DefaultHostIP constant (was duplicated)
- Narrow GetHostPortForContainer fallback to single wildcard/empty bindings
- Warn when serving via a remote Docker daemon; show a navigable localhost
  URL alongside 0.0.0.0; bracket IPv6 display hosts
- Rename generic serve `host` var to `serveHost`
- Add unit tests for port binding helpers, host parsing, remote-host
  detection, and serve URL formatting; make run_test table-driven
- Regenerate docs/cli.md and docs/llms.txt
@ask-bonk

ask-bonk Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@ask-bonk

ask-bonk Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@ask-bonk

ask-bonk Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

APIError: Bad Request: AiError: AiError: Invalid data for image - reason invalid base64 encoding (aad49186-90be-4eb7-8a90-c388fee5327e)

github run

@ask-bonk

ask-bonk Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

APIError: Bad Request: AiError: AiError: Invalid data for image - reason invalid base64 encoding (67ef8603-4189-4822-9de4-14041877885e)

github run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants