Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(reading): isolate malformed extension catalogs

Bingxi Zhao (Frank)37f56de042ea
diff --git a/web/lib/reading-api.ts b/web/lib/reading-api.ts
index 87388b2d5..a52c35f83 100644
--- a/web/lib/reading-api.ts
+++ b/web/lib/reading-api.ts
@@ -215,9 +215,17 @@ export async function getUnitText(
export async function listReadingExtensions(): Promise<
ReadingExtensionManifest[]
> {
- return unwrap(
+ const payload: unknown = await unwrap(
await apiFetch(apiUrl(`${BASE}/extensions`), { cache: "no-store" }),
);
+ if (!Array.isArray(payload)) return [];
+ return payload.filter(
+ (row): row is ReadingExtensionManifest =>
+ Boolean(row) &&
+ typeof row === "object" &&
+ typeof (row as ReadingExtensionManifest).id === "string" &&
+ Array.isArray((row as ReadingExtensionManifest).actions),
+ );
}
export async function runReadingExtension(
diff --git a/web/tests/e2e/reading-w3c-annotations.audit.ts b/web/tests/e2e/reading-w3c-annotations.audit.ts
index ef7eab633..fb2525a4f 100644
--- a/web/tests/e2e/reading-w3c-annotations.audit.ts
+++ b/web/tests/e2e/reading-w3c-annotations.audit.ts
@@ -50,6 +50,9 @@ test.beforeEach(async ({ page }) => {
});
}
if (path === "/api/v1/settings/ui") return json({ language: "en" });
+ if (path === "/api/v1/dashboard/suggestions") {
+ return json({ suggestions: [], stale: false });
+ }
if (path === "/api/v1/settings/llm-options") {
return json({
active: { profile_id: "p", model_id: "m" },
@@ -69,6 +72,7 @@ test.beforeEach(async ({ page }) => {
if (path === "/api/v1/reading/supported-formats") {
return json({ extensions: [".txt"], max_bytes: 1024, raw_view_extensions: [] });
}
+ if (path === "/api/v1/reading/extensions") return json([]);
if (path === "/api/v1/reading/materials") return json([material]);
if (path === "/api/v1/reading/materials/w3c-material") {
return json(material);
diff --git a/web/tests/reading-extensions.test.ts b/web/tests/reading-extensions.test.ts
index 6b61a584e..c653fa595 100644
--- a/web/tests/reading-extensions.test.ts
+++ b/web/tests/reading-extensions.test.ts
@@ -30,3 +30,8 @@ test("the browser sends a locator and selection, not trusted visible text", () =
assert.match(api, /runReadingExtension/);
assert.doesNotMatch(api, /visible_text\?: string/);
});
+
+test("a malformed extension catalog cannot crash the whole reader", () => {
+ assert.match(api, /if \(!Array\.isArray\(payload\)\) return \[\]/);
+ assert.match(api, /Array\.isArray\(\(row as ReadingExtensionManifest\)\.actions\)/);
+});