Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:notifications): harden HTML email template escaping

johavenb9909b33b8db
diff --git a/backend/package.json b/backend/package.json
index 59026ae4..b8005dbf 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -58,6 +58,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.15.1",
"deepmerge": "^4.3.1",
+ "entities": "^4.5.0",
"drizzle-kit": "^0.31.10",
"drizzle-orm": "^0.45.2",
"fast-xml-parser": "^5.8.0",
diff --git a/backend/src/applications/notifications/mails/models.ts b/backend/src/applications/notifications/mails/models.ts
index 0480522a..a838df56 100644
--- a/backend/src/applications/notifications/mails/models.ts
+++ b/backend/src/applications/notifications/mails/models.ts
@@ -1,3 +1,4 @@
+import { escapeUTF8 } from 'entities'
import { ACTION } from '../../../common/constants'
import { i18nLocale } from '../../../common/i18n'
import { capitalizeString, SERVER_NAME } from '../../../common/shared'
@@ -27,7 +28,7 @@ export function commentMail(
const content = `${mailAuthor(options.author)}${mailEventOnElement(tr.event, notification.element)}${mailItalicContent(options.content)}`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${urlFromSpaceFile(options.currentUrl, notification)}">${SERVER_NAME}</a><br>${tr.footer}<br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(urlFromSpaceFile(options.currentUrl, notification))}">${SERVER_NAME}</a><br>${tr.footer}<br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -51,7 +52,7 @@ export function spaceMail(
const content = `${mailEventOnElement(tr.event, notification.element)}`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${spaceUrl}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(spaceUrl)}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -77,9 +78,9 @@ export function spaceRootMail(
const spaceRootUrl =
options.action === ACTION.ADD ? urlFromSpaceFile(options.currentUrl, notification) : urlFromSpace(options.currentUrl, spaceName)
- const content = `${mailAuthor(options.author)}${mailEventOnElement(tr.event, notification.element)}&nbsp;${tr.originEvent}&nbsp;<b>${spaceName}</b>`
+ const content = `${mailAuthor(options.author)}${mailEventOnElement(tr.event, notification.element)}&nbsp;${tr.originEvent}&nbsp;<b>${escapeUTF8(spaceName)}</b>`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${spaceRootUrl}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(spaceRootUrl)}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(spaceName)}`, mailTemplate(content, footer)]
}
@@ -102,7 +103,7 @@ export function shareMail(
const content = `${options.author ? mailAuthor(options.author) : ''}${mailEventOnElement(tr.event, notification.element)}`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${urlFromSpaceFile(options.currentUrl, notification)}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(urlFromSpaceFile(options.currentUrl, notification))}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -128,10 +129,10 @@ export function linkMail(
let content = `${options.author ? mailAuthor(options.author) : ''}${mailEventOnElement(tr.event, notification.element)}`
if (options.linkPassword) {
- content += `<br><br>${tr.passwordText}:&nbsp;<div style="border:1px solid #000; padding:8px; display:inline-block;">${options.linkPassword}</div>`
+ content += `<br><br>${tr.passwordText}:&nbsp;<div style="border:1px solid #000; padding:8px; display:inline-block;">${escapeUTF8(options.linkPassword)}</div>`
}
- const footer = `<br>${tr.urlText}&nbsp;<a href="${urlFromLink(options.currentUrl, options.linkUUID)}">${SERVER_NAME}</a>`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(urlFromLink(options.currentUrl, options.linkUUID))}">${SERVER_NAME}</a>`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -155,7 +156,7 @@ export function syncMail(
const content = `${mailEventOnElement(tr.event, notification.element)}`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${syncUrl}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(syncUrl)}">${SERVER_NAME}</a><br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -170,7 +171,7 @@ export function auth2FaMail(language: i18nLocale, notification: NotificationCont
browser: 'Browser'
})
- const content = `${tr.event}<br><br>${tr.addressIp}:&nbsp;${notification.url}<br>${tr.browser}:&nbsp;${notification.element}`
+ const content = `${escapeUTF8(tr.event)}<br><br>${tr.addressIp}:&nbsp;${escapeUTF8(notification.url)}<br>${tr.browser}:&nbsp;${escapeUTF8(notification.element)}`
const footer = `<br>${tr.footer}<br>`
@@ -186,7 +187,7 @@ export function authLockedMail(language: i18nLocale, notification: NotificationC
addressIp: 'Address IP'
})
- const content = `${tr.event}<br><br>${tr.addressIp}:&nbsp;${notification.url}<br>`
+ const content = `${escapeUTF8(tr.event)}<br><br>${tr.addressIp}:&nbsp;${escapeUTF8(notification.url)}<br>`
const footer = `<br>${tr.footer}<br>`
@@ -211,7 +212,7 @@ export function requestUnlockMail(
const content = `${options.author ? mailAuthor(options.author) : ''}${mailEventOnElement(tr.event, notification.element)}`
- const footer = `<br>${tr.urlText}&nbsp;<a href="${urlFromSpaceFile(options.currentUrl, notification)}">${SERVER_NAME}</a><br>${tr.footer}<br>${tr.defaultFooter}`
+ const footer = `<br>${tr.urlText}&nbsp;<a href="${escapeUTF8(urlFromSpaceFile(options.currentUrl, notification))}">${SERVER_NAME}</a><br>${tr.footer}<br>${tr.defaultFooter}`
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
@@ -224,7 +225,7 @@ export function serverUpdateAvailableMail(language: i18nLocale, notification: No
event: notification.event
})
- const content = `${tr.event}:&nbsp;<b><a href="${notification.externalUrl}" target="_blank" rel="noopener">${notification.element}</a></b>`
+ const content = `${escapeUTF8(tr.event)}:&nbsp;<b><a href="${escapeUTF8(notification.externalUrl ?? '')}" target="_blank" rel="noopener">${escapeUTF8(notification.element)}</a></b>`
const footer = `<br>${tr.footer}<br>${tr.defaultFooter}`
diff --git a/backend/src/applications/notifications/mails/templates.spec.ts b/backend/src/applications/notifications/mails/templates.spec.ts
new file mode 100644
index 00000000..919072dd
--- /dev/null
+++ b/backend/src/applications/notifications/mails/templates.spec.ts
@@ -0,0 +1,48 @@
+import type { i18nLocale } from '../../../common/i18n'
+import { commentMail } from './models'
+import { mailAuthor, mailEventOnElement, mailItalicContent } from './templates'
+
+describe('mail templates escaping', () => {
+ const language: i18nLocale = 'fr'
+ const payload = `<img src=x onerror="alert('xss')">`
+ const escapedPayload = '&lt;img src=x onerror=&quot;alert(&apos;xss&apos;)&quot;&gt;'
+ const escapedUrlPayload = '%3Cimg%20src%3Dx%20onerror%3D%22alert(&apos;xss&apos;)%22%3E'
+
+ it('escapes author display name and avatar source', () => {
+ const html = mailAuthor({
+ fullName: payload,
+ avatarBase64: `data:image/svg+xml,"${payload}"`
+ } as any)
+
+ expect(html).toContain(`<b>${escapedPayload}</b>`)
+ expect(html).toContain('src="data:image/svg+xml,&quot;&lt;img src=x onerror=&quot;alert(&apos;xss&apos;)&quot;&gt;&quot;"')
+ expect(html).not.toContain('<img src=x onerror=')
+ })
+
+ it('escapes event element and italic content', () => {
+ expect(mailEventOnElement(payload, payload)).toContain(`${escapedPayload}:&nbsp;<b>${escapedPayload}</b>`)
+ expect(mailItalicContent(payload)).toBe(`<p><i>${escapedPayload}</i></p>`)
+ })
+
+ it('escapes comment mail content and encodes generated URL values', () => {
+ const [, html] = commentMail(
+ language,
+ {
+ app: 'comments',
+ event: payload,
+ element: `file ${payload}.txt`,
+ url: `space/${payload}`
+ } as any,
+ {
+ content: payload,
+ currentUrl: 'https://sync-in.test',
+ author: { fullName: payload, avatarBase64: 'avatar', login: 'attacker' } as any
+ }
+ )
+
+ expect(html).toContain(escapedPayload)
+ expect(html).toContain(`space/${escapedUrlPayload}`)
+ expect(html).toContain(`select=file%20${escapedUrlPayload}.txt`)
+ expect(html).not.toContain('<img src=x onerror=')
+ })
+})
diff --git a/backend/src/applications/notifications/mails/templates.ts b/backend/src/applications/notifications/mails/templates.ts
index bb38a408..2dd8b664 100644
--- a/backend/src/applications/notifications/mails/templates.ts
+++ b/backend/src/applications/notifications/mails/templates.ts
@@ -1,3 +1,4 @@
+import { escapeUTF8 } from 'entities'
import { capitalizeString } from '../../../common/shared'
import { UserModel } from '../../users/models/user.model'
@@ -18,8 +19,8 @@ ${footer}
</html>`
export const mailAuthor = (author: UserModel) =>
- `<img style="border-radius: 50% !important; vertical-align: middle; object-fit: cover;" height="40" width="40" src="${author.avatarBase64}" alt="avatar">&nbsp;<b>${author.fullName}</b>&nbsp;`
+ `<img style="border-radius: 50% !important; vertical-align: middle; object-fit: cover;" height="40" width="40" src="${escapeUTF8(author.avatarBase64 ?? '')}" alt="avatar">&nbsp;<b>${escapeUTF8(author.fullName)}</b>&nbsp;`
-export const mailEventOnElement = (event: string, element: string) => `${event}:&nbsp;<b>${capitalizeString(element)}</b>`
+export const mailEventOnElement = (event: string, element: string) => `${escapeUTF8(event)}:&nbsp;<b>${escapeUTF8(capitalizeString(element))}</b>`
-export const mailItalicContent = (content: string) => `<p><i>${content}</i></p>`
+export const mailItalicContent = (content: string) => `<p><i>${escapeUTF8(content)}</i></p>`
diff --git a/backend/src/applications/notifications/mails/urls.ts b/backend/src/applications/notifications/mails/urls.ts
index 25a5da92..2f1615f4 100644
--- a/backend/src/applications/notifications/mails/urls.ts
+++ b/backend/src/applications/notifications/mails/urls.ts
@@ -3,20 +3,27 @@ import { SPACES_BASE_ROUTE } from '../../spaces/constants/routes'
import { SYNC_BASE_ROUTE } from '../../sync/constants/routes'
import { NotificationContent } from '../interfaces/notification-properties.interface'
+function encodePath(path: string): string {
+ return path
+ .split('/')
+ .map((part) => encodeURIComponent(part))
+ .join('/')
+}
+
export function urlSpaceBase(url: string): string {
return url ? `${url}/#/${SPACES_BASE_ROUTE}` : ''
}
export function urlFromSpaceFile(url: string, notification: NotificationContent): string {
- return `${urlSpaceBase(url)}/${notification.url}?select=${notification.element}`
+ return `${urlSpaceBase(url)}/${encodePath(notification.url)}?select=${encodeURIComponent(notification.element)}`
}
export function urlFromLink(url: string, uuid: string): string {
- return `${url}/#/${PUBLIC_LINKS_ROUTE.LINK}/${uuid}`
+ return `${url}/#/${PUBLIC_LINKS_ROUTE.LINK}/${encodeURIComponent(uuid)}`
}
export function urlFromSpace(url: string, spaceName?: string) {
- return `${urlSpaceBase(url)}${spaceName ? `?select=${spaceName}` : ''}`
+ return `${urlSpaceBase(url)}${spaceName ? `?select=${encodeURIComponent(spaceName)}` : ''}`
}
export function urlFromSync(url: string): string {
diff --git a/package-lock.json b/package-lock.json
index 15ab46ce..5e1ac74b 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -67,6 +67,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.15.1",
"deepmerge": "^4.3.1",
+ "entities": "^4.5.0",
"drizzle-kit": "^0.31.10",
"drizzle-orm": "^0.45.2",
"fast-xml-parser": "^5.8.0",