The minimalistic document archiving platform.
refactor(server): replace sanitize-html with built-in HTML escaping (#1061)
Corentin Thomasset93c369d3e57d
diff --git a/.changeset/dark-dolls-judge.md b/.changeset/dark-dolls-judge.mdnew file mode 100644index 00000000..5c195cf2--- /dev/null+++ b/.changeset/dark-dolls-judge.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Html-ish characters in email templates are now escaped instead of sanitized-outdiff --git a/apps/papra-server/package.json b/apps/papra-server/package.jsonindex ca8769f4..9aa86f80 100644--- a/apps/papra-server/package.json+++ b/apps/papra-server/package.json@@ -80,7 +80,6 @@"picomatch": "^4.0.2","posthog-node": "^5.20.0","resend": "^4.6.0",- "sanitize-html": "^2.17.0","stripe": "^17.7.0","tsx": "catalog:","valibot": "catalog:"@@ -98,7 +97,6 @@"@types/node-cron": "^3.0.11","@types/nodemailer": "^6.4.17","@types/picomatch": "^4.0.0",- "@types/sanitize-html": "^2.16.0","@vitest/coverage-v8": "catalog:","esbuild": "^0.24.2","eslint": "catalog:",diff --git a/apps/papra-server/src/modules/app/auth/auth.emails.services.ts b/apps/papra-server/src/modules/app/auth/auth.emails.services.tsindex 9ca70f90..ebce5ad1 100644--- a/apps/papra-server/src/modules/app/auth/auth.emails.services.ts+++ b/apps/papra-server/src/modules/app/auth/auth.emails.services.ts@@ -1,7 +1,7 @@import type { User } from 'better-auth';import type { EmailsServices } from '../../emails/emails.services';import { injectArguments } from '@corentinth/chisels';-import { sanitize } from '../../shared/html/html';+import { escapeHtml } from '../../shared/html/html';import { createLogger } from '../../shared/logger/logger';const logger = createLogger({ namespace: 'auth.emails.services' });@@ -25,7 +25,7 @@ export async function sendVerificationEmail({ user, url, emailsServices }: { useto: user.email,subject: 'Verify your email',html: `-<p>Hello ${sanitize(user.name)},</p>+<p>Hello ${escapeHtml(user.name)},</p><p>Verify your email by clicking <a href="${url}">here</a></p><p>Or by pasting the following link into your browser: ${url}</p><p style="color: #888;">Best regards,</p>@@ -41,7 +41,7 @@ export async function sendPasswordResetEmail({ user, url, emailsServices }: { usto: user.email,subject: 'Reset your password',html: `-<p>Hello ${sanitize(user.name)},</p>+<p>Hello ${escapeHtml(user.name)},</p><p>Reset your password by clicking <a href="${url}">here</a></p><p>Or by pasting the following link into your browser: ${url}</p><p style="color: #888;">Best regards,</p>diff --git a/apps/papra-server/src/modules/organizations/organizations.usecases.ts b/apps/papra-server/src/modules/organizations/organizations.usecases.tsindex 8e4f27ca..0ec705cb 100644--- a/apps/papra-server/src/modules/organizations/organizations.usecases.ts+++ b/apps/papra-server/src/modules/organizations/organizations.usecases.ts@@ -14,7 +14,7 @@ import { createForbiddenError } from '../app/auth/auth.errors';import { getClientBaseUrl } from '../config/config.models';import { getOrganizationPlan } from '../plans/plans.usecases';import { addDays } from '../shared/date';-import { sanitize } from '../shared/html/html';+import { escapeHtml } from '../shared/html/html';import { createLogger } from '../shared/logger/logger';import { isDefined } from '../shared/utils';import { doesSubscriptionBlockDeletion } from '../subscriptions/subscriptions.models';@@ -313,7 +313,7 @@ export async function sendOrganizationInvitationEmail({path: '/invitations',});- const organizationName = sanitize(organization.name);+ const organizationName = escapeHtml(organization.name);await emailsServices.sendEmail({to: email,diff --git a/apps/papra-server/src/modules/shared/html/html.test.ts b/apps/papra-server/src/modules/shared/html/html.test.tsindex a8e28848..32d3b5b6 100644--- a/apps/papra-server/src/modules/shared/html/html.test.ts+++ b/apps/papra-server/src/modules/shared/html/html.test.ts@@ -1,13 +1,15 @@import { describe, expect, test } from 'vitest';-import { sanitize } from './html';+import { escapeHtml } from './html';describe('html', () => {- describe('sanitizeHtml', () => {- test('all html tags and attributes should be removed from the provided string', () => {- expect(sanitize('<p>Hello <b>world</b>!</p>')).toBe('Hello world!');- expect(sanitize('<a href="https://example.com">Link</a>')).toBe('Link');- expect(sanitize('<img src="https://example.com/image.png" alt="Image" />')).toBe('');- expect(sanitize('<script>alert("Hello");</script>')).toBe('');+ describe('escapeHtml', () => {+ test('replace &, <, >, ", and \' with their corresponding HTML entities', () => {+ expect(escapeHtml('<p>Hello & "world"\'s test!</p>')).toBe('<p>Hello & "world"'s test!</p>');+ expect(escapeHtml('&<>"\'')).toBe('&<>"'');+ expect(escapeHtml('<div>')).toBe('&lt;div&gt;');++ expect(escapeHtml('No special characters')).toBe('No special characters');+ expect(escapeHtml('')).toBe('');});});});diff --git a/apps/papra-server/src/modules/shared/html/html.ts b/apps/papra-server/src/modules/shared/html/html.tsindex 946d4f6e..34ecbb65 100644--- a/apps/papra-server/src/modules/shared/html/html.ts+++ b/apps/papra-server/src/modules/shared/html/html.ts@@ -1,5 +1,12 @@-import sanitizeHtml from 'sanitize-html';--export function sanitize(html: string) {- return sanitizeHtml(html, { allowedTags: [], allowedAttributes: {} });+export function escapeHtml(html: string) {+ return html.replace(/[&<>"']/g, (match) => {+ switch (match) {+ case '&': return '&';+ case '<': return '<';+ case '>': return '>';+ case '"': return '"';+ case '\'': return ''';+ default: return match;+ }+ });}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 90820597..4a84cb90 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -557,9 +557,6 @@ importers:resend:specifier: ^4.6.0- sanitize-html:- specifier: ^2.17.0- version: 2.17.0stripe:specifier: ^17.7.0version: 17.7.0@@ -606,9 +603,6 @@ importers:'@types/picomatch':specifier: ^4.0.0version: 4.0.0- '@types/sanitize-html':- specifier: ^2.16.0- version: 2.16.0'@vitest/coverage-v8':specifier: 'catalog:'version: 4.0.3([email protected](@types/[email protected])(@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))@@ -5095,9 +5089,6 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-Qec1E3mhALmaspIrhWt9jkQMNdw6bReVu64mjvhbhq2NFPftLPVr+l1SZgmw/66WwBNpDh7ao5AT6gF5v41PFA==}- '@types/[email protected]':- resolution: {integrity: sha512-l6rX1MUXje5ztPT0cAFtUayXF06DqPhRyfVXareEN5gGCFaP/iwsxIyKODr9XDhfxPpN6vXUFNfo5kZMXCxBtw==}-'@types/[email protected]':resolution: {integrity: sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==}@@ -8386,10 +8377,6 @@ packages:resolution: {integrity: sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==}engines: {node: '>=12'}- resolution: {integrity: sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==}- engines: {node: '>=0.10.0'}-resolution: {integrity: sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==}@@ -9777,9 +9764,6 @@ packages:resolution: {integrity: sha512-Nt/a5SfCLiTnQAjx3fHlqp8hRgTL3z7kTQZzvIMS9uCAepnCyjpdEc6M/sz69WqMBdaDBw9sF1F1UaHROYzGkQ==}engines: {node: '>=10'}- resolution: {integrity: sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==}-resolution: {integrity: sha512-HlsyYdMBnbPQ9Jr/VgJ1YF4scnldvJpJxCVx6KgqPL4dxppsWrJHCIIxQXMJrqGnsRkNPATbeMJ8Yxu7JMsYcA==}@@ -10530,9 +10514,6 @@ packages:resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}- resolution: {integrity: sha512-dLAADUSS8rBwhaevT12yCezvioCA+bmUTPH/u57xKPT8d++voeYE6HeluA/bPbQ15TwDBG2ii+QZIEmYx8VdxA==}-resolution: {integrity: sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==}@@ -17346,10 +17327,6 @@ snapshots:dependencies:csstype: 3.2.3- '@types/[email protected]':- dependencies:- htmlparser2: 8.0.2-'@types/[email protected]':dependencies:'@types/node': 24.0.10@@ -21428,8 +21405,6 @@ snapshots:- [email protected]: {}-optional: true@@ -23385,8 +23360,6 @@ snapshots:dependencies:pngjs: 3.4.0- [email protected]: {}-@@ -24328,15 +24301,6 @@ snapshots:- dependencies:- deepmerge: 4.3.1- escape-string-regexp: 4.0.0- htmlparser2: 8.0.2- is-plain-object: 5.0.0- parse-srcset: 1.0.2- postcss: 8.5.6-