mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
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.
This commit is contained in:
16 files changed
+343
-227
No files matched your search
@@ -23,6 +23,7 @@ import {
|
||||
AuthorizationError,
|
||||
BadRequestError,
|
||||
NotFoundError,
|
||||
UnprocessableEntityError,
|
||||
} from "~/utils/errors";
|
||||
import isCloudHosted from "~/utils/isCloudHosted";
|
||||
import { detectLanguage } from "~/utils/language";
|
||||
@@ -169,6 +170,13 @@ function Authorize() {
|
||||
)}
|
||||
<Pre>{redirectUri}</Pre>
|
||||
</Text>
|
||||
) : clientError instanceof UnprocessableEntityError ? (
|
||||
<Text as="p" type="secondary">
|
||||
{t(
|
||||
"The OAuth client could not be loaded from its metadata document"
|
||||
)}
|
||||
<Pre>{clientError.message}</Pre>
|
||||
</Text>
|
||||
) : clientError instanceof AuthorizationError ? (
|
||||
<Text as="p" type="secondary">
|
||||
{t(
|
||||
|
||||
@@ -251,6 +251,15 @@ export function UnprocessableEntityError(
|
||||
});
|
||||
}
|
||||
|
||||
export function InvalidClientMetadataError(
|
||||
message = "The client metadata document is invalid"
|
||||
) {
|
||||
return httpErrors(422, message, {
|
||||
id: "invalid_client_metadata",
|
||||
isReportable: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function RequestTimeoutError(
|
||||
message = "Request took too long to complete"
|
||||
) {
|
||||
|
||||
@@ -65,49 +65,71 @@ describe("OAuthClient", () => {
|
||||
});
|
||||
|
||||
describe("logos", () => {
|
||||
const buildLogoUrl = () =>
|
||||
const buildHash = () => randomUUID().replace(/-/g, "").repeat(2);
|
||||
const buildLogoUrl = (clientId: string) =>
|
||||
FileStorage.getUrlForKey(
|
||||
OAuthClient.logoKey(randomUUID().replace(/-/g, "").repeat(2), "png")
|
||||
OAuthClient.logoKey(clientId, buildHash(), "png")
|
||||
);
|
||||
const buildClientWithLogo = () => {
|
||||
const id = randomUUID();
|
||||
return buildMetadataDocumentOAuthClient({
|
||||
id,
|
||||
avatarUrl: buildLogoUrl(id),
|
||||
});
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("should find the key of a copied logo", () => {
|
||||
const key = OAuthClient.logoKey("a".repeat(64), "png");
|
||||
it("should find the key of a copied logo", async () => {
|
||||
const client = await buildMetadataDocumentOAuthClient();
|
||||
const key = OAuthClient.logoKey(client.id, "a".repeat(64), "png");
|
||||
|
||||
expect(OAuthClient.logoKeyFromUrl(FileStorage.getUrlForKey(key))).toBe(
|
||||
key
|
||||
);
|
||||
expect(client.logoKeyFromUrl(FileStorage.getUrlForKey(key))).toBe(key);
|
||||
});
|
||||
|
||||
it("should not find a key in a URL that names the same path elsewhere", () => {
|
||||
const key = OAuthClient.logoKey("a".repeat(64), "png");
|
||||
it("should not find a key in a URL on another host", async () => {
|
||||
const client = await buildMetadataDocumentOAuthClient();
|
||||
const key = OAuthClient.logoKey(client.id, "a".repeat(64), "png");
|
||||
|
||||
expect(
|
||||
OAuthClient.logoKeyFromUrl(`https://evil.example.com/${key}`)
|
||||
client.logoKeyFromUrl(`https://evil.example.com/${key}`)
|
||||
).toBeUndefined();
|
||||
expect(client.logoKeyFromUrl(null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should not find a key for the logo of another client", async () => {
|
||||
const client = await buildMetadataDocumentOAuthClient();
|
||||
|
||||
expect(client.logoKeyFromUrl(buildLogoUrl(randomUUID()))).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should not find a key that leaves the folder of the client", async () => {
|
||||
const client = await buildMetadataDocumentOAuthClient();
|
||||
const key = `${OAuthClient.logoKeyPrefix(client.id)}..`;
|
||||
|
||||
expect(
|
||||
client.logoKeyFromUrl(FileStorage.getUrlForKey(key))
|
||||
).toBeUndefined();
|
||||
expect(OAuthClient.logoKeyFromUrl(null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should delete the logo when the client is deleted", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const avatarUrl = buildLogoUrl();
|
||||
const client = await buildMetadataDocumentOAuthClient({ avatarUrl });
|
||||
const client = await buildClientWithLogo();
|
||||
const key = client.logoKeyFromUrl(client.avatarUrl);
|
||||
|
||||
await client.destroy({ force: true });
|
||||
|
||||
expect(deleteFile).toHaveBeenCalledWith(
|
||||
OAuthClient.logoKeyFromUrl(avatarUrl)
|
||||
);
|
||||
expect(deleteFile).toHaveBeenCalledWith(key);
|
||||
});
|
||||
|
||||
it("should keep a logo that another client uses", async () => {
|
||||
it("should not delete the logo of another client", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const avatarUrl = buildLogoUrl();
|
||||
const client = await buildMetadataDocumentOAuthClient({ avatarUrl });
|
||||
await buildMetadataDocumentOAuthClient({ avatarUrl });
|
||||
const other = await buildClientWithLogo();
|
||||
const client = await buildMetadataDocumentOAuthClient({
|
||||
avatarUrl: other.avatarUrl,
|
||||
});
|
||||
|
||||
await client.destroy();
|
||||
|
||||
@@ -116,21 +138,17 @@ describe("OAuthClient", () => {
|
||||
|
||||
it("should delete a replaced logo", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const avatarUrl = buildLogoUrl();
|
||||
const client = await buildMetadataDocumentOAuthClient({ avatarUrl });
|
||||
const client = await buildClientWithLogo();
|
||||
const key = client.logoKeyFromUrl(client.avatarUrl);
|
||||
|
||||
await client.update({ avatarUrl: buildLogoUrl() });
|
||||
await client.update({ avatarUrl: buildLogoUrl(client.id) });
|
||||
|
||||
expect(deleteFile).toHaveBeenCalledWith(
|
||||
OAuthClient.logoKeyFromUrl(avatarUrl)
|
||||
);
|
||||
expect(deleteFile).toHaveBeenCalledWith(key);
|
||||
});
|
||||
|
||||
it("should not delete a logo when the transaction rolls back", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const client = await buildMetadataDocumentOAuthClient({
|
||||
avatarUrl: buildLogoUrl(),
|
||||
});
|
||||
const client = await buildClientWithLogo();
|
||||
|
||||
await expect(
|
||||
sequelize.transaction(async (transaction) => {
|
||||
|
||||
@@ -216,6 +216,63 @@ class OAuthClient extends ParanoidModel<
|
||||
return OAuthClient.isMetadataDocumentClientId(this.clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the storage key of a logo that was copied for this client.
|
||||
*
|
||||
* @param avatarUrl An avatar URL of this client.
|
||||
* @returns The storage key, or undefined if the URL is not a copied logo.
|
||||
*/
|
||||
public logoKeyFromUrl(avatarUrl: string | null | undefined) {
|
||||
const prefix = OAuthClient.logoKeyPrefix(this.id);
|
||||
const index = avatarUrl?.lastIndexOf(prefix) ?? -1;
|
||||
if (!avatarUrl || index === -1) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const key = avatarUrl.slice(index);
|
||||
if (!/^[0-9a-f]{64}\.[^/]+$/.test(key.slice(prefix.length))) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Only a URL that this installation built can name a file to delete.
|
||||
return FileStorage.getUrlForKey(key) === avatarUrl ? key : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a logo that was copied for this client from file storage. When a
|
||||
* transaction is given, this runs after it commits.
|
||||
*
|
||||
* @param avatarUrl The avatar URL that this client no longer uses.
|
||||
* @param transaction The transaction of the change, if any.
|
||||
*/
|
||||
private async deleteLogoFile(
|
||||
avatarUrl: string | null | undefined,
|
||||
transaction?: Transaction | null
|
||||
) {
|
||||
const key = this.logoKeyFromUrl(avatarUrl);
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
|
||||
const deleteFile = async () => {
|
||||
try {
|
||||
await FileStorage.deleteFile(key);
|
||||
} catch (err) {
|
||||
// A failed delete must not fail the change to the client.
|
||||
Logger.warn("Failed to delete OAuth client logo from storage", {
|
||||
key,
|
||||
error: errToString(err),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (transaction) {
|
||||
(transaction.parent || transaction).afterCommit(deleteFile);
|
||||
} else {
|
||||
await deleteFile();
|
||||
}
|
||||
}
|
||||
|
||||
// hooks
|
||||
|
||||
@BeforeCreate
|
||||
@@ -241,7 +298,7 @@ class OAuthClient extends ParanoidModel<
|
||||
options: InstanceUpdateOptions
|
||||
) {
|
||||
if (model.changed("avatarUrl")) {
|
||||
await OAuthClient.deleteUnusedLogo(
|
||||
await model.deleteLogoFile(
|
||||
model.previous("avatarUrl"),
|
||||
options.transaction
|
||||
);
|
||||
@@ -253,79 +310,33 @@ class OAuthClient extends ParanoidModel<
|
||||
model: OAuthClient,
|
||||
options: InstanceDestroyOptions
|
||||
) {
|
||||
await OAuthClient.deleteUnusedLogo(model.avatarUrl, options.transaction);
|
||||
await model.deleteLogoFile(model.avatarUrl, options.transaction);
|
||||
}
|
||||
|
||||
// static methods
|
||||
|
||||
/**
|
||||
* Returns the storage key for a logo copied from a client's logo URL. Keys
|
||||
* are content addressed, so clients that share a logo share one file.
|
||||
* Returns the storage key for a logo copied from a client's logo URL. Each
|
||||
* client has its own files, and the hash changes the URL when the logo
|
||||
* changes so that caches do not show an old logo.
|
||||
*
|
||||
* @param clientId The ID of the client.
|
||||
* @param hash The SHA-256 hash of the logo, in hex.
|
||||
* @param extension The file extension of the logo.
|
||||
* @returns The storage key.
|
||||
*/
|
||||
public static logoKey(hash: string, extension: string) {
|
||||
return `${Buckets.public}/oauth-clients/${hash}/logo.${extension}`;
|
||||
public static logoKey(clientId: string, hash: string, extension: string) {
|
||||
return `${OAuthClient.logoKeyPrefix(clientId)}${hash}.${extension}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the storage key of a copied logo from its URL.
|
||||
* Returns the storage key prefix for the logos of a client.
|
||||
*
|
||||
* @param avatarUrl The avatar URL of a client.
|
||||
* @returns The storage key, or undefined if the URL is not a copied logo.
|
||||
* @param clientId The ID of the client.
|
||||
* @returns The storage key prefix, with a trailing slash.
|
||||
*/
|
||||
public static logoKeyFromUrl(avatarUrl: string | null | undefined) {
|
||||
const key = avatarUrl?.match(
|
||||
/public\/oauth-clients\/[0-9a-f]{64}\/[^/]+$/
|
||||
)?.[0];
|
||||
if (!key) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Only a URL that this installation built can name a file to delete.
|
||||
return FileStorage.getUrlForKey(key) === avatarUrl ? key : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a copied logo from file storage when no client uses it. When a
|
||||
* transaction is given, this runs after it commits.
|
||||
*
|
||||
* @param avatarUrl The avatar URL that a client no longer uses.
|
||||
* @param transaction The transaction of the change, if any.
|
||||
*/
|
||||
private static async deleteUnusedLogo(
|
||||
avatarUrl: string | null | undefined,
|
||||
transaction?: Transaction | null
|
||||
) {
|
||||
const key = OAuthClient.logoKeyFromUrl(avatarUrl);
|
||||
if (!avatarUrl || !key) {
|
||||
return;
|
||||
}
|
||||
|
||||
const deleteFile = async () => {
|
||||
try {
|
||||
const inUse = await OAuthClient.unscoped().count({
|
||||
where: { avatarUrl },
|
||||
});
|
||||
if (inUse === 0) {
|
||||
await FileStorage.deleteFile(key);
|
||||
}
|
||||
} catch (err) {
|
||||
// A failed delete must not fail the change to the client.
|
||||
Logger.warn("Failed to delete OAuth client logo from storage", {
|
||||
key,
|
||||
error: errToString(err),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (transaction) {
|
||||
(transaction.parent || transaction).afterCommit(deleteFile);
|
||||
} else {
|
||||
await deleteFile();
|
||||
}
|
||||
public static logoKeyPrefix(clientId: string) {
|
||||
return `${Buckets.public}/oauth-clients/${clientId}/`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { subDays, subHours } from "date-fns";
|
||||
import { Scope } from "@shared/types";
|
||||
import { OAuthClient } from "@server/models";
|
||||
@@ -185,8 +186,10 @@ describe("CleanupDynamicOAuthClientsTask", () => {
|
||||
|
||||
it("should delete the logo of a deleted client", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const key = OAuthClient.logoKey("b".repeat(64), "png");
|
||||
const id = randomUUID();
|
||||
const key = OAuthClient.logoKey(id, "b".repeat(64), "png");
|
||||
await buildMetadataDocumentOAuthClient({
|
||||
id,
|
||||
avatarUrl: FileStorage.getUrlForKey(key),
|
||||
createdAt: subDays(new Date(), 3),
|
||||
});
|
||||
@@ -203,14 +206,14 @@ describe("CleanupDynamicOAuthClientsTask", () => {
|
||||
|
||||
it("should only run delete hooks for clients within the limit", async () => {
|
||||
const deleteFile = vi.spyOn(FileStorage, "deleteFile");
|
||||
const keys = ["c", "d"].map((char) =>
|
||||
OAuthClient.logoKey(char.repeat(64), "png")
|
||||
);
|
||||
const clients = await Promise.all(
|
||||
keys.map((key) =>
|
||||
[randomUUID(), randomUUID()].map((id) =>
|
||||
buildOAuthClient({
|
||||
id,
|
||||
createdById: null,
|
||||
avatarUrl: FileStorage.getUrlForKey(key),
|
||||
avatarUrl: FileStorage.getUrlForKey(
|
||||
OAuthClient.logoKey(id, "c".repeat(64), "png")
|
||||
),
|
||||
createdAt: subDays(new Date(), 3),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -45,7 +45,7 @@ describe("UploadOAuthClientLogoTask", () => {
|
||||
expect(client.avatarUrl).not.toContain("example.com");
|
||||
});
|
||||
|
||||
it("should share one file between clients with the same logo", async () => {
|
||||
it("should store a separate file for each client", async () => {
|
||||
const first = await buildMetadataDocumentOAuthClient();
|
||||
const second = await buildMetadataDocumentOAuthClient();
|
||||
const logoUrl = buildLogoUrl();
|
||||
@@ -56,7 +56,8 @@ describe("UploadOAuthClientLogoTask", () => {
|
||||
await task.perform({ oauthClientId: second.id, logoUrl });
|
||||
|
||||
await Promise.all([first.reload(), second.reload()]);
|
||||
expect(second.avatarUrl).toEqual(first.avatarUrl);
|
||||
expect(first.avatarUrl).toContain(first.id);
|
||||
expect(second.avatarUrl).toContain(second.id);
|
||||
});
|
||||
|
||||
it("should not store content that is not an image", async () => {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { errToString } from "@shared/utils/error";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { OAuthClient } from "@server/models";
|
||||
import { ImageHelper } from "@server/models/helpers/ImageHelper";
|
||||
@@ -27,23 +26,6 @@ export default class UploadOAuthClientLogoTask extends BaseTask<Props> {
|
||||
/** The maximum time to wait for a logo. */
|
||||
public static fetchTimeout = 5000;
|
||||
|
||||
/**
|
||||
* Schedule a logo upload. A failure is logged, not thrown, because the logo
|
||||
* is optional and must not fail the request that changed the client.
|
||||
*
|
||||
* @param props the task props.
|
||||
*/
|
||||
public static async scheduleOptional(props: Props) {
|
||||
try {
|
||||
await new UploadOAuthClientLogoTask().schedule(props);
|
||||
} catch (err) {
|
||||
Logger.warn("Failed to schedule OAuth client logo upload", {
|
||||
oauthClientId: props.oauthClientId,
|
||||
error: errToString(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async perform({ oauthClientId, logoUrl }: Props) {
|
||||
const client = await OAuthClient.findByPk(oauthClientId);
|
||||
if (!client) {
|
||||
@@ -75,22 +57,19 @@ export default class UploadOAuthClientLogoTask extends BaseTask<Props> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Content addressed, so clients that share a logo share one file.
|
||||
const hash = createHash("sha256").update(body).digest("hex");
|
||||
if (client.avatarUrl?.includes(hash)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const key = OAuthClient.logoKey(hash, imageType.extension);
|
||||
if (!(await FileStorage.getFileExists(key))) {
|
||||
await FileStorage.store({
|
||||
body,
|
||||
contentType: imageType.contentType,
|
||||
contentLength: body.byteLength,
|
||||
key,
|
||||
acl: "public-read",
|
||||
});
|
||||
}
|
||||
const key = OAuthClient.logoKey(client.id, hash, imageType.extension);
|
||||
await FileStorage.store({
|
||||
body,
|
||||
contentType: imageType.contentType,
|
||||
contentLength: body.byteLength,
|
||||
key,
|
||||
acl: "public-read",
|
||||
});
|
||||
|
||||
// Always the canonical URL, so the client hooks can find the key to delete.
|
||||
await client.update({ avatarUrl: FileStorage.getUrlForKey(key) });
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Job } from "bull";
|
||||
import { BaseTask } from "./BaseTask";
|
||||
|
||||
class TestTask extends BaseTask<{ id: string }> {
|
||||
public async perform() {
|
||||
// Not used in these tests
|
||||
}
|
||||
}
|
||||
|
||||
describe("BaseTask", () => {
|
||||
describe("scheduleOptional", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("should return the job when it is scheduled", async () => {
|
||||
const task = new TestTask();
|
||||
const job = { id: "job" } as Job;
|
||||
vi.spyOn(task, "schedule").mockResolvedValue(job);
|
||||
|
||||
await expect(task.scheduleOptional({ id: "1" })).resolves.toBe(job);
|
||||
});
|
||||
|
||||
it("should not throw when the job cannot be scheduled", async () => {
|
||||
const task = new TestTask();
|
||||
vi.spyOn(task, "schedule").mockRejectedValue(new Error("Queue error"));
|
||||
|
||||
await expect(task.scheduleOptional({ id: "1" })).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { Job, JobOptions } from "bull";
|
||||
import { errToString } from "@shared/utils/error";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { taskQueue } from "../../";
|
||||
|
||||
export enum TaskPriority {
|
||||
@@ -27,6 +29,28 @@ export abstract class BaseTask<T extends object> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule this task when its work is optional. A failure to add the job is
|
||||
* logged, not thrown, so that it does not fail the caller.
|
||||
*
|
||||
* @param props Properties to be used by the task
|
||||
* @param options Job options such as priority and retry strategy, as defined by Bull.
|
||||
* @returns A promise that resolves to the job, or undefined if it was not added
|
||||
*/
|
||||
public async scheduleOptional(
|
||||
props: T,
|
||||
options?: JobOptions
|
||||
): Promise<Job | undefined> {
|
||||
try {
|
||||
return await this.schedule(props, options);
|
||||
} catch (err) {
|
||||
Logger.warn(`Failed to schedule ${this.constructor.name}`, {
|
||||
error: errToString(err),
|
||||
});
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the task.
|
||||
*
|
||||
|
||||
@@ -291,7 +291,7 @@ describe("oauthClients.info", () => {
|
||||
expect(requests).toEqual(0);
|
||||
});
|
||||
|
||||
it("should return 404 when a metadata document cannot be fetched", async () => {
|
||||
it("should return a distinct error when a metadata document cannot be fetched", async () => {
|
||||
const user = await buildUser();
|
||||
const clientId = `https://${randomUUID()}.example.com/oauth/client.json`;
|
||||
msw.use(http.get(clientId, () => new HttpResponse(null, { status: 404 })));
|
||||
@@ -300,7 +300,10 @@ describe("oauthClients.info", () => {
|
||||
body: { clientId },
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(404);
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(422);
|
||||
expect(body.error).toEqual("invalid_client_metadata");
|
||||
expect(body.message).toContain("status 404");
|
||||
});
|
||||
|
||||
it("should validate redirectUri parameter", async () => {
|
||||
|
||||
@@ -244,12 +244,12 @@ router.post(
|
||||
// The logo is copied to file storage in the background, so the consent
|
||||
// screen never loads it from a host that the client controls.
|
||||
if (logo_uri) {
|
||||
ctx.state.transaction.afterCommit(() =>
|
||||
UploadOAuthClientLogoTask.scheduleOptional({
|
||||
ctx.state.transaction.afterCommit(async () => {
|
||||
await new UploadOAuthClientLogoTask().scheduleOptional({
|
||||
oauthClientId: client.id,
|
||||
logoUrl: logo_uri,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Logger.info("authentication", "OAuth client registered", {
|
||||
@@ -309,12 +309,12 @@ router.put(
|
||||
|
||||
// The stored logo is kept until the background task replaces it.
|
||||
if (logo_uri) {
|
||||
ctx.state.transaction.afterCommit(() =>
|
||||
UploadOAuthClientLogoTask.scheduleOptional({
|
||||
ctx.state.transaction.afterCommit(async () => {
|
||||
await new UploadOAuthClientLogoTask().scheduleOptional({
|
||||
oauthClientId: client.id,
|
||||
logoUrl: logo_uri,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ctx.body = presentDCRClient(team.url, client, {
|
||||
|
||||
@@ -58,7 +58,12 @@ export default function oauthErrorHandler() {
|
||||
let errorType = "server_error";
|
||||
if (status === 400) {
|
||||
errorType = "invalid_request";
|
||||
} else if (status === 401) {
|
||||
} else if (
|
||||
status === 401 ||
|
||||
(err instanceof Error &&
|
||||
"id" in err &&
|
||||
err.id === "invalid_client_metadata")
|
||||
) {
|
||||
errorType = "invalid_client";
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,10 @@ describe("#oauth.register", () => {
|
||||
});
|
||||
|
||||
it("should include client_uri and upload logo_uri in the background", async () => {
|
||||
const schedule = vi.spyOn(UploadOAuthClientLogoTask.prototype, "schedule");
|
||||
const schedule = vi.spyOn(
|
||||
UploadOAuthClientLogoTask.prototype,
|
||||
"scheduleOptional"
|
||||
);
|
||||
const res = await server.post("/oauth/register", {
|
||||
body: {
|
||||
client_name: "Branded Client",
|
||||
@@ -885,6 +888,23 @@ describe("client ID metadata documents", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should return a distinct error when the metadata document cannot be loaded", async () => {
|
||||
const subdomain = buildSubdomain();
|
||||
const team = await buildTeam({ subdomain });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const clientId = `https://${randomUUID()}.example.com/oauth/client.json`;
|
||||
msw.use(
|
||||
http.get(clientId, () => new HttpResponse("error", { status: 503 }))
|
||||
);
|
||||
|
||||
const res = await authorize(user, subdomain, clientId);
|
||||
expect(res.status).toEqual(422);
|
||||
expect(res.headers.get("location")).toBeNull();
|
||||
const body = await res.json();
|
||||
expect(body.error).toEqual("invalid_client");
|
||||
expect(body.error_description).toContain("status 503");
|
||||
});
|
||||
|
||||
it("should not authorize a metadata document client when MCP is disabled", async () => {
|
||||
const subdomain = buildSubdomain();
|
||||
const team = await buildTeam({
|
||||
@@ -1009,15 +1029,4 @@ describe("client ID metadata documents", () => {
|
||||
expect(res.status).toEqual(403);
|
||||
expect(requests).toEqual(0);
|
||||
});
|
||||
|
||||
it("should not authorize when the document cannot be fetched", async () => {
|
||||
const subdomain = buildSubdomain();
|
||||
const team = await buildTeam({ subdomain });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const clientId = `https://${randomUUID()}.example.com/oauth/client.json`;
|
||||
msw.use(http.get(clientId, () => new HttpResponse(null, { status: 404 })));
|
||||
|
||||
const res = await authorize(user, subdomain, clientId);
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
});
|
||||
@@ -305,6 +305,14 @@ describe("ClientMetadataHelper", () => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
const expectInvalidMetadata = (clientId: string, message?: string) =>
|
||||
expect(
|
||||
ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).rejects.toMatchObject({
|
||||
id: "invalid_client_metadata",
|
||||
...(message && { message: expect.stringContaining(message) }),
|
||||
});
|
||||
|
||||
it("should not fetch when the team has turned MCP off", async () => {
|
||||
const clientId = buildClientId();
|
||||
const mock = mockDocument(clientId, buildDocument(clientId));
|
||||
@@ -415,7 +423,7 @@ describe("ClientMetadataHelper", () => {
|
||||
it("should upload the logo in the background", async () => {
|
||||
const schedule = vi.spyOn(
|
||||
UploadOAuthClientLogoTask.prototype,
|
||||
"schedule"
|
||||
"scheduleOptional"
|
||||
);
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, buildDocument(clientId));
|
||||
@@ -470,12 +478,7 @@ describe("ClientMetadataHelper", () => {
|
||||
});
|
||||
mockDocument(cached.clientId, "error", { status: 500 });
|
||||
|
||||
const client = await ClientMetadataHelper.findOrFetchByClientId(
|
||||
cached.clientId,
|
||||
team
|
||||
);
|
||||
|
||||
expect(client).toBeNull();
|
||||
await expectInvalidMetadata(cached.clientId, "status 500");
|
||||
const reloaded = await OAuthClient.findByPk(cached.id);
|
||||
expect(reloaded?.name).toEqual(cached.name);
|
||||
expect(reloaded?.metadataExpiresAt).toEqual(cached.metadataExpiresAt);
|
||||
@@ -510,9 +513,7 @@ describe("ClientMetadataHelper", () => {
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, buildDocument(clientId), { status });
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
});
|
||||
|
||||
it("should not follow redirects", async () => {
|
||||
@@ -530,9 +531,7 @@ describe("ClientMetadataHelper", () => {
|
||||
)
|
||||
);
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId, "status 302");
|
||||
expect(mock.requests()).toEqual(0);
|
||||
});
|
||||
|
||||
@@ -545,36 +544,28 @@ describe("ClientMetadataHelper", () => {
|
||||
})
|
||||
);
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId, "could not be fetched");
|
||||
});
|
||||
|
||||
it("should reject invalid JSON", async () => {
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, "{ not json");
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId, "not valid JSON");
|
||||
});
|
||||
|
||||
it("should reject a client_id that does not match the URL", async () => {
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, buildDocument(buildClientId()));
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId, "does not match its URL");
|
||||
});
|
||||
|
||||
it("should reject a document with a client_secret", async () => {
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, buildDocument(clientId, { client_secret: "abc" }));
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
});
|
||||
|
||||
it.each(["client_secret_post", "client_secret_basic", "private_key_jwt"])(
|
||||
@@ -586,9 +577,7 @@ describe("ClientMetadataHelper", () => {
|
||||
buildDocument(clientId, { token_endpoint_auth_method: method })
|
||||
);
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -603,9 +592,7 @@ describe("ClientMetadataHelper", () => {
|
||||
})
|
||||
);
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
expect(await OAuthClient.findByClientId(clientId)).toBeNull();
|
||||
});
|
||||
|
||||
@@ -627,9 +614,7 @@ describe("ClientMetadataHelper", () => {
|
||||
const clientId = buildClientId();
|
||||
mockDocument(clientId, buildDocument(clientId, { redirect_uris: [] }));
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
});
|
||||
|
||||
it("should reject an http redirect URI", async () => {
|
||||
@@ -641,9 +626,7 @@ describe("ClientMetadataHelper", () => {
|
||||
})
|
||||
);
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId);
|
||||
});
|
||||
|
||||
it("should reject a document that is not JSON", async () => {
|
||||
@@ -652,9 +635,7 @@ describe("ClientMetadataHelper", () => {
|
||||
headers: { "Content-Type": "text/plain" },
|
||||
});
|
||||
|
||||
expect(
|
||||
await ClientMetadataHelper.findOrFetchByClientId(clientId, team)
|
||||
).toBeNull();
|
||||
await expectInvalidMetadata(clientId, "must be JSON");
|
||||
});
|
||||
|
||||
it("should accept a structured JSON content type", async () => {
|
||||
|
||||
@@ -8,10 +8,11 @@ import { Day, Minute, Second } from "@shared/utils/time";
|
||||
import { getHostname } from "@shared/utils/urls";
|
||||
import { OAuthClientValidation } from "@shared/validations";
|
||||
import env from "@server/env";
|
||||
import { AuthorizationError } from "@server/errors";
|
||||
import { AuthorizationError, InvalidClientMetadataError } from "@server/errors";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { OAuthClient, Share, Team } from "@server/models";
|
||||
import UploadOAuthClientLogoTask from "@server/queues/tasks/UploadOAuthClientLogoTask";
|
||||
import type { Response } from "@server/utils/fetch";
|
||||
import fetch from "@server/utils/fetch";
|
||||
import { OAuthHelper } from "./OAuthHelper";
|
||||
|
||||
@@ -91,9 +92,12 @@ export class ClientMetadataHelper {
|
||||
*
|
||||
* @param clientId The public client identifier.
|
||||
* @param team The team of the user that requests the client.
|
||||
* @returns The OAuth client, or null if it is not found or not valid.
|
||||
* @returns The OAuth client, or null if it is not found or the client_id is
|
||||
* not a valid metadata document URL.
|
||||
* @throws AuthorizationError if the client is a metadata document and the
|
||||
* team has turned MCP off.
|
||||
* @throws InvalidClientMetadataError if the metadata document cannot be
|
||||
* fetched or is not valid.
|
||||
*/
|
||||
public static async findOrFetchByClientId(
|
||||
clientId: string,
|
||||
@@ -130,30 +134,21 @@ export class ClientMetadataHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
let client: OAuthClient | null;
|
||||
let logoUrl: string | undefined;
|
||||
try {
|
||||
const { document, expiresAt } =
|
||||
await ClientMetadataHelper.fetchMetadataDocument(clientId);
|
||||
client = await ClientMetadataHelper.saveClient(
|
||||
clientId,
|
||||
existing,
|
||||
document,
|
||||
expiresAt
|
||||
);
|
||||
logoUrl = document.logo_uri;
|
||||
} catch (err) {
|
||||
// An existing client is kept so that issued tokens continue to work,
|
||||
// but it cannot be authorized again until a fetch succeeds.
|
||||
Logger.warn("Failed to load OAuth client metadata document", {
|
||||
clientId,
|
||||
error: errToString(err),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
// An existing client is kept when the fetch fails, so that issued tokens
|
||||
// continue to work, but it cannot be authorized again until a fetch
|
||||
// succeeds.
|
||||
const { document, expiresAt } =
|
||||
await ClientMetadataHelper.fetchMetadataDocument(clientId);
|
||||
const client = await ClientMetadataHelper.saveClient(
|
||||
clientId,
|
||||
existing,
|
||||
document,
|
||||
expiresAt
|
||||
);
|
||||
const logoUrl = document.logo_uri;
|
||||
|
||||
if (client && logoUrl) {
|
||||
await UploadOAuthClientLogoTask.scheduleOptional({
|
||||
await new UploadOAuthClientLogoTask().scheduleOptional({
|
||||
oauthClientId: client.id,
|
||||
logoUrl,
|
||||
});
|
||||
@@ -260,25 +255,42 @@ export class ClientMetadataHelper {
|
||||
*
|
||||
* @param clientId The metadata document URL.
|
||||
* @returns The validated document and the time the cached copy expires.
|
||||
* @throws An error if the document cannot be fetched or is not valid.
|
||||
* @throws InvalidClientMetadataError if the document cannot be fetched or
|
||||
* is not valid.
|
||||
*/
|
||||
public static async fetchMetadataDocument(clientId: string): Promise<{
|
||||
document: MetadataDocument;
|
||||
expiresAt: Date;
|
||||
}> {
|
||||
const res = await fetch(clientId, {
|
||||
method: "GET",
|
||||
redirect: "manual",
|
||||
// Special-use addresses are never allowed, even when the installation
|
||||
// allows them for other integrations (draft section 8.6).
|
||||
allowIPAddressList: [],
|
||||
size: ClientMetadataHelper.maxDocumentSize,
|
||||
signal: AbortSignal.timeout(ClientMetadataHelper.fetchTimeout),
|
||||
headers: { Accept: "application/json" },
|
||||
});
|
||||
let res: Response;
|
||||
let text: string;
|
||||
try {
|
||||
res = await fetch(clientId, {
|
||||
method: "GET",
|
||||
redirect: "manual",
|
||||
// Special-use addresses are never allowed, even when the installation
|
||||
// allows them for other integrations (draft section 8.6).
|
||||
allowIPAddressList: [],
|
||||
size: ClientMetadataHelper.maxDocumentSize,
|
||||
signal: AbortSignal.timeout(ClientMetadataHelper.fetchTimeout),
|
||||
headers: { Accept: "application/json" },
|
||||
});
|
||||
text = await res.text();
|
||||
} catch (err) {
|
||||
// Network errors can describe internal hosts, so they are only logged.
|
||||
Logger.warn("Failed to fetch OAuth client metadata document", {
|
||||
clientId,
|
||||
error: errToString(err),
|
||||
});
|
||||
throw InvalidClientMetadataError(
|
||||
"The client metadata document could not be fetched"
|
||||
);
|
||||
}
|
||||
|
||||
if (res.status !== 200) {
|
||||
throw new Error(`Unexpected response status ${res.status}`);
|
||||
throw InvalidClientMetadataError(
|
||||
`The client metadata document responded with status ${res.status}`
|
||||
);
|
||||
}
|
||||
|
||||
const contentType = res.headers.get("content-type")?.split(";")[0].trim();
|
||||
@@ -286,12 +298,33 @@ export class ClientMetadataHelper {
|
||||
!contentType ||
|
||||
!/^application\/(?:[\w.-]+\+)?json$/i.test(contentType)
|
||||
) {
|
||||
throw new Error(`Unexpected content type ${contentType}`);
|
||||
throw InvalidClientMetadataError(
|
||||
`The client metadata document must be JSON, not ${contentType ?? "an unknown type"}`
|
||||
);
|
||||
}
|
||||
|
||||
const document = MetadataDocumentSchema.parse(JSON.parse(await res.text()));
|
||||
let json: unknown;
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch {
|
||||
throw InvalidClientMetadataError(
|
||||
"The client metadata document is not valid JSON"
|
||||
);
|
||||
}
|
||||
|
||||
const result = MetadataDocumentSchema.safeParse(json);
|
||||
if (!result.success) {
|
||||
const issue = result.error.issues[0];
|
||||
throw InvalidClientMetadataError(
|
||||
`The client metadata document is invalid: ${issue.path.join(".") || "document"} ${issue.message}`
|
||||
);
|
||||
}
|
||||
|
||||
const document = result.data;
|
||||
if (document.client_id !== clientId) {
|
||||
throw new Error("client_id does not match the document URL");
|
||||
throw InvalidClientMetadataError(
|
||||
"The client_id in the client metadata document does not match its URL"
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1160,6 +1160,7 @@
|
||||
"An error occurred": "An error occurred",
|
||||
"The OAuth client could not be found, please check the provided client ID": "The OAuth client could not be found, please check the provided client ID",
|
||||
"The OAuth client could not be loaded, please check the redirect URI is valid": "The OAuth client could not be loaded, please check the redirect URI is valid",
|
||||
"The OAuth client could not be loaded from its metadata document": "The OAuth client could not be loaded from its metadata document",
|
||||
"The OAuth client could not be loaded, please check your workspace subdomain is correct": "The OAuth client could not be loaded, please check your workspace subdomain is correct",
|
||||
"Required OAuth parameters are missing": "Required OAuth parameters are missing",
|
||||
"Authorize": "Authorize",
|
||||
|
||||
Reference in new issue
Block a user