Skip to content

Commit 818112b

Browse files
stolinskiclaude
andcommitted
Never store GitHub tokens when pulling the private local image
Only syntaxfm members (checked via gh) attempt an authenticated pull, and it runs with a throwaway Docker config and a PATH that hides credential helpers, so the token is never saved and existing ghcr.io logins are untouched. Non-members and members missing read:packages get one clear line and run signed out. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 13bf9a4 commit 818112b

2 files changed

Lines changed: 86 additions & 32 deletions

File tree

‎packages/auth-local/container.js‎

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Docker and locking internals shared by the dev-server entry (index.js) and the detached updater.
22
import { spawn } from 'node:child_process';
3+
import { access, constants, mkdtemp, rm, symlink } from 'node:fs/promises';
34
import { createServer } from 'node:net';
5+
import { tmpdir } from 'node:os';
6+
import { delimiter, join } from 'node:path';
47

58
export const SYNTAX_AUTH_LOCAL_PORT = 37960;
69
export const SYNTAX_AUTH_LOCAL_ORIGIN = `http://localhost:${SYNTAX_AUTH_LOCAL_PORT}`;
@@ -29,12 +32,15 @@ export function warn(message) {
2932
/**
3033
* @param {string} command
3134
* @param {string[]} args
32-
* @param {string} [input] written to stdin
35+
* @param {{ input?: string, env?: NodeJS.ProcessEnv }} [options]
3336
* @returns {Promise<{ code: number | null, stdout: string, stderr: string }>}
3437
*/
35-
function run(command, args, input) {
38+
function run(command, args, { input, env } = {}) {
3639
return new Promise((resolve) => {
37-
const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'pipe'] });
40+
const child = spawn(command, args, {
41+
env: env ?? process.env,
42+
stdio: ['pipe', 'pipe', 'pipe']
43+
});
3844
let stdout = '';
3945
let stderr = '';
4046
child.stdout.on('data', (chunk) => (stdout += chunk));
@@ -54,31 +60,73 @@ function is_access_denied(stderr) {
5460
return /unauthorized|denied|forbidden/i.test(stderr);
5561
}
5662

57-
// The image is private to the Syntax team, so reuse the developer's GitHub CLI login for ghcr.io.
58-
async function login_with_github_cli() {
59-
const token = await run('gh', ['auth', 'token', '--hostname', 'github.com']);
60-
const user = await run('gh', ['api', 'user', '--jq', '.login']);
61-
if (token.code !== 0 || user.code !== 0) return false;
62-
63-
const login = await run(
64-
'docker',
65-
['login', 'ghcr.io', '--username', user.stdout, '--password-stdin'],
66-
token.stdout
67-
);
68-
return login.code === 0;
63+
async function find_docker_binary() {
64+
for (const directory of (process.env.PATH ?? '').split(delimiter)) {
65+
const candidate = join(directory, 'docker');
66+
try {
67+
await access(candidate, constants.X_OK);
68+
return candidate;
69+
} catch {
70+
// Not in this PATH entry.
71+
}
72+
}
73+
return null;
6974
}
7075

71-
/** @returns {Promise<{ error: string | null, is_denied: boolean }>} */
72-
export async function pull_image() {
73-
let result = await docker(['pull', '--quiet', IMAGE]);
76+
// The image is private to the Syntax team. For a syntaxfm member, pull once with their GitHub CLI
77+
// token in a throwaway Docker config. PATH exposes only the docker binary, so Docker cannot fall
78+
// back to the OS keychain: the token is never saved, and any existing ghcr.io login is untouched.
79+
/** @returns {Promise<{ code: number | null, stdout: string, stderr: string } | null>} */
80+
async function pull_as_syntax_team_member() {
81+
const membership = await run('gh', ['api', 'user/memberships/orgs/syntaxfm', '--jq', '.state']);
82+
if (membership.code !== 0 || membership.stdout !== 'active') return null;
83+
84+
const [user, token, context, docker_binary] = await Promise.all([
85+
run('gh', ['api', 'user', '--jq', '.login']),
86+
run('gh', ['auth', 'token', '--hostname', 'github.com']),
87+
docker(['context', 'inspect', '--format', '{{.Endpoints.docker.Host}}']),
88+
find_docker_binary()
89+
]);
90+
if (user.code !== 0 || token.code !== 0 || context.code !== 0 || !docker_binary) return null;
7491

75-
if (result.code !== 0 && is_access_denied(result.stderr) && (await login_with_github_cli())) {
76-
result = await docker(['pull', '--quiet', IMAGE]);
92+
const config_directory = await mkdtemp(join(tmpdir(), 'syntax-auth-docker-config-'));
93+
const binary_directory = await mkdtemp(join(tmpdir(), 'syntax-auth-docker-bin-'));
94+
try {
95+
await symlink(docker_binary, join(binary_directory, 'docker'));
96+
const env = {
97+
...process.env,
98+
PATH: binary_directory,
99+
DOCKER_CONFIG: config_directory,
100+
DOCKER_HOST: context.stdout
101+
};
102+
const login = await run(
103+
'docker',
104+
['login', 'ghcr.io', '--username', user.stdout, '--password-stdin'],
105+
{ input: token.stdout, env }
106+
);
107+
if (login.code !== 0) return login;
108+
return await run('docker', ['pull', '--quiet', IMAGE], { env });
109+
} finally {
110+
await rm(config_directory, { recursive: true, force: true });
111+
await rm(binary_directory, { recursive: true, force: true });
77112
}
113+
}
114+
115+
/**
116+
* @returns {Promise<{ error: string | null, access_problem: 'not_syntax_team' | 'missing_read_packages' | null }>}
117+
*/
118+
export async function pull_image() {
119+
const result = await docker(['pull', '--quiet', IMAGE]);
120+
if (result.code === 0) return { error: null, access_problem: null };
121+
if (!is_access_denied(result.stderr)) return { error: result.stderr, access_problem: null };
122+
123+
const team_result = await pull_as_syntax_team_member();
124+
if (!team_result) return { error: result.stderr, access_problem: 'not_syntax_team' };
125+
if (team_result.code === 0) return { error: null, access_problem: null };
78126

79-
return result.code === 0
80-
? { error: null, is_denied: false }
81-
: { error: result.stderr, is_denied: is_access_denied(result.stderr) };
127+
return is_access_denied(team_result.stderr)
128+
? { error: team_result.stderr, access_problem: 'missing_read_packages' }
129+
: { error: team_result.stderr, access_problem: null };
82130
}
83131

84132
// True only when Syntax Auth itself answers, never another program that happens to use the port.

‎packages/auth-local/index.js‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import {
2020
with_container_lock
2121
} from './container.js';
2222

23-
const NO_ACCESS_MESSAGE =
24-
'Signed-out mode: local Syntax Auth is for the Syntax team. Team members run ' +
25-
'`gh auth refresh -h github.com -s read:packages` once, then restart dev.';
23+
const ACCESS_MESSAGES = {
24+
not_syntax_team:
25+
'Running signed out: local Syntax Auth is for the Syntax team, via the GitHub CLI (`gh auth login`).',
26+
missing_read_packages:
27+
'Running signed out: run `gh auth refresh -h github.com -s read:packages` once, then restart dev.'
28+
};
29+
// Returned by ensure_image once it has already explained why the app runs signed out.
30+
const SIGNED_OUT = 'signed-out';
2631
const READY_TIMEOUT_MS = 180_000;
2732
const DOCKER_START_TIMEOUT_MS = 120_000;
2833
const UPDATER_PATH = fileURLToPath(new URL('./update.js', import.meta.url));
@@ -58,8 +63,12 @@ async function ensure_image() {
5863
if ((await docker(['image', 'inspect', IMAGE])).code === 0) return null;
5964

6065
log('Downloading local Syntax Auth (first run only)');
61-
const { error, is_denied } = await pull_image();
62-
return is_denied ? NO_ACCESS_MESSAGE : error;
66+
const { error, access_problem } = await pull_image();
67+
if (access_problem) {
68+
warn(ACCESS_MESSAGES[access_problem]);
69+
return SIGNED_OUT;
70+
}
71+
return error;
6372
}
6473

6574
/** @returns {Promise<string | null>} null when Syntax Auth is running or starting. */
@@ -105,10 +114,7 @@ export async function ensure_syntax_auth() {
105114
}
106115

107116
const error = (await ensure_image()) ?? (await with_container_lock(start_container));
108-
if (error === NO_ACCESS_MESSAGE) {
109-
warn(error);
110-
return;
111-
}
117+
if (error === SIGNED_OUT) return;
112118
if (error) {
113119
warn(
114120
error.includes('address already in use')

0 commit comments

Comments
 (0)