-
Notifications
You must be signed in to change notification settings - Fork 61
🤖 refactor: secure-by-default mux server + consolidate startup via ServerService #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThomasK33
wants to merge
7
commits into
main
Choose a base branch
from
auth-1yke
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−65
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
078e063
Add ServerService lockfile path accessor
ThomasK33 15ce2cd
🤖 tests: add server auth token resolver helper and unit tests
ThomasK33 7d1b65c
🤖 refactor: consolidate mux server CLI startup
ThomasK33 46c9f2a
Pass auth token in smoke test server and oRPC calls
ThomasK33 7ca123c
fix: restore early lockfile check before service initialization
ThomasK33 ff4d794
fix: use LAN URL for remote instructions + escape auth token
ThomasK33 63f963e
fix: replace emoji warning with plain text in CLI output
ThomasK33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { resolveServerAuthToken } from "./serverAuthToken"; | ||
|
|
||
| describe("resolveServerAuthToken", () => { | ||
| test("returns disabled mode when noAuth is true regardless of other values", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: true, | ||
| cliToken: "abc", | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "disabled", token: "" }); | ||
| }); | ||
|
|
||
| test("uses cliToken when provided", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: "abc", | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "abc", source: "cli" }); | ||
| }); | ||
|
|
||
| test("trims cliToken", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: " abc ", | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "abc", source: "cli" }); | ||
| }); | ||
|
|
||
| test("falls through to envToken when cliToken is empty", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: "", | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "env-token", source: "env" }); | ||
| }); | ||
|
|
||
| test("falls through to envToken when cliToken is whitespace only", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: " ", | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "env-token", source: "env" }); | ||
| }); | ||
|
|
||
| test("uses envToken when cliToken is not provided", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: undefined, | ||
| envToken: "env-token", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "env-token", source: "env" }); | ||
| }); | ||
|
|
||
| test("trims envToken", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: undefined, | ||
| envToken: " env-token ", | ||
| }) | ||
| ).toEqual({ mode: "enabled", token: "env-token", source: "env" }); | ||
| }); | ||
|
|
||
| test("uses generated token when neither cliToken nor envToken is provided", () => { | ||
| const generated = resolveServerAuthToken({ | ||
| noAuth: false, | ||
| cliToken: undefined, | ||
| envToken: undefined, | ||
| randomBytesFn: () => Buffer.from("a".repeat(32)), | ||
| }); | ||
|
|
||
| expect(generated).toEqual({ | ||
| mode: "enabled", | ||
| token: Buffer.from("a".repeat(32)).toString("hex"), | ||
| source: "generated", | ||
| }); | ||
| }); | ||
|
|
||
| test("noAuth still wins when cliToken is set", () => { | ||
| expect( | ||
| resolveServerAuthToken({ | ||
| noAuth: true, | ||
| cliToken: "abc", | ||
| envToken: undefined, | ||
| }) | ||
| ).toEqual({ mode: "disabled", token: "" }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { randomBytes } from "crypto"; | ||
|
|
||
| export type ResolvedAuthToken = | ||
| | { mode: "disabled"; token: "" } | ||
| | { mode: "enabled"; token: string; source: "cli" | "env" | "generated" }; | ||
|
|
||
| /** | ||
| * Resolve the auth token for `mux server` from CLI flags, env vars, or generate one. | ||
| * | ||
| * Precedence: --no-auth > --auth-token > MUX_SERVER_AUTH_TOKEN > auto-generated. | ||
| */ | ||
| export function resolveServerAuthToken(opts: { | ||
| noAuth: boolean; | ||
| cliToken: string | undefined; | ||
| envToken: string | undefined; | ||
| /** Injectable for deterministic testing. */ | ||
| randomBytesFn?: (n: number) => Buffer; | ||
| }): ResolvedAuthToken { | ||
| if (opts.noAuth) { | ||
| return { mode: "disabled", token: "" }; | ||
| } | ||
|
|
||
| const cli = opts.cliToken?.trim(); | ||
| if (cli) { | ||
| return { mode: "enabled", token: cli, source: "cli" }; | ||
| } | ||
|
|
||
| const env = opts.envToken?.trim(); | ||
| if (env) { | ||
| return { mode: "enabled", token: env, source: "env" }; | ||
| } | ||
|
|
||
| const gen = (opts.randomBytesFn ?? randomBytes)(32).toString("hex"); | ||
| return { mode: "enabled", token: gen, source: "generated" }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.