mirror of
https://github.com/outline/outline.git
synced 2026-11-25 15:15:15 +03:00
* feat: Support OAuth Client ID Metadata Documents for MCP clients MCP clients can send an https URL as their client_id. The server fetches the metadata document at that URL and caches one team-less client row per URL, so MCP works on the root domain without dynamic registration. Closes #14010 * fix: Address review findings for client ID metadata documents - Only count unexpired tokens when cleaning up metadata document clients - Reject backslashes, whitespace and trailing host dots in client_id URLs - Strip trailing dots before installation host comparisons - Type clientSecret as nullable * fix: Keep OAuth clients with pending authorization codes during cleanup * refactor: Simplify client ID metadata document handling - Gate metadata document fetches on the MCP preference inside the helper - Add isMCPClient getter and a shared client ID prefix constant - Enforce the team/metadata document CHECK in both directions - Index oauthClientId on OAuth token and code tables - Reuse parseDomain, clamp and time helpers * revert: Remove pending authorization code check from OAuth client cleanup * fix: Block all special-use addresses when fetching client metadata documents ALLOWED_PRIVATE_IP_ADDRESSES no longer applies to client ID metadata document requests (draft -02 section 8.6). The fetch wrapper takes an allowIPAddressList option to override the environment list. * fix: Reject empty userinfo in client metadata document URLs https://@host and https://:@host have a userinfo component that the URL parser drops, so check the raw authority (draft -02 section 3). * fix: Accept a root path in client metadata document URLs Draft -02 section 3 marks a root path as NOT RECOMMENDED, not invalid, so https://example.com/ is a valid client_id. A URL with no path is still rejected. * fix: Reject client metadata documents with private key material A jwks key with private or symmetric members (d, p, q, dp, dq, qi, oth, k) makes the document invalid, so it is not cached (draft -02 section 4.1). Public keys are still ignored. * fix: Respect no-cache, no-store and Age for client metadata documents no-store and no-cache now expire the cached document at once, so it is fetched again before the next authorization. Freshness from max-age or Expires (measured from Date) is reduced by Age before clamping (draft -02 section 5.2). * feat: Copy DCR and CIMD client logos to file storage in the background logo_uri was rendered directly on the consent screen, which lets the logo host track users and change the image at any time (draft -02 sections 8.8 and 9.2). A background task now fetches the logo with the strict fetch rules, accepts only PNG, JPEG, GIF and WebP by their leading bytes, and stores it content addressed in the public bucket. Requests never wait for the upload. * fix: Match loopback redirect URIs on any port Native apps such as Claude Code register http://localhost/callback and then listen on a free port, so an exact match rejected them. Loopback redirect URIs now match on any port (RFC 8252 section 7.3); the scheme, host and path must still match, and other URIs still match exactly. * fix: Only break the redirect URL mid-word on the consent screen break-all on the whole notice split words such as "continue" in the loopback warning. It now applies only to the redirect URL. * chore: Shorten the loopback warning on the consent screen * fix: Show the punycode host of client metadata documents on the consent screen A lookalike Unicode host was shown as written. Hostnames now come from a shared getHostname helper that returns the punycode form. * refactor: Move image type detection to ImageHelper and use it for avatars Avatars and integration logos copied from a URL are now stored only if they are PNG, JPEG, GIF or WebP, with the content type detected from the file data rather than the remote host. * fix: Address review findings for OAuth client logos and metadata caching - Count the time since the Date header as age when caching metadata documents, so a document is not cached past its Expires time - Log logo scheduling failures instead of failing registration requests - Run dynamic client registration in a transaction - Delete copied logos from storage when no client uses them * fix: Store one logo per OAuth client and return a distinct metadata document error - Logos are stored per client, so a file is only deleted when its own client stops using it. This removes the reference count and its race. - A metadata document that cannot be fetched or is invalid now returns invalid_client_metadata with the reason, not a 404. Network errors are only logged. - Move scheduleOptional to BaseTask so that any task can use it.
380 lines
11 KiB
TypeScript
380 lines
11 KiB
TypeScript
import { Hour, Minute, Week } from "@shared/utils/time";
|
|
import BaseStorage from "./BaseStorage";
|
|
import env from "@server/env";
|
|
|
|
/**
|
|
* Mock implementation of BaseStorage for testing purposes.
|
|
*/
|
|
class MockStorage extends BaseStorage {
|
|
public storedFiles: Array<{
|
|
key: string;
|
|
body: Buffer;
|
|
contentType?: string;
|
|
acl?: string;
|
|
}> = [];
|
|
|
|
async getPresignedPost() {
|
|
return {};
|
|
}
|
|
|
|
async getFileStream() {
|
|
return null;
|
|
}
|
|
|
|
getUploadUrl() {
|
|
return "https://storage.example.com";
|
|
}
|
|
|
|
getUrlForKey(key: string) {
|
|
return `https://storage.example.com/${key}`;
|
|
}
|
|
|
|
async getSignedUrl() {
|
|
return "https://storage.example.com/signed";
|
|
}
|
|
|
|
async store({
|
|
body,
|
|
contentType,
|
|
key,
|
|
acl,
|
|
}: {
|
|
body: Buffer;
|
|
contentType?: string;
|
|
key: string;
|
|
acl?: string;
|
|
}) {
|
|
this.storedFiles.push({
|
|
key,
|
|
body: body as Buffer,
|
|
contentType,
|
|
acl,
|
|
});
|
|
return this.getUrlForKey(key);
|
|
}
|
|
|
|
async getFileHandle() {
|
|
return { path: "/tmp/test", cleanup: async () => {} };
|
|
}
|
|
|
|
async getFileExists() {
|
|
return false;
|
|
}
|
|
|
|
async moveFile() {}
|
|
|
|
async deleteFile() {}
|
|
|
|
static signingDateFor(expiresIn: number) {
|
|
return MockStorage.getSigningDate(expiresIn);
|
|
}
|
|
}
|
|
|
|
describe("BaseStorage", () => {
|
|
describe("getSigningDate", () => {
|
|
// A time deliberately offset from a window boundary, so that rounding is
|
|
// exercised and two calls cannot straddle a boundary.
|
|
const now = new Date("2026-04-16T12:07:31.500Z");
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(now);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("should return the same date for calls within a window", () => {
|
|
const first = MockStorage.signingDateFor(Hour.seconds);
|
|
vi.setSystemTime(new Date("2026-04-16T12:08:01.500Z"));
|
|
const second = MockStorage.signingDateFor(Hour.seconds);
|
|
expect(second.getTime()).toBe(first.getTime());
|
|
});
|
|
|
|
it("should round down to the start of the window", () => {
|
|
// The window is capped at 15 minutes, so 12:07:31.500 rounds to 12:00.
|
|
const signingDate = MockStorage.signingDateFor(Hour.seconds);
|
|
expect(signingDate.toISOString()).toBe("2026-04-16T12:00:00.000Z");
|
|
});
|
|
|
|
it("should move to the next window once the boundary passes", () => {
|
|
const first = MockStorage.signingDateFor(Hour.seconds);
|
|
vi.setSystemTime(new Date("2026-04-16T12:22:31.500Z"));
|
|
const second = MockStorage.signingDateFor(Hour.seconds);
|
|
expect(second.getTime()).toBeGreaterThan(first.getTime());
|
|
});
|
|
|
|
it("should align to a window no longer than half the lifetime", () => {
|
|
// The lifetime is shorter than the cap, so the window halves to 5
|
|
// minutes and 12:07:31.500 rounds to 12:05.
|
|
const expiresIn = 10 * Minute.seconds;
|
|
const signingDate = MockStorage.signingDateFor(expiresIn);
|
|
const elapsed = now.getTime() - signingDate.getTime();
|
|
|
|
expect(signingDate.toISOString()).toBe("2026-04-16T12:05:00.000Z");
|
|
expect(elapsed).toBeGreaterThanOrEqual(0);
|
|
expect(elapsed).toBeLessThanOrEqual((expiresIn / 2) * 1000);
|
|
});
|
|
|
|
it("should cap the window so long lifetimes stay predictable", () => {
|
|
const signingDate = MockStorage.signingDateFor(Week.seconds);
|
|
const elapsed = now.getTime() - signingDate.getTime();
|
|
expect(elapsed).toBeLessThanOrEqual(BaseStorage.maxSigningWindow * 1000);
|
|
});
|
|
|
|
it("should not align a lifetime too short to divide", () => {
|
|
const signingDate = MockStorage.signingDateFor(1);
|
|
expect(signingDate.getTime()).toBe(now.getTime());
|
|
});
|
|
});
|
|
|
|
describe("getPresignedPut", () => {
|
|
it("should return undefined from default implementation", async () => {
|
|
const storage = new MockStorage();
|
|
const result = await storage.getPresignedPut(
|
|
"uploads/test/key",
|
|
"private",
|
|
1000000,
|
|
"image/png"
|
|
);
|
|
expect(result).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("storeFromUrl", () => {
|
|
let storage: MockStorage;
|
|
|
|
const originalMaxSize = env.FILE_STORAGE_UPLOAD_MAX_SIZE;
|
|
|
|
beforeEach(() => {
|
|
storage = new MockStorage();
|
|
storage.storedFiles = [];
|
|
env.FILE_STORAGE_UPLOAD_MAX_SIZE = 500;
|
|
});
|
|
|
|
afterEach(() => {
|
|
env.FILE_STORAGE_UPLOAD_MAX_SIZE = originalMaxSize;
|
|
});
|
|
|
|
describe("base64 URL size validation", () => {
|
|
it("should enforce maxUploadSize option for base64 URLs", async () => {
|
|
const largeDataSize = 1000;
|
|
const maxUploadSize = 500;
|
|
|
|
const largeBuffer = Buffer.alloc(largeDataSize, "a");
|
|
const base64Data = largeBuffer.toString("base64");
|
|
const base64Url = `data:image/png;base64,${base64Data}`;
|
|
|
|
const result = await storage.storeFromUrl(
|
|
base64Url,
|
|
"test-key",
|
|
"public-read",
|
|
undefined,
|
|
{ maxUploadSize }
|
|
);
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
|
|
it("should enforce FILE_STORAGE_UPLOAD_MAX_SIZE for base64 URLs", async () => {
|
|
const largeDataSize = 1000; // Exceeds our test limit of 500
|
|
|
|
const largeBuffer = Buffer.alloc(largeDataSize, "a");
|
|
const base64Data = largeBuffer.toString("base64");
|
|
const base64Url = `data:image/png;base64,${base64Data}`;
|
|
|
|
const result = await storage.storeFromUrl(
|
|
base64Url,
|
|
"test-key",
|
|
"public-read"
|
|
);
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
|
|
it("should allow base64 URLs within size limits", async () => {
|
|
const dataSize = 100;
|
|
const maxUploadSize = 500;
|
|
|
|
const buffer = Buffer.alloc(dataSize, "a");
|
|
const base64Data = buffer.toString("base64");
|
|
const base64Url = `data:image/png;base64,${base64Data}`;
|
|
|
|
const result = await storage.storeFromUrl(
|
|
base64Url,
|
|
"test-key",
|
|
"public-read",
|
|
undefined,
|
|
{ maxUploadSize }
|
|
);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result?.contentLength).toBe(dataSize);
|
|
expect(result?.contentType).toBe("image/png");
|
|
expect(storage.storedFiles).toHaveLength(1);
|
|
expect(storage.storedFiles[0].body.byteLength).toBe(dataSize);
|
|
});
|
|
|
|
it("should use the minimum of maxUploadSize and FILE_STORAGE_UPLOAD_MAX_SIZE", async () => {
|
|
// Create data that's larger than maxUploadSize but smaller than FILE_STORAGE_UPLOAD_MAX_SIZE
|
|
const maxUploadSize = 200;
|
|
const dataSize = 300;
|
|
|
|
const buffer = Buffer.alloc(dataSize, "a");
|
|
const base64Data = buffer.toString("base64");
|
|
const base64Url = `data:image/png;base64,${base64Data}`;
|
|
|
|
const result = await storage.storeFromUrl(
|
|
base64Url,
|
|
"test-key",
|
|
"public-read",
|
|
undefined,
|
|
{ maxUploadSize }
|
|
);
|
|
|
|
// Should be rejected because it exceeds maxUploadSize even though it's under FILE_STORAGE_UPLOAD_MAX_SIZE
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
|
|
it("should return undefined for empty base64 data", async () => {
|
|
const base64Url = "data:image/png;base64,";
|
|
|
|
const result = await storage.storeFromUrl(
|
|
base64Url,
|
|
"test-key",
|
|
"public-read"
|
|
);
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("imagesOnly", () => {
|
|
it("should store a supported image with the detected content type", async () => {
|
|
const gif = Buffer.from("GIF89a...").toString("base64");
|
|
|
|
const result = await storage.storeFromUrl(
|
|
`data:image/png;base64,${gif}`,
|
|
"test-key",
|
|
"public-read",
|
|
undefined,
|
|
{ imagesOnly: true }
|
|
);
|
|
|
|
expect(result?.contentType).toBe("image/gif");
|
|
expect(storage.storedFiles).toHaveLength(1);
|
|
expect(storage.storedFiles[0].contentType).toBe("image/gif");
|
|
});
|
|
|
|
it("should not store an SVG", async () => {
|
|
const svg = Buffer.from(
|
|
'<svg xmlns="http://www.w3.org/2000/svg"></svg>'
|
|
).toString("base64");
|
|
|
|
const result = await storage.storeFromUrl(
|
|
`data:image/png;base64,${svg}`,
|
|
"test-key",
|
|
"public-read",
|
|
undefined,
|
|
{ imagesOnly: true }
|
|
);
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("internal URL handling", () => {
|
|
it("should return undefined for URLs already on the storage provider", async () => {
|
|
const result = await storage.storeFromUrl(
|
|
"https://storage.example.com/existing-file.png",
|
|
"test-key",
|
|
"public-read"
|
|
);
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(storage.storedFiles).toHaveLength(0);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("getContentDisposition", () => {
|
|
const storage = new MockStorage();
|
|
|
|
it("should include the file name so browsers keep the extension", () => {
|
|
expect(storage.getContentDisposition("text/plain", "config.yaml")).toBe(
|
|
'attachment; filename="config.yaml"'
|
|
);
|
|
});
|
|
|
|
it("should return inline for safe content types", () => {
|
|
expect(storage.getContentDisposition("image/png", "photo.png")).toBe(
|
|
'inline; filename="photo.png"'
|
|
);
|
|
});
|
|
|
|
it("should omit the file name when not provided", () => {
|
|
expect(storage.getContentDisposition("text/plain")).toBe("attachment");
|
|
expect(storage.getContentDisposition("image/png")).toBe("inline");
|
|
});
|
|
|
|
it("should encode a latin-1 file name rather than send it raw", () => {
|
|
expect(
|
|
storage.getContentDisposition("application/zip", "Café-export.zip")
|
|
).toBe(
|
|
"attachment; filename=\"Caf?-export.zip\"; filename*=UTF-8''Caf%C3%A9-export.zip"
|
|
);
|
|
});
|
|
|
|
it("should encode a file name outside latin-1", () => {
|
|
expect(storage.getContentDisposition("image/png", "日本語.png")).toBe(
|
|
"inline; filename=\"???.png\"; filename*=UTF-8''%E6%97%A5%E6%9C%AC%E8%AA%9E.png"
|
|
);
|
|
});
|
|
|
|
it("should only produce US-ASCII, whatever the file name", () => {
|
|
const names = [
|
|
"Zürich Team-export.markdown.zip",
|
|
"naïve.pdf",
|
|
"Ünicode ✨ 日本語.png",
|
|
"plain.txt",
|
|
];
|
|
|
|
for (const name of names) {
|
|
const value = storage.getContentDisposition("application/zip", name);
|
|
expect(value).toMatch(/^[\x20-\x7e]*$/);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("getContentDispositionType", () => {
|
|
const storage = new MockStorage();
|
|
|
|
it("should return attachment when content type is missing", () => {
|
|
expect(storage.getContentDispositionType()).toBe("attachment");
|
|
});
|
|
|
|
it("should return inline for audio, video, and safe types", () => {
|
|
expect(storage.getContentDispositionType("audio/mpeg")).toBe("inline");
|
|
expect(storage.getContentDispositionType("video/mp4")).toBe("inline");
|
|
expect(storage.getContentDispositionType("application/pdf")).toBe(
|
|
"inline"
|
|
);
|
|
});
|
|
|
|
it("should return attachment for other content types", () => {
|
|
expect(storage.getContentDispositionType("image/svg+xml")).toBe(
|
|
"attachment"
|
|
);
|
|
expect(storage.getContentDispositionType("text/plain")).toBe(
|
|
"attachment"
|
|
);
|
|
});
|
|
});
|
|
});
|