Skip to content

Commit bfe84e4

Browse files
ralyodioclaude
andauthored
qc login works over SSH: paste-code mode, and paste fallback (qc 0.1.1) (#278)
Running qc on a server sent the browser (on your laptop) to its own 127.0.0.1 after approval, where nothing listens. qc now detects an SSH session with no display and uses the paste-a-code flow from the start; in loopback mode it also accepts the callback URL (or bare code) pasted into the terminal, with the state checked, so a misdirected redirect can always be recovered. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 85203c5 commit bfe84e4

3 files changed

Lines changed: 89 additions & 10 deletions

File tree

‎packages/qryptchat/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@profullstack/qryptchat",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "qc: qrypt.chat in your terminal. A full-screen end-to-end encrypted chat client (ML-KEM-1024), plus a scriptable CLI and an MCP server.",
55
"type": "module",
66
"bin": {

‎src/cli/login.js‎

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,40 @@ const PAGE = (title, body) => `<!doctype html><meta charset="utf-8"><title>${tit
2525
<main><h1>${title}</h1><p>${body}</p></main>`;
2626

2727
/** Open a URL in the default browser; false when there is nothing to open it with. */
28-
export function openBrowser(url, { platform = process.platform, env = process.env } = {}) {
29-
if (env.QC_NO_BROWSER || ((env.SSH_CONNECTION || env.SSH_TTY) && !env.DISPLAY && !env.WAYLAND_DISPLAY && platform === 'linux')) {
30-
return false;
28+
/**
29+
* True when no browser can come back to this machine: an SSH session with no
30+
* display, or QC_NO_BROWSER. A loopback redirect would then land on the
31+
* browser's OWN 127.0.0.1 (your laptop), where nothing is listening.
32+
*/
33+
export function isRemote({ platform = process.platform, env = process.env } = {}) {
34+
if (env.QC_NO_BROWSER) return true;
35+
return !!(env.SSH_CONNECTION || env.SSH_TTY) && !env.DISPLAY && !env.WAYLAND_DISPLAY && platform !== 'darwin' && platform !== 'win32';
36+
}
37+
38+
/**
39+
* A code from what the user pasted: the URL the browser ended up on
40+
* (…/callback?code=…&state=…) or the bare code. A URL whose state does not
41+
* match this login is refused.
42+
*/
43+
export function parsePasted(input, state) {
44+
const text = String(input || '').trim();
45+
if (!text) return null;
46+
if (/^https?:\/\//i.test(text)) {
47+
let url;
48+
try {
49+
url = new URL(text);
50+
} catch {
51+
return null;
52+
}
53+
const got = url.searchParams.get('state');
54+
if (got && got !== state) throw new Error('That link belongs to a different login. Run qc login again.');
55+
return url.searchParams.get('code');
3156
}
57+
return /^[A-Za-z0-9_-]{20,}$/.test(text) ? text : null;
58+
}
59+
60+
export function openBrowser(url, { platform = process.platform, env = process.env } = {}) {
61+
if (isRemote({ platform, env })) return false;
3262
const [cmd, args] =
3363
platform === 'darwin' ? ['open', [url]] : platform === 'win32' ? ['cmd', ['/c', 'start', '', url]] : ['xdg-open', [url]];
3464
try {
@@ -85,7 +115,9 @@ export async function login(options = {}) {
85115

86116
let server;
87117
let redirectUri = 'oob';
88-
if (!options.oob) {
118+
// Over SSH the browser is on another machine: a loopback redirect cannot reach us.
119+
const oob = options.oob || isRemote({ env });
120+
if (!oob) {
89121
server = createServer();
90122
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
91123
redirectUri = `http://127.0.0.1:${server.address().port}/callback`;
@@ -109,21 +141,47 @@ export async function login(options = {}) {
109141
print(` Confirmation code: ${match}`);
110142
print(' Check that the browser shows the same code before you approve.');
111143
print('');
112-
const opened = !options.oob && openBrowser(url.toString(), { env });
144+
const opened = !oob && openBrowser(url.toString(), { env });
113145
print(opened ? ' Opened your browser. If it did not, open:' : ' Open this link in a browser where you are signed in to qrypt.chat:');
114146
print(` ${url.toString()}`);
115147
print('');
116148

117149
let code;
150+
let rl;
118151
try {
119152
if (server) {
120-
code = await waitForCallback(server, state);
153+
// The redirect normally comes straight back. If the browser is somewhere
154+
// else after all, the page it lands on (or the code) can be pasted here.
155+
const waits = [waitForCallback(server, state)];
156+
if (process.stdin.isTTY) {
157+
rl = createInterface({ input: process.stdin, output: process.stderr });
158+
waits.push(
159+
(async () => {
160+
for (;;) {
161+
let line;
162+
try {
163+
line = await rl.question(' Waiting for the browser… or paste the URL it ended up on: ');
164+
} catch {
165+
return new Promise(() => {}); // prompt closed: the callback won
166+
}
167+
const pasted = parsePasted(line, state);
168+
if (pasted) return pasted;
169+
print(' That is not a qc login code or callback URL.');
170+
}
171+
})(),
172+
);
173+
}
174+
code = await Promise.race(waits);
121175
} else {
122-
const rl = createInterface({ input: process.stdin, output: process.stderr });
123-
code = (await rl.question(' Paste the code from the browser: ')).trim();
124-
rl.close();
176+
rl = createInterface({ input: process.stdin, output: process.stderr });
177+
for (;;) {
178+
code = parsePasted(await rl.question(' Paste the code (or the URL) from the browser: '), state);
179+
if (code) break;
180+
print(' That is not a qc login code.');
181+
}
125182
}
126183
} finally {
184+
rl?.close();
127185
server?.close();
128186
}
129187
if (!code) throw new Error('No code came back from the browser.');

‎tests/cli/qc.test.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,24 @@ describe('qc mcp', () => {
156156
expect(await handle(fake, { method: 'notifications/initialized' }, { version: '1' })).toBeUndefined();
157157
});
158158
});
159+
160+
describe('qc login on a remote box', () => {
161+
it('treats SSH without a display as remote (no loopback redirect)', async () => {
162+
const { isRemote } = await import('../../src/cli/login.js');
163+
expect(isRemote({ platform: 'linux', env: { SSH_CONNECTION: '1 2 3 4' } })).toBe(true);
164+
expect(isRemote({ platform: 'linux', env: { SSH_CONNECTION: '1 2 3 4', DISPLAY: ':0' } })).toBe(false);
165+
expect(isRemote({ platform: 'linux', env: {} })).toBe(false);
166+
expect(isRemote({ platform: 'darwin', env: { SSH_TTY: '/dev/pts/1' } })).toBe(false);
167+
expect(isRemote({ platform: 'linux', env: { QC_NO_BROWSER: '1' } })).toBe(true);
168+
});
169+
170+
it('takes a pasted callback URL or a bare code, and refuses another login’s URL', async () => {
171+
const { parsePasted } = await import('../../src/cli/login.js');
172+
const code = '35_r7KgyhCrrz2JNWUpYJFFN8sCOSjQhuQm0NZe8Oi4';
173+
expect(parsePasted(`http://127.0.0.1:35839/callback?code=${code}&state=abc`, 'abc')).toBe(code);
174+
expect(parsePasted(` ${code} `, 'abc')).toBe(code);
175+
expect(() => parsePasted(`http://127.0.0.1:1/callback?code=${code}&state=zzz`, 'abc')).toThrow(/different login/);
176+
expect(parsePasted('nope', 'abc')).toBeNull();
177+
expect(parsePasted('', 'abc')).toBeNull();
178+
});
179+
});

0 commit comments

Comments
 (0)