Skip to content

fix(config): validate HTTP_PORT range and harden Windows browser launch - #1198

Open
nmgaston wants to merge 1 commit into
mainfrom
validateConfigurationPort
Open

fix(config): validate HTTP_PORT range and harden Windows browser launch#1198
nmgaston wants to merge 1 commit into
mainfrom
validateConfigurationPort

Conversation

@nmgaston

@nmgaston nmgaston commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

PR: HTTP Port Validation & Windows Browser Launch Hardening

Summary

This PR adds early HTTP port validation during configuration loading and hardens
Windows browser launch argument handling.

The goal is to fail fast on malformed externally supplied port values
(for example from HTTP_PORT env var), instead of allowing invalid values to
reach later runtime networking/browser paths.


Problem

Malformed HTTP_PORT values are accepted during config load and
can fail later during listen/open-browser flows with less actionable errors.

Example Observed on Windows:

  1. App starts config/init flow

  2. Later fails with:

     listen tcp: lookup tcp/8181 & calc: unknown port
    

This indicates missing early validation of port syntax/range.


What Changed

1. Early HTTP_PORT Validation in Config Load

  • Added validation in config.go inside NewConfig after env overlay
  • Port must be decimal numeric and in valid TCP range 1-65535
  • Added sentinel errors:
    • ErrPortNotNumeric
    • ErrPortOutOfRange
  • Added validatePort helper

2. Windows Browser Launch Hardening

  • Updated Windows browser command args in browser.go to include required
    empty title argument for start:

      cmd /c start "" <url>
    

This avoids start misinterpreting the URL as the window title and reduces
ambiguous command parsing behavior.


Files Changed

File Change
config.go Added validatePort helper and sentinel errors
browser.go Added empty title argument to Windows cmd /c start

Validation Rules

Input Result
8181 ✅ Valid
99999 ErrPortOutOfRange
abc ErrPortNotNumeric
8181& calc ErrPortNotNumeric
-1 ErrPortOutOfRange
`` (empty) ErrPortNotNumeric

Testing

Port Validation

  • Set HTTP_PORT to invalid values and verify app fails fast with clear error
  • Set HTTP_PORT to valid value and verify app starts normally

Windows Browser Launch

  • Verify cmd /c start "" <url> is used on Windows
  • Verify URL is not misinterpreted as window title

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.12%. Comparing base (2296f65) to head (16cb56f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1198      +/-   ##
==========================================
+ Coverage   50.09%   50.12%   +0.03%     
==========================================
  Files         146      146              
  Lines       13552    13561       +9     
==========================================
+ Hits         6789     6798       +9     
  Misses       6171     6171              
  Partials      592      592              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

Pull request overview

This PR tightens runtime configuration validation and adjusts Windows browser launching to avoid cmd start mis-parsing URLs, improving robustness for local UI startup.

Changes:

  • Added HTTP port validation in config.NewConfig() and unit tests for valid/invalid port values.
  • Hardened the Windows cmd /c start invocation by adding the required empty title argument, and updated related tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
config/config.go Adds HTTP port validation to prevent invalid HTTP_PORT values from reaching runtime URL/listener setup.
config/config_test.go Adds targeted tests for port validation and NewConfig() failure on invalid ports.
cmd/app/browser.go Updates Windows browser launch args to include the required empty title parameter for start.
cmd/app/browser_test.go Updates Windows expectations to match the revised browser launch invocation.
Suppressed comments (1)

cmd/app/browser_test.go:46

  • This Windows expectation should match the (quoted) URL argument passed to cmd /c start so the test accurately validates the hardened invocation.
	mockCmdExecutor.On("Execute", "cmd", []string{windowsCmdFlag, windowsCmdStart, windowsCmdTitle, "http://localhost:8080"}).Return(nil)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cmd/app/browser.go Outdated
Comment thread cmd/app/browser_test.go
Comment thread config/config.go Outdated
@nmgaston
nmgaston force-pushed the validateConfigurationPort branch 4 times, most recently from 9ff24db to b036586 Compare August 13, 2026 23:48
@nmgaston
nmgaston requested a lite review from Copilot August 13, 2026 23:50

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (5)

config/config.go:343

  • The port validation errors currently read as if the value necessarily came from the HTTP_PORT env var, but ConsoleConfig.Port can also come from config.yml/defaults. Rewording these to refer to the HTTP port while still mentioning HTTP_PORT makes the failure less misleading.
	ErrPortNotNumeric = errors.New("HTTP_PORT must be a decimal integer")
	ErrPortOutOfRange = errors.New("HTTP_PORT must be in range 1–65535")

cmd/app/browser_test.go:33

  • If the Windows implementation is updated to use rundll32 url.dll,FileProtocolHandler (matching pkg/tray/tray.go), the expected args in this helper should be updated accordingly; otherwise launchBrowser tests will drift from the hardened behavior.
	case "windows":
		return "cmd", []string{windowsCmdFlag, windowsCmdStart, windowsCmdTitle, "\"" + url + "\""}

cmd/app/browser_test.go:47

  • The Windows openBrowser test expectation should align with the safer rundll32 url.dll,FileProtocolHandler invocation if openBrowser is updated accordingly.
	mockCmdExecutor.On("Execute", "cmd", []string{windowsCmdFlag, windowsCmdStart, windowsCmdTitle, "\"http://localhost:8080\""}).Return(nil)

cmd/app/browser.go:68

  • Even with the empty title argument, using cmd /c start is still a shell-mediated launch. Switching to rundll32 url.dll,FileProtocolHandler matches the existing implementation in pkg/tray/tray.go and avoids cmd.exe parsing entirely.
	case "windows":
		cmd = "cmd"
		args = []string{windowsCmdFlag, windowsCmdStart, windowsCmdTitle, "\"" + url + "\""}

cmd/app/browser.go:52

  • This Windows browser-launch path is still built around cmd /c start, which is harder to reason about safely (metacharacter parsing / quoting rules) and duplicates logic that the codebase already avoids elsewhere by using rundll32 url.dll,FileProtocolHandler (e.g., pkg/tray/tray.go:137-141). Consider switching this helper to the same approach.

This issue also appears on line 66 of the same file.

// windowsCmdFlag is the /c flag passed to cmd.exe to run a command and exit.
// windowsCmdStart is the Windows shell verb that opens a URL in the default browser.
// windowsCmdTitle is the mandatory empty window-title argument required by `start` to
// prevent it from interpreting the URL as the window title, which can cause unexpected
// command parsing on Windows when the URL contains special characters.

@nmgaston
nmgaston force-pushed the validateConfigurationPort branch from 62f19a3 to 16cb56f Compare August 14, 2026 00:42
@nmgaston
nmgaston marked this pull request as ready for review August 14, 2026 00:52
@nmgaston
nmgaston requested a review from a team as a code owner August 14, 2026 00:52
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.

2 participants