-
Notifications
You must be signed in to change notification settings - Fork 137
feat: append cli_context param to altimate auth URL for PostHog session correlation #1068
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
altimate-harness-bot
wants to merge
4
commits into
main
Choose a base branch
from
feat/cli-context-auth
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b8c72c5
feat: [AI] add cli_context to browser auth URL for PostHog session co…
3395c4c
fix: [AI] address PR review issues on cli_context auth URL param
d6a0c68
fix: [AI] address code review feedback on cli_context auth URL param
43fb343
fix: [AI] address second-round review on cli_context auth URL param
saravmajestic 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
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,105 @@ | ||
| // altimate_change — shared machine-id helper extracted from plugin/altimate.ts. | ||
| // All three call sites (telemetry/index.ts, plugin/altimate.ts, cli/welcome.ts) | ||
| // use this to guarantee they converge on the same file and the same UUID value. | ||
| import { randomUUID } from "crypto" | ||
| import fs from "fs" | ||
| import os from "os" | ||
| import path from "path" | ||
| import { Log } from "./log" | ||
|
|
||
| const log = Log.create({ service: "machine-id" }) | ||
|
|
||
| // Max bytes to read from the machine-id file. A UUID is 36 chars; 512 bytes | ||
| // is generous enough for any valid value while capping pathological cases | ||
| // (multi-MB symlink targets, garbage-filled files). | ||
| const MAX_BYTES = 512 | ||
|
|
||
| // RFC 4122 v4 UUID — the only format we mint, so the only format we accept. | ||
| const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i | ||
|
|
||
| /** | ||
| * Read the machine-id from `~/.altimate/machine-id`, minting a new random UUID | ||
| * with `flag: "wx"` (exclusive create) if the file is absent. | ||
| * | ||
| * - **Race-safe**: two concurrent callers on a fresh install converge on the | ||
| * same UUID — the winner writes, the loser re-reads. | ||
| * - **Regular-file-only**: uses `lstat` and rejects symlinks / non-regular | ||
| * files rather than following them to an attacker-chosen target. | ||
| * - **Size-capped**: reads at most 512 bytes to avoid multi-MB file attacks. | ||
| * - **UUID-validated**: rejects content that does not match RFC 4122 v4 UUID | ||
| * format (corrupt file, injected content, etc.) and returns `""` with a warn | ||
| * log so callers can omit the field rather than propagate garbage. | ||
| * | ||
| * @param machineIdPath Override path (for tests). Defaults to `~/.altimate/machine-id`. | ||
| * @returns A v4 UUID string, or `""` if the value is invalid or unreadable. | ||
| */ | ||
| export function getOrCreateMachineId(machineIdPath?: string): string { | ||
| const idPath = machineIdPath ?? path.join(os.homedir(), ".altimate", "machine-id") | ||
|
|
||
| // --- Read path --- | ||
| let raw: string | undefined | ||
| try { | ||
| // lstat (not stat) so a symlink is inspected as itself rather than followed | ||
| // to an attacker-chosen target. Reject anything that is not a regular file | ||
| // (symlink, directory, socket, …) and cap read size to avoid multi-MB files. | ||
| const stat = fs.lstatSync(idPath) | ||
| if (!stat.isFile()) { | ||
| log.warn("machine-id is not a regular file — omitting", { path: idPath }) | ||
| return "" | ||
| } | ||
| if (stat.size > MAX_BYTES) { | ||
| log.warn("machine-id file exceeds size limit — omitting", { path: idPath, size: stat.size }) | ||
| return "" | ||
| } | ||
| raw = fs.readFileSync(idPath, "utf8").trim() | ||
| } catch (readErr) { | ||
| const code = (readErr as NodeJS.ErrnoException)?.code | ||
| if (code !== "ENOENT") { | ||
| // EACCES, EMFILE, etc. — log and bail; we cannot create either. | ||
| log.warn("machine-id read failed", { code, path: idPath }) | ||
| return "" | ||
| } | ||
| // File absent — fall through to create path below. | ||
| raw = undefined | ||
| } | ||
|
|
||
| if (raw !== undefined) { | ||
| // Validate before returning: reject corrupt or symlink-injected content. | ||
| if (!UUID_RE.test(raw)) { | ||
| log.warn("machine-id file contains non-UUID content — omitting", { path: idPath }) | ||
| return "" | ||
| } | ||
| return raw | ||
| } | ||
|
|
||
| // --- Create path (ENOENT) --- | ||
| // `flag: "wx"` is atomic exclusive-create: the OS guarantees only one writer | ||
| // succeeds. The loser re-reads what the winner wrote. | ||
| const candidate = randomUUID() | ||
| fs.mkdirSync(path.dirname(idPath), { recursive: true }) | ||
| try { | ||
| fs.writeFileSync(idPath, candidate, { encoding: "utf8", flag: "wx" }) | ||
| return candidate | ||
| } catch (writeErr) { | ||
| const code = (writeErr as NodeJS.ErrnoException)?.code | ||
| if (code !== "EEXIST") { | ||
| log.warn("machine-id create failed", { code, path: idPath }) | ||
| return "" | ||
| } | ||
| // Lost the race — read what the winner wrote. | ||
| try { | ||
| const winner = fs.readFileSync(idPath, "utf8").trim() | ||
| if (!UUID_RE.test(winner)) { | ||
| log.warn("machine-id written by race winner is non-UUID — omitting", { path: idPath }) | ||
| return "" | ||
| } | ||
| return winner | ||
| } catch (rereadErr) { | ||
| log.warn("machine-id re-read after race failed", { | ||
| code: (rereadErr as NodeJS.ErrnoException)?.code, | ||
| path: idPath, | ||
| }) | ||
| return "" | ||
| } | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MINOR — Documentation: the frontend contract is unspecified, and this payload is untrusted input on that side
Nothing records what the consumer must do: decode base64url (not standard base64), require
v === 1, cap decoded size, catch base64/JSON parse failures, validate field types, and treatcli_version: "local"as a legitimate dev value rather than a release.From the frontend's perspective a URL query param is attacker-suppliable — anyone can hand-craft one and load
/register.Fix: add JSDoc here stating the contract, and make sure the companion PR validates rather than trusts.