Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(oauth): force visible ChatGPT callback

Rahmanb6c86a89f211
diff --git a/app/oauth/authorize/actions.test.ts b/app/oauth/authorize/actions.test.ts
new file mode 100644
index 00000000..a934c937
--- /dev/null
+++ b/app/oauth/authorize/actions.test.ts
@@ -0,0 +1,71 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ requireSession: vi.fn(),
+ getClient: vi.fn(),
+ storeCode: vi.fn(),
+ randomToken: vi.fn(),
+}));
+
+vi.mock("@/lib/auth/require-session", () => ({ requireSession: mocks.requireSession }));
+vi.mock("@/lib/mcp/store", () => ({
+ getClient: mocks.getClient, storeCode: mocks.storeCode, CODE_TTL_MS: 60_000,
+}));
+vi.mock("@/lib/mcp/pkce", () => ({
+ randomToken: mocks.randomToken,
+ isAllowedRedirect: (raw: string) => {
+ try {
+ const url = new URL(raw);
+ return url.protocol === "https:" || (url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname));
+ } catch { return false; }
+ },
+}));
+vi.mock("@/lib/mcp/scope", () => ({
+ mcpEnabled: () => true,
+ parseScope: (value: string) => (["read", "write", "exec"].includes(value) ? value : "read"),
+ clampScope: (value: string) => value,
+}));
+
+const { approve } = await import("./actions");
+
+const form = (redirectUri = "https://chatgpt.com/connector/oauth/test") => {
+ const data = new FormData();
+ data.set("client_id", "chatgpt-client");
+ data.set("redirect_uri", redirectUri);
+ data.set("code_challenge", "challenge");
+ data.set("code_challenge_method", "S256");
+ data.set("state", "state-1");
+ data.set("scope", "exec");
+ return data;
+};
+
+describe("OAuth consent approval", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.requireSession.mockResolvedValue(true);
+ mocks.randomToken.mockReturnValue("mso_code_test");
+ mocks.getClient.mockResolvedValue({ redirectUris: ["https://chatgpt.com/connector/oauth/test"] });
+ });
+
+ it("returns the validated callback for explicit top-level navigation", async () => {
+ const before = Date.now();
+ await expect(approve(form())).resolves.toEqual({
+ ok: true,
+ redirectTo: "https://chatgpt.com/connector/oauth/test?code=mso_code_test&state=state-1",
+ });
+ expect(mocks.storeCode).toHaveBeenCalledWith("mso_code_test", expect.objectContaining({
+ clientId: "chatgpt-client",
+ redirectUri: "https://chatgpt.com/connector/oauth/test",
+ codeChallenge: "challenge",
+ scope: "exec",
+ expiresAt: expect.any(Number),
+ }));
+ expect(mocks.storeCode.mock.calls[0][1].expiresAt).toBeGreaterThanOrEqual(before + 59_000);
+ });
+
+ it("does not mint a code after the owner session expires", async () => {
+ mocks.requireSession.mockResolvedValue(false);
+ await expect(approve(form())).resolves.toEqual({ ok: false, error: "Not signed in." });
+ expect(mocks.storeCode).not.toHaveBeenCalled();
+ });
+});
diff --git a/app/oauth/authorize/actions.ts b/app/oauth/authorize/actions.ts
index b6d11841..05262f2a 100644
--- a/app/oauth/authorize/actions.ts
+++ b/app/oauth/authorize/actions.ts
@@ -1,19 +1,25 @@
"use server";
-import { redirect } from "next/navigation";
import { requireSession } from "@/lib/auth/require-session";
import { getClient, storeCode, CODE_TTL_MS } from "@/lib/mcp/store";
import { randomToken, isAllowedRedirect } from "@/lib/mcp/pkce";
import { parseScope, clampScope, mcpEnabled, type Scope } from "@/lib/mcp/scope";
-// The consent decision. A Server Action rather than an API route because it must
-// be same-origin by construction: this is the one place a signed-in session is
-// converted into a bearer that acts on the host.
-export async function approve(form: FormData): Promise<{ error: string } | never> {
- if (!mcpEnabled()) return { error: "MCP is disabled on this server." };
+export type ApprovalResult =
+ | { ok: false; error: string }
+ | { ok: true; redirectTo: string };
+
+// The consent decision remains a Server Action so the signed-in mutation is
+// same-origin by construction. Return the validated callback URL to the client and
+// let the browser perform an explicit TOP-LEVEL navigation. `redirect()` inside a
+// nested client action is transported over a fetch; the callback can complete and
+// mint a token while the visible MSO tab stays put, which is exactly what the owner
+// observed with ChatGPT.
+export async function approve(form: FormData): Promise<ApprovalResult> {
+ if (!mcpEnabled()) return { ok: false, error: "MCP is disabled on this server." };
// The session cookie is the ONLY thing authorizing this. Re-checked here and not
// inherited from the page render — a page can be cached, an action cannot.
- if (!(await requireSession())) return { error: "Not signed in." };
+ if (!(await requireSession())) return { ok: false, error: "Not signed in." };
const clientId = String(form.get("client_id") ?? "");
const redirectUri = String(form.get("redirect_uri") ?? "");
@@ -22,8 +28,8 @@ export async function approve(form: FormData): Promise<{ error: string } | never
const state = String(form.get("state") ?? "");
const scope: Scope = clampScope(parseScope(String(form.get("scope") ?? "read")));
- if (method !== "S256" || !challenge) return { error: "This client did not use PKCE S256, which mso requires." };
- if (!isAllowedRedirect(redirectUri)) return { error: "The redirect target is not https (or localhost)." };
+ if (method !== "S256" || !challenge) return { ok: false, error: "This client did not use PKCE S256, which mso requires." };
+ if (!isAllowedRedirect(redirectUri)) return { ok: false, error: "The redirect target is not https (or localhost)." };
const client = await getClient(clientId);
// A user-defined client (ChatGPT's flow) never registers, so it has no record
@@ -31,7 +37,7 @@ export async function approve(form: FormData): Promise<{ error: string } | never
// code is still bound to this exact client_id + redirect_uri at exchange. What
// is NOT allowed is a REGISTERED client redirecting somewhere it never declared.
if (client && !client.redirectUris.includes(redirectUri)) {
- return { error: "That redirect target is not registered for this client." };
+ return { ok: false, error: "That redirect target is not registered for this client." };
}
const code = randomToken("mso_code_");
@@ -46,5 +52,5 @@ export async function approve(form: FormData): Promise<{ error: string } | never
const dest = new URL(redirectUri);
dest.searchParams.set("code", code);
if (state) dest.searchParams.set("state", state);
- redirect(dest.toString());
+ return { ok: true, redirectTo: dest.toString() };
}
diff --git a/app/oauth/authorize/consent-form.tsx b/app/oauth/authorize/consent-form.tsx
index aa5ac656..012ec28a 100644
--- a/app/oauth/authorize/consent-form.tsx
+++ b/app/oauth/authorize/consent-form.tsx
@@ -3,6 +3,7 @@
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { approve } from "./actions";
+import { followOAuthRedirect } from "./oauth-navigation";
import type { Scope } from "@/lib/mcp/scope";
const TIERS: { value: Scope; label: string; blurb: string }[] = [
@@ -39,10 +40,14 @@ export function ConsentForm({
<form
action={async (fd) => {
setBusy(true);
- // approve() redirects on success, so anything returned here is a failure.
- const r = await approve(fd);
+ setError(null);
+ const result = await approve(fd);
+ if (result.ok) {
+ followOAuthRedirect(result.redirectTo);
+ return;
+ }
setBusy(false);
- if (r?.error) setError(r.error);
+ setError(result.error);
}}
className="space-y-5"
>
diff --git a/app/oauth/authorize/oauth-navigation.test.ts b/app/oauth/authorize/oauth-navigation.test.ts
new file mode 100644
index 00000000..53e231b9
--- /dev/null
+++ b/app/oauth/authorize/oauth-navigation.test.ts
@@ -0,0 +1,14 @@
+import { describe, expect, it, vi } from "vitest";
+import { followOAuthRedirect } from "./oauth-navigation";
+
+describe("OAuth browser navigation", () => {
+ it("replaces the visible page with the validated ChatGPT callback", () => {
+ const replace = vi.fn();
+ followOAuthRedirect("https://chatgpt.com/connector/oauth/test?code=one", { replace });
+ expect(replace).toHaveBeenCalledWith("https://chatgpt.com/connector/oauth/test?code=one");
+ });
+
+ it("refuses plaintext non-loopback destinations even if called incorrectly", () => {
+ expect(() => followOAuthRedirect("http://example.com/callback", { replace: vi.fn() })).toThrow(/https or a loopback/);
+ });
+});
diff --git a/app/oauth/authorize/oauth-navigation.ts b/app/oauth/authorize/oauth-navigation.ts
new file mode 100644
index 00000000..3b6972af
--- /dev/null
+++ b/app/oauth/authorize/oauth-navigation.ts
@@ -0,0 +1,11 @@
+export type OAuthLocation = Pick<Location, "replace">;
+
+/** Force the OAuth callback into the visible top-level browsing context. */
+export function followOAuthRedirect(raw: string, location: OAuthLocation = window.location): void {
+ const url = new URL(raw);
+ const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]";
+ if (url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) {
+ throw new Error("OAuth redirect must use https or a loopback http address");
+ }
+ location.replace(url.toString());
+}
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 56b00556..712a69b0 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -16,6 +16,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.
**Fixed**
+- `oauth` force visible ChatGPT callback
- `icons` use native macOS and Windows artwork
- `update` remove passwordless sudo requirement
- use official agent logos
diff --git a/docs/MCP.md b/docs/MCP.md
index 4660d7f9..5a42e09f 100644
--- a/docs/MCP.md
+++ b/docs/MCP.md
@@ -115,6 +115,12 @@ by `workflow_start` on each step in that run. Multiple conversations may hold is
workflows in parallel on the same token. A call that omits the id is deliberately
standalone, and an unknown id is refused before the operation runs.
+On approval, MSO returns the validated PKCE callback to the consent client, which uses
+a top-level `location.replace()` rather than a nested Server Action redirect. This avoids
+the confusing state where ChatGPT has already exchanged the code but the MSO tab remains
+open. A setup started from ChatGPT Settings may still return to the app setup/tool-scan
+surface rather than to the exact conversation that was open.
+
Use `skills_search` alone for capability research or an unfamiliar single-step task;
do not call it immediately before `workflow_start` for the same work. `workflow_finish`
requires the exact returned id and, after independent verification, records the redacted
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index 59d23d3e..4613021d 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -34,6 +34,15 @@ visible operational trace, done conditions, rollback, approvals and recipe redac
risk, policy, H1, unresolved placeholders and 200-line ceiling, and is part of the main
`check` gate. The new trusted `mso-skill-authoring` playbook explains the flow.
+OAuth consent now also forces a visible top-level callback. The previous success path
+called Next's `redirect()` from a Server Action nested inside a client form action. The
+authorization callback could run through the action transport—ChatGPT exchanged the code
+and created an exec token—while the MSO tab remained on “Connecting…”. The server action
+now returns only its already-validated, PKCE-bound callback URL, and the client performs
+`window.location.replace()`. This preserves the same-origin approval boundary while making
+the browser visibly leave MSO for ChatGPT; non-HTTPS non-loopback targets are refused again
+at the browser boundary.
+
## 2026-08-19 — MCP one-call bootstrap, visible workflows and faster repository work (DONE)
A refreshed ChatGPT connection proved the server really exposed all 21 MCP tools, but