mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 17:47:50 +08:00
fix(reading): isolate malformed extension catalogs
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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\)/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user