mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 19:52:53 +08:00
fix(examples): validate the macos launchagent port as plain decimal (#4733)
## Description
`examples/deployment/macos-launchagent/install.sh` checks the port with
`! [[ "${PORT}" =~ ^[0-9]+$ ]] || [[ "${PORT}" -lt 1024 ]] || [[
"${PORT}" -gt 65535 ]]` (line 126 on `1da8930a`). The regex lets any
digit string through, and `-lt` / `-gt` inside `[[ ]]` do bash
arithmetic, so:
- a leading `0` is read as octal: `0100000` is 32768 and passes; `08080`
is not valid octal, bash prints an arithmetic error, and the failed test
counts as false, so it is accepted;
- a number past 64 bits wraps: `18446744073709553616` passes the range
test;
- `01024` is rejected although the decimal value 1024 is in range.
The real command is `headroom proxy --port`, a `click.IntRange(1,
65535)` (`headroom/cli/proxy.py:298-301`, read, not run), so an accepted
bad value is written into the plist and the LaunchAgent fails when it
starts.
This PR requires 4 or 5 decimal digits with no leading zero
(`^[1-9][0-9]{3,4}$`) before the existing range test. Every accepted
value is then plain decimal 1000-99999, which cannot overflow, and the
`-lt 1024` / `-gt 65535` checks do the rest. A port written with a
leading zero (`01024`) is rejected; that is deliberate.
Part of #4730
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `examples/deployment/macos-launchagent/install.sh`: the port regex is
`^[1-9][0-9]{3,4}$` instead of `^[0-9]+$`. One line.
## Testing
- [x] Unit tests pass (`pytest`) (the new test file only; I did not run
the full suite)
- [ ] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
This is a shell example script, so the Python lint and type checks do
not apply to it. The new test file is checked with ruff (below). The
project's bug-fix policy asks for a test that fails before and passes
after, so a committed test runs the real script (below), and the earlier
probe is kept under it.
### Test Output
#### Committed regression test
`tests/test_macos_launchagent_install_port.py` runs the real
`install.sh` under `bash` with stub `uname`, `launchctl`, `headroom` and
`lsof` on `PATH` and a fake `HOME`. It is skipped when `bash` is not on
`PATH`. Environment: Ubuntu on WSL2, bash, Python venv
`/var/tmp/cb-fix-8mh5/repo/.venv`, pytest `-p no:cacheprovider`, test
file run from a scratch copy (no `headroom` import, no repo conftest).
Same test file, `install.sh` taken from `origin/main` (`1da8930a`):
```text
FAILED test_invalid_port_is_rejected[0100000]
FAILED test_invalid_port_is_rejected[08080]
FAILED test_invalid_port_is_rejected[18446744073709553616]
3 failed, 7 passed in 0.30s
```
Same test file, `install.sh` from this branch:
```text
10 passed in 0.33s
```
`ruff format --check` and `ruff check` on the new file: clean.
#### Earlier manual driver
A small probe takes the condition line that follows `# Validate port
number` out of the given `install.sh` and evaluates it for each value
(Git Bash on Windows 11, bash 5.3.15; `<probe>` stands for the local
path of my probe script).
`origin/main` (`1da8930a`):
```text
condition: ! [[ "${PORT}" =~ ^[0-9]+$ ]] || [[ "${PORT}" -lt 1024 ]] || [[ "${PORT}" -gt 65535 ]]
'0100000' ACCEPT
'08080' <probe>: line 8: [[: 08080: value too great for base (error token is "08080") <probe>: line 8: [[: 08080: value too great for base (error token is "08080") ACCEPT
'18446744073709553616' ACCEPT
'01024' REJECT
'1023' REJECT
'1024' ACCEPT
'8787' ACCEPT
'65535' ACCEPT
'65536' REJECT
'abc' REJECT
'' REJECT
```
This branch:
```text
condition: ! [[ "${PORT}" =~ ^[1-9][0-9]{3,4}$ ]] || [[ "${PORT}" -lt 1024 ]] || [[ "${PORT}" -gt 65535 ]]
'0100000' REJECT
'08080' REJECT
'18446744073709553616' REJECT
'01024' REJECT
'1023' REJECT
'1024' ACCEPT
'8787' ACCEPT
'65535' ACCEPT
'65536' REJECT
'abc' REJECT
'' REJECT
```
`ACCEPT` means the condition is false, so `install.sh` goes on; `REJECT`
means it calls `fatal`. The three bad values and nothing else change
from ACCEPT to REJECT, plus `08080` loses its bash error. `01024` was
already rejected on main and stays rejected.
## Real Behavior Proof
- Environment: Git Bash on Windows 11, bash 5.3.15. No macOS available.
- Exact command / steps: the probe above, run once on `origin/main`'s
`install.sh` and once on this branch's, for the values `0100000 08080
18446744073709553616 01024 1023 1024 8787 65535 65536 abc ''`.
- Observed result: on `origin/main` `0100000`, `08080` (with a bash
arithmetic error) and `18446744073709553616` are accepted; on this
branch all three are rejected, and `1024`, `8787`, `65535` are still
accepted. Tables above.
- Not tested: real macOS launchd; macOS bash 3.2 (the system bash) and
`launchctl`; I did not run `install.sh` end to end on a Mac. That
`headroom proxy --port` rejects such values is read from
`headroom/cli/proxy.py:298-301`, not run.
## Runtime Rollout Safety
- Rollout-managed feature(s): none; example script only.
- Minimum rollout channel: n/a
- Stable/default behavior changed: no for valid ports. Invalid ports
that were accepted are now rejected, and `--port 01024`-style input with
a leading zero is rejected.
- Kill switch / disable path: n/a
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this commit.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes (new test
file only; the probe above is the check)
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Additional Notes
- Scope is an example script. The wrong port only breaks that user's own
LaunchAgent; nothing here is a security issue.
- Other defects in the same directory are in #4730 and separate PRs.
Produced with AI assistance (Claude Code) operated by me (HetCreep); the
probe was run on my machine against a clone of the repository.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
JD Davis
parent
0f3b0d8a21
commit
a91c48a3a4
@@ -123,7 +123,7 @@ else
|
||||
fi
|
||||
|
||||
# Validate port number
|
||||
if ! [[ "${PORT}" =~ ^[0-9]+$ ]] || [[ "${PORT}" -lt 1024 ]] || [[ "${PORT}" -gt 65535 ]]; then
|
||||
if ! [[ "${PORT}" =~ ^[1-9][0-9]{3,4}$ ]] || [[ "${PORT}" -lt 1024 ]] || [[ "${PORT}" -gt 65535 ]]; then
|
||||
fatal "Invalid port number: ${PORT} (must be 1024-65535)"
|
||||
fi
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
"""Run the real macOS LaunchAgent install.sh up to its port validation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import stat
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SCRIPT = (
|
||||
Path(__file__).resolve().parents[1]
|
||||
/ "examples"
|
||||
/ "deployment"
|
||||
/ "macos-launchagent"
|
||||
/ "install.sh"
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
os.name == "nt" or shutil.which("bash") is None,
|
||||
reason="needs POSIX Python and bash: the fixture passes native paths and env to bash",
|
||||
)
|
||||
|
||||
|
||||
def _stub(directory: Path, name: str, body: str) -> None:
|
||||
path = directory / name
|
||||
path.write_text("#!/bin/sh\n" + body + "\n", newline="\n")
|
||||
path.chmod(path.stat().st_mode | stat.S_IEXEC)
|
||||
|
||||
|
||||
def _run_install(tmp_path: Path, port: str) -> subprocess.CompletedProcess[str]:
|
||||
stubs = tmp_path / "stubs"
|
||||
stubs.mkdir()
|
||||
_stub(stubs, "uname", "echo Darwin")
|
||||
_stub(stubs, "headroom", "exit 0")
|
||||
_stub(stubs, "launchctl", "exit 0")
|
||||
_stub(stubs, "lsof", "exit 1")
|
||||
home = tmp_path / "home"
|
||||
home.mkdir()
|
||||
# Only the script is copied, not its plist template: a port that passes
|
||||
# validation stops at "Template file not found", before anything is written
|
||||
# outside tmp_path.
|
||||
script = tmp_path / "install.sh"
|
||||
shutil.copy(SCRIPT, script)
|
||||
env = {
|
||||
**os.environ,
|
||||
"HOME": str(home),
|
||||
"PATH": f"{stubs}{os.pathsep}{os.environ['PATH']}",
|
||||
}
|
||||
return subprocess.run(
|
||||
["bash", str(script), "--port", port, "--unattended"],
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"port",
|
||||
["0100000", "08080", "18446744073709553616", "01024", "1023", "65536", "abc"],
|
||||
)
|
||||
def test_invalid_port_is_rejected(tmp_path: Path, port: str) -> None:
|
||||
result = _run_install(tmp_path, port)
|
||||
assert result.returncode != 0
|
||||
assert "Invalid port number" in result.stderr
|
||||
|
||||
|
||||
@pytest.mark.parametrize("port", ["1024", "8787", "65535"])
|
||||
def test_valid_port_passes_the_port_check(tmp_path: Path, port: str) -> None:
|
||||
result = _run_install(tmp_path, port)
|
||||
assert "Invalid port number" not in result.stderr
|
||||
assert "Template file not found" in result.stderr
|
||||
Reference in New Issue
Block a user