|
| 1 | +# Security Review |
| 2 | + |
| 3 | +Last reviewed: 2026-04-20 |
| 4 | + |
| 5 | +## Scope |
| 6 | + |
| 7 | +Full codebase review covering: |
| 8 | + |
| 9 | +- Embedded HTTP server with session token auth (tray binary) |
| 10 | +- OS-native scheduler registration (Windows/Linux/macOS) |
| 11 | +- ZIP archive extraction from GitHub |
| 12 | +- Credential handling (keychain + file fallback) |
| 13 | +- Content loading from multiple directory layers |
| 14 | +- Static file serving via embedded filesystem |
| 15 | +- External API interactions (Discovery Center, GitHub, YouTube RSS, ip-api.com) |
| 16 | +- Autostart registration (LaunchAgent plist, .desktop files, Windows Registry) |
| 17 | +- Subprocess execution across the CLI |
| 18 | + |
| 19 | +## Methodology |
| 20 | + |
| 21 | +Three-phase analysis: |
| 22 | + |
| 23 | +1. Broad vulnerability scan across all security-critical areas |
| 24 | +2. Per-finding deep-dive validation by independent reviewers |
| 25 | +3. False-positive filtering against confidence threshold (>=80%) |
| 26 | + |
| 27 | +Categories examined: input validation, authentication/authorization, crypto/secrets management, injection/code execution, data exposure, path traversal, and deserialization. |
| 28 | + |
| 29 | +## Findings |
| 30 | + |
| 31 | +No vulnerabilities met the high-confidence reporting threshold. |
| 32 | + |
| 33 | +## Areas Investigated |
| 34 | + |
| 35 | +### ZIP Extraction (`internal/sync/fetcher.go`) |
| 36 | + |
| 37 | +**Check:** Zip slip attack via `../` entries in downloaded archives. |
| 38 | + |
| 39 | +**Result:** Protected. The extraction code normalizes the destination with `filepath.Abs`, cleans entry paths with `filepath.Join` + `filepath.FromSlash`, and validates with a `strings.HasPrefix` check including the path separator. A dedicated test (`TestFetcher_BlocksZipSlip`) verifies the guard. |
| 40 | + |
| 41 | +### Tray Binary Extraction (`internal/trayctl/extract.go`) |
| 42 | + |
| 43 | +**Check:** Path traversal during tar.gz/zip extraction of the tray companion binary. |
| 44 | + |
| 45 | +**Result:** Safe by design. Archive contents are extracted into memory only (via `io.ReadAll`), then written to a single controlled path returned by `Manager.BinaryPath()`. No intermediate filesystem operations are exposed. |
| 46 | + |
| 47 | +### HTTP Static File Server (`cmd/sap-devs-tray/server.go`) |
| 48 | + |
| 49 | +**Check:** Directory traversal via requests like `../../etc/passwd`. |
| 50 | + |
| 51 | +**Result:** Safe. Static assets are served from a Go `embed.FS`, which is fundamentally bounded to the files embedded at compile time. There is no access to the real filesystem. |
| 52 | + |
| 53 | +### Session Token Auth (`cmd/sap-devs-tray/server.go`) |
| 54 | + |
| 55 | +**Check:** Token leakage via URL query parameters, Referrer headers, or browser history. |
| 56 | + |
| 57 | +**Result:** Acceptable for the threat model. The token is passed in query parameters within a Wails v3 embedded webview (not a regular browser). Mitigating factors: |
| 58 | + |
| 59 | +- Wails webview cannot navigate to external URLs — no Referrer leakage |
| 60 | +- All resources are embedded — no CDN or external font/image loads |
| 61 | +- Server binds to `127.0.0.1` only — no network exposure |
| 62 | +- Token is 128-bit, generated via `crypto/rand` — cryptographically secure |
| 63 | +- Token is per-process and regenerated on each app restart |
| 64 | + |
| 65 | +### OS Scheduler Registration (`internal/service/scheduler_*.go`) |
| 66 | + |
| 67 | +**Check:** Shell command injection via the binary path embedded in scheduled task definitions. |
| 68 | + |
| 69 | +**Result:** Not exploitable. The binary path originates exclusively from `os.Executable()` (resolved through `filepath.EvalSymlinks`), which is system state — not user input. Exploitation would require an attacker to control where the binary is installed, which already grants direct code execution without needing shell injection. Platform-specific notes: |
| 70 | + |
| 71 | +- **Windows:** Double-quoted paths in `cmd /c` cannot be broken by valid Windows filenames (which prohibit `"`) |
| 72 | +- **Linux:** Path in double quotes inside single quotes in the systemd unit; breaking out requires a `'` in the path, but an attacker with that level of filesystem control can replace the binary directly |
| 73 | +- **macOS:** Similar to Linux; plist passes the command string to `/bin/sh -c` |
| 74 | + |
| 75 | +### Autostart Registration (`internal/trayctl/autostart.go`) |
| 76 | + |
| 77 | +**Check:** XML injection (macOS plist) and desktop entry injection (Linux `.desktop` file) via unescaped binary path. |
| 78 | + |
| 79 | +**Result:** Not exploitable. Binary path comes from `os.Executable()`. Additionally: |
| 80 | + |
| 81 | +- macOS: launchd's XML parser treats `<string>` values as literal data, not evaluated for entities |
| 82 | +- Linux: The `Exec=` field in desktop entries is parsed by the desktop environment via direct exec, not shell-evaluated per the freedesktop specification |
| 83 | + |
| 84 | +### Content Loader (`internal/content/loader.go`) |
| 85 | + |
| 86 | +**Check:** Path traversal via pack IDs or content paths, including the project layer (`.sap-devs/`). |
| 87 | + |
| 88 | +**Result:** Safe. `os.ReadDir` returns only immediate child entries (names cannot contain path separators on any filesystem). The project directory is hardcoded to `.sap-devs/` joined with the current working directory — no user-controllable path components. |
| 89 | + |
| 90 | +### Credentials (`internal/credentials/credentials.go`) |
| 91 | + |
| 92 | +**Check:** Token exposure in logs, error messages, or insecure file storage. |
| 93 | + |
| 94 | +**Result:** Safe across all layers of the credential lifecycle. |
| 95 | + |
| 96 | +**Storage:** Primary storage uses the OS keychain via `zalando/go-keyring` (macOS Keychain, Windows Credential Manager, Linux Secret Service). When the keychain is unavailable, the fallback writes to `<configDir>/credentials` with `0600` permissions (owner read/write only). |
| 97 | + |
| 98 | +**No token leakage in logs.** The three `fmt.Fprintf(os.Stderr, ...)` calls in the credentials package only log the keychain error message (e.g., "keychain unavailable: ..."), never the token value itself. |
| 99 | + |
| 100 | +**Token masked in CLI output.** The `config show` command uses a `maskedToken()` helper (`cmd/config.go:71-83`) that displays "set" or "not set" — the actual token value is never printed. |
| 101 | + |
| 102 | +**Shell history warning.** When the user passes a token as a CLI argument (`sap-devs config token <value>`), the code explicitly warns about shell history exposure and suggests using the interactive prompt instead (`cmd/config.go:191-192`). |
| 103 | + |
| 104 | +**Resolution chain is clean.** `Resolve()` checks environment variables (`GH_TOKEN`, `GITHUB_TOKEN`, `GITHUB_TOOLS_SAP_TOKEN`) then keychain/file — the resolved token is never logged or included in error messages at any point in the chain. |
| 105 | + |
| 106 | +**Credential file naming.** The `credFileForUser` function builds the fallback filename from the `service` parameter (e.g., `credentials-youtube`). This value comes from the `--service` CLI flag, which is trusted input. `filepath.Join` normalizes the path, preventing traversal. |
| 107 | + |
| 108 | +### Environment Variable Execution (`TERMINAL`, `PAGER`) |
| 109 | + |
| 110 | +**Check:** Command injection via `$TERMINAL` or `$PAGER` environment variables. |
| 111 | + |
| 112 | +**Result:** Excluded from scope. Environment variables are trusted values in this security model — an attacker who can modify them already has equivalent access to the user's session. |
| 113 | + |
| 114 | +## Positive Security Observations |
| 115 | + |
| 116 | +| Area | Implementation | |
| 117 | +|------|---------------| |
| 118 | +| Zip slip protection | Explicit `HasPrefix` check with path separator, backed by unit test | |
| 119 | +| Session tokens | 128-bit via `crypto/rand.Read` + `hex.EncodeToString` | |
| 120 | +| HTTP server binding | Hardcoded to `127.0.0.1:0` (loopback, random port) | |
| 121 | +| Static asset serving | Go `embed.FS` eliminates directory traversal by design | |
| 122 | +| Binary path resolution | `filepath.EvalSymlinks` before use in scheduler/autostart | |
| 123 | +| Subprocess arguments | `exec.Command` with separate argument strings (not shell-concatenated) | |
| 124 | +| Archive size limits | `io.LimitReader` applied during tray binary extraction | |
| 125 | +| Credential file permissions | `0600` on fallback credential file | |
0 commit comments