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(mcp): make ChatGPT file imports region agnostic

rahmanef634486c484c615
diff --git a/lib/mcp/openai-file-upload.test.ts b/lib/mcp/openai-file-upload.test.ts
index 9edfae6a..67d0451a 100644
--- a/lib/mcp/openai-file-upload.test.ts
+++ b/lib/mcp/openai-file-upload.test.ts
@@ -18,78 +18,65 @@ beforeEach(() => {
});
describe("ChatGPT file host allowlist", () => {
- it("accepts the observed OpenAI Southeast Asia blob host", async () => {
- const result = await importOpenAiProvidedFile({
- file: {
- download_url: "https://oaisdmntprseasia.blob.core.windows.net/container/file.png?sig=redacted",
- file_id: "file_test",
- mime_type: "image/png",
- file_name: "file.png",
- size: 4,
- },
- dest: "/home/antinrml/generated-images",
- filename: "file.png",
- });
- expect(result.bytes).toBe(4);
- expect(uploadInto).toHaveBeenCalledTimes(1);
+ const importUrl = async (download_url: string, file_id = "file_test") => importOpenAiProvidedFile({
+ file: {
+ download_url,
+ file_id,
+ mime_type: "image/png",
+ file_name: "file.png",
+ size: 4,
+ },
+ dest: "/home/antinrml/generated-images",
+ filename: "file.png",
});
- it("accepts the observed OpenAI Australia East blob host", async () => {
- const result = await importOpenAiProvidedFile({
- file: {
- download_url: "https://oaisdmntpraustraliaeast.blob.core.windows.net/container/file.png?sig=redacted",
- file_id: "file_test_au",
- mime_type: "image/png",
- file_name: "file-au.png",
- size: 4,
- },
- dest: "/home/antinrml/generated-images",
- filename: "file-au.png",
- });
+ it.each([
+ "oaisdmntprseasia.blob.core.windows.net",
+ "oaisdmntpraustraliaeast.blob.core.windows.net",
+ "oaisdmntprnznorth.blob.core.windows.net",
+ "oaisdmntprindiasocentral.blob.core.windows.net",
+ "oaisdmntprfuture123.blob.core.windows.net",
+ ])("accepts OpenAI regional Azure storage account %s", async (host) => {
+ const result = await importUrl(`https://${host}/container/file.png?sig=redacted`);
expect(result.bytes).toBe(4);
expect(uploadInto).toHaveBeenCalledTimes(1);
});
- it("accepts the observed OpenAI New Zealand North blob host", async () => {
- const result = await importOpenAiProvidedFile({
- file: {
- download_url: "https://oaisdmntprnznorth.blob.core.windows.net/container/file.png?sig=redacted",
- file_id: "file_test_nz",
- mime_type: "image/png",
- file_name: "file-nz.png",
- size: 4,
- },
- dest: "/home/antinrml/generated-images",
- filename: "file-nz.png",
- });
- expect(result.bytes).toBe(4);
- expect(uploadInto).toHaveBeenCalledTimes(1);
+ it.each([
+ "attacker.blob.core.windows.net",
+ "oaisdmntpr.blob.core.windows.net",
+ "oaisdmntpr-seasia.blob.core.windows.net",
+ "evil.oaisdmntprseasia.blob.core.windows.net",
+ "oaisdmntprseasia.blob.core.windows.net.evil.example",
+ ])("rejects non-OpenAI/lookalike Azure host %s", async (host) => {
+ await expect(importUrl(`https://${host}/container/file.png`)).rejects.toThrow("host is not allowed");
+ expect(uploadInto).not.toHaveBeenCalled();
});
- it("accepts the observed OpenAI India South Central blob host", async () => {
- const result = await importOpenAiProvidedFile({
- file: {
- download_url: "https://oaisdmntprindiasocentral.blob.core.windows.net/container/file.png?sig=redacted",
- file_id: "file_test_in",
- mime_type: "image/png",
- file_name: "file-in.png",
- size: 4,
- },
- dest: "/home/antinrml/generated-images",
- filename: "file-in.png",
- });
- expect(result.bytes).toBe(4);
- expect(uploadInto).toHaveBeenCalledTimes(1);
+ it("rejects a redirect from an allowed OpenAI host to an unrelated host", async () => {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(null, {
+ status: 302,
+ headers: { location: "https://attacker.blob.core.windows.net/container/file.png" },
+ })));
+ await expect(importUrl("https://oaisdmntprseasia.blob.core.windows.net/container/file.png"))
+ .rejects.toThrow("host is not allowed: attacker.blob.core.windows.net");
+ expect(uploadInto).not.toHaveBeenCalled();
});
- it("rejects an unrelated Azure blob account", async () => {
- await expect(importOpenAiProvidedFile({
- file: {
- download_url: "https://attacker.blob.core.windows.net/container/file.png",
- file_id: "file_test",
- mime_type: "image/png",
- },
- dest: "/home/antinrml/generated-images",
- })).rejects.toThrow("host is not allowed: attacker.blob.core.windows.net");
+ it("allows redirects only when every hop remains on a trusted OpenAI host", async () => {
+ const fetchMock = vi.fn()
+ .mockResolvedValueOnce(new Response(null, {
+ status: 302,
+ headers: { location: "https://oaisdmntprfuture123.blob.core.windows.net/container/file.png?sig=next" },
+ }))
+ .mockResolvedValueOnce(new Response(Uint8Array.from([137, 80, 78, 71]), {
+ status: 200,
+ headers: { "content-type": "image/png", "content-length": "4" },
+ }));
+ vi.stubGlobal("fetch", fetchMock);
+ const result = await importUrl("https://files.oaiusercontent.com/start/file.png");
+ expect(result.bytes).toBe(4);
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(uploadInto).toHaveBeenCalledTimes(1);
});
});
diff --git a/lib/mcp/openai-file-upload.ts b/lib/mcp/openai-file-upload.ts
index edf1de34..0f8b7c2e 100644
--- a/lib/mcp/openai-file-upload.ts
+++ b/lib/mcp/openai-file-upload.ts
@@ -27,27 +27,37 @@ function trustedDownloadUrl(raw: string): URL {
try { url = new URL(raw); } catch { throw new HostError("file.download_url is invalid"); }
if (url.protocol !== "https:") throw new HostError("file.download_url must use HTTPS");
const host = url.hostname.toLowerCase();
- const exactOpenAiHosts = new Set([
- "files.oaiusercontent.com",
- // ChatGPT-generated conversation files in the Southeast Asia storage region.
- // Keep this exact: accepting arbitrary *.blob.core.windows.net would turn the
- // bridge into a server-side fetch primitive.
- "oaisdmntprseasia.blob.core.windows.net",
- // ChatGPT-generated conversation files observed from the Australia East region.
- // Exact account only; never allow arbitrary Azure Blob hosts.
- "oaisdmntpraustraliaeast.blob.core.windows.net",
- // ChatGPT-generated conversation files observed from the New Zealand North region.
- // Exact account only; arbitrary Azure Blob accounts remain rejected.
- "oaisdmntprnznorth.blob.core.windows.net",
- // ChatGPT native image generation, India South Central.
- "oaisdmntprindiasocentral.blob.core.windows.net",
- ]);
- if (!exactOpenAiHosts.has(host) && !host.endsWith(".oaiusercontent.com")) {
+
+ // ChatGPT temporary files are served either from oaiusercontent.com or from
+ // OpenAI-owned Azure Storage accounts whose globally unique account name uses
+ // the stable `oaisdmntpr<region>` prefix. Match that account family rather than
+ // enumerating regions, but never accept arbitrary *.blob.core.windows.net hosts.
+ // Azure storage account names are globally unique, so an unrelated tenant cannot
+ // create another account with the same full account name.
+ const openAiAzureBlobHost = /^oaisdmntpr[a-z0-9]{2,40}\.blob\.core\.windows\.net$/;
+ const openAiContentHost = host === "files.oaiusercontent.com" || host.endsWith(".oaiusercontent.com");
+ if (!openAiContentHost && !openAiAzureBlobHost.test(host)) {
throw new HostError(`file.download_url host is not allowed: ${host}`);
}
return url;
}
+async function fetchTrustedFile(initialUrl: URL): Promise<Response> {
+ let url = initialUrl;
+ for (let hop = 0; hop <= 3; hop += 1) {
+ const response = await fetch(url, {
+ redirect: "manual",
+ signal: AbortSignal.timeout(60_000),
+ headers: { accept: "image/png,image/webp,image/jpeg,application/octet-stream" },
+ });
+ if (![301, 302, 303, 307, 308].includes(response.status)) return response;
+ const location = response.headers.get("location");
+ if (!location) throw new HostError("OpenAI file redirect was missing a location");
+ url = trustedDownloadUrl(new URL(location, url).toString());
+ }
+ throw new HostError("OpenAI file download exceeded the redirect limit");
+}
+
function safeFilename(input: string | undefined, fallback: string): string {
const value = path.basename((input || fallback).trim());
if (!value || value === "." || value === ".." || value.length > 200) throw new HostError("filename is invalid");
@@ -76,11 +86,7 @@ export async function importOpenAiProvidedFile(opts: {
throw new HostError("file exceeds the 20 MiB MCP import limit");
}
- const response = await fetch(url, {
- redirect: "follow",
- signal: AbortSignal.timeout(60_000),
- headers: { accept: "image/png,image/webp,image/jpeg,application/octet-stream" },
- });
+ const response = await fetchTrustedFile(url);
if (!response.ok) throw new HostError(`OpenAI file download failed (${response.status})`);
const declared = Number(response.headers.get("content-length") || 0);
if (declared > MAX_FILE_BYTES) throw new HostError("file exceeds the 20 MiB MCP import limit");