Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Hide raw error details on 404 pages (#5663)

Ashish Vermafac728e02953
diff --git a/frontend/src/components/VFourOhFour.vue b/frontend/src/components/VFourOhFour.vue
index ad81e2aa0..9dbd20ef8 100644
--- a/frontend/src/components/VFourOhFour.vue
+++ b/frontend/src/components/VFourOhFour.vue
@@ -65,7 +65,6 @@ useHead({
<h1 v-else class="mb-6 heading-5 lg:mb-10 lg:heading-2">
{{ $t("404.title") }}
</h1>
- <p class="sr-only">{{ error }}</p>
<p v-if="!isRateLimited" class="label-bold lg:heading-6">
<i18n-t scope="global" keypath="404.main" tag="span">
<template #link>
diff --git a/frontend/test/playwright/e2e/audio-detail.spec.ts b/frontend/test/playwright/e2e/audio-detail.spec.ts
index aa72e0390..b1a18b0df 100644
--- a/frontend/test/playwright/e2e/audio-detail.spec.ts
+++ b/frontend/test/playwright/e2e/audio-detail.spec.ts
@@ -103,6 +103,8 @@ test("sends AUDIO_INTERACTION event on seek", async ({ page, context }) => {
test("shows the 404 error page when no valid id", async ({ page }) => {
await page.goto("audio/foo")
await expect(errorPageHeading(page)).toBeVisible()
+ await expect(page.locator("main p.sr-only")).toHaveCount(0)
+ await expect(page.locator("main")).not.toContainText(/"statusCode"|"stack"/)
})
test("shows the 404 error page when no id", async ({ page }) => {
diff --git a/frontend/test/playwright/e2e/image-detail.spec.ts b/frontend/test/playwright/e2e/image-detail.spec.ts
index dcaf30d81..88f57b18a 100644
--- a/frontend/test/playwright/e2e/image-detail.spec.ts
+++ b/frontend/test/playwright/e2e/image-detail.spec.ts
@@ -73,6 +73,35 @@ test("shows the 404 error page when no id", async ({ page }) => {
await expect(errorPageLocator(page)).toBeVisible()
})
+test("shows a 404 without raw error details for a missing image on SSR", async ({
+ page,
+}) => {
+ const response = await page.goto("image/da5cb478-c093-4d62-b721-cda18797e3fc")
+
+ expect(response?.status()).toBe(404)
+ await expect(errorPageLocator(page)).toBeVisible()
+ await expect(page.locator("main p.sr-only")).toHaveCount(0)
+ await expect(page.locator("main")).not.toContainText(/"statusCode"|"stack"/)
+})
+
+test("shows a 404 without raw error details for a missing image on CSR", async ({
+ page,
+}) => {
+ const message = "No image with this diagnostic ID was found."
+ // Without the `proxy_requests` flag, the browser calls the API directly.
+ await page.route(/\/(v1\/images|api\/image)\//, (route) =>
+ route.fulfill({ status: 404, json: { detail: message } })
+ )
+
+ await page.goto("/")
+ await page.locator("a.home-cell").first().click()
+
+ await expect(errorPageLocator(page)).toBeVisible()
+ await expect(page.locator("main p.sr-only")).toHaveCount(0)
+ await expect(page.locator("main")).not.toContainText(message)
+ await expect(page.locator("main")).not.toContainText(/"statusCode"|"stack"/)
+})
+
test("sends GET_MEDIA event on CTA button click", async ({ context, page }) => {
const analyticsEvents = collectAnalyticsEvents(context)
diff --git a/frontend/test/unit/specs/components/v-four-oh-four.spec.js b/frontend/test/unit/specs/components/v-four-oh-four.spec.js
new file mode 100644
index 000000000..615df9edd
--- /dev/null
+++ b/frontend/test/unit/specs/components/v-four-oh-four.spec.js
@@ -0,0 +1,36 @@
+import { defineComponent, h } from "vue"
+
+import { screen, within } from "@testing-library/vue"
+import { describe, expect, it } from "vitest"
+import { render } from "~~/test/unit/test-utils/render"
+
+import VFourOhFour from "~/components/VFourOhFour.vue"
+
+describe("VFourOhFour", () => {
+ it("renders recovery content without exposing raw error details", async () => {
+ const error = {
+ url: "/image/00000000-0000-0000-0000-000000000000",
+ statusCode: 404,
+ statusMessage: "Not Found",
+ message: "No image with this diagnostic ID was found.",
+ stack: "",
+ }
+
+ await render(
+ defineComponent({
+ setup: () => () => h(VFourOhFour, { error }),
+ })
+ )
+
+ const main = screen.getByRole("main")
+ expect(within(main).getByRole("heading", { level: 1 })).toHaveTextContent(
+ /The content.*seems to have disappeared\./
+ )
+ expect(
+ within(main).getByRole("link", { name: "Openverse" })
+ ).toHaveAttribute("href", "/")
+ expect(main.textContent).not.toContain(error.message)
+ expect(main.textContent).not.toContain('"statusCode"')
+ expect(main.textContent).not.toContain('"stack"')
+ })
+})