Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Fix breaking schema.org with special chars

Chocobozzz797445268ba0
diff --git a/packages/core-utils/src/renderer/html.ts b/packages/core-utils/src/renderer/html.ts
index b2c1d2bac1..6a72547cd5 100644
--- a/packages/core-utils/src/renderer/html.ts
+++ b/packages/core-utils/src/renderer/html.ts
@@ -85,3 +85,14 @@ export function escapeAttribute (value: string) {
return String(value).replace(/"/g, '"')
}
+
+export function escapeHtmlJSONStr (jsonStr: string) {
+ if (!jsonStr) return ''
+
+ return jsonStr
+ .replace(/</g, '\\u003c')
+ .replace(/>/g, '\\u003e')
+ .replace(/&/g, '\\u0026')
+ .replace(/\u2028/g, '\\u2028')
+ .replace(/\u2029/g, '\\u2029')
+}
diff --git a/packages/tests/src/client/head-tags.ts b/packages/tests/src/client/head-tags.ts
index 95d9d58ccb..fe2657924f 100644
--- a/packages/tests/src/client/head-tags.ts
+++ b/packages/tests/src/client/head-tags.ts
@@ -268,18 +268,6 @@ describe('Test <head> HTML tags', function () {
})
})
- describe('Escaping', function () {
- it('Should correctly escape values', async function () {
- await servers[0].users.updateMe({ description: '<strong>"super description"</strong>' })
-
- const res = await makeGetRequest({ url: servers[0].url, path: '/a/root', accept: 'text/html', expectedStatus: HttpStatusCode.OK_200 })
- const text = res.text
-
- expect(text).to.contain(`<meta property="twitter:description" content="&quot;super description&quot;" />`)
- expect(text).to.contain(`<meta property="og:description" content="&quot;super description&quot;" />`)
- })
- })
-
describe('Mastodon link', function () {
async function check (path: string, href: string, exist = true) {
const res = await makeGetRequest({ url: servers[0].url, path, accept: 'text/html', expectedStatus: HttpStatusCode.OK_200 })
@@ -373,11 +361,18 @@ describe('Test <head> HTML tags', function () {
for (const id of playlistIds) {
await commonPageTest(path + id)
- const res = await makeGetRequest({ url: servers[0].url, path: path + id, accept: 'text/html', expectedStatus: HttpStatusCode.OK_200 })
+ const res = await makeGetRequest({
+ url: servers[0].url,
+ path: path + id,
+ accept: 'text/html',
+ expectedStatus: HttpStatusCode.OK_200
+ })
const text = res.text
const feedUrl = `${servers[0].url}/feeds/podcast/videos.xml?playlistId=${playlist.id}`
- expect(text).to.contain(`<link rel="alternate" type="application/rss+xml" title="${playlistName} - Podcast feed" href="${feedUrl}" />`)
+ expect(text).to.contain(
+ `<link rel="alternate" type="application/rss+xml" title="${playlistName} - Podcast feed" href="${feedUrl}" />`
+ )
}
}
})
@@ -395,6 +390,44 @@ describe('Test <head> HTML tags', function () {
})
})
+ describe('Escaping', function () {
+ it('Should correctly escape values', async function () {
+ {
+ await servers[0].users.updateMe({ description: '<strong>"super description"</strong>' })
+
+ const res = await makeGetRequest({
+ url: servers[0].url,
+ path: '/a/root',
+ accept: 'text/html',
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ const text = res.text
+
+ expect(text).to.contain(`<meta property="twitter:description" content="&quot;super description&quot;" />`)
+ expect(text).to.contain(`<meta property="og:description" content="&quot;super description&quot;" />`)
+ }
+
+ {
+ await servers[0].channels.update({
+ channelName: 'root_channel',
+ attributes: {
+ displayName: '</strong>toto'
+ }
+ })
+
+ const res = await makeGetRequest({
+ url: servers[0].url,
+ path: '/w/' + videoIds[0],
+ accept: 'text/html',
+ expectedStatus: HttpStatusCode.OK_200
+ })
+ const text = res.text
+
+ expect(text).to.contain('\\u003c/strong\\u003etoto')
+ }
+ })
+ })
+
after(async function () {
await cleanupTests(servers)
})
diff --git a/server/core/lib/html/shared/tags-html.ts b/server/core/lib/html/shared/tags-html.ts
index 19e6d8c80c..0013999a39 100644
--- a/server/core/lib/html/shared/tags-html.ts
+++ b/server/core/lib/html/shared/tags-html.ts
@@ -1,4 +1,4 @@
-import { escapeAttribute, escapeHTML } from '@peertube/peertube-core-utils'
+import { escapeAttribute, escapeHTML, escapeHtmlJSONStr } from '@peertube/peertube-core-utils'
import { mdToPlainText } from '@server/helpers/markdown.js'
import { getActivityStreamDuration } from '@server/lib/activitypub/activity.js'
import { ServerConfigManager } from '@server/lib/server-config-manager.js'
@@ -188,7 +188,7 @@ export class TagsHtml {
const schemaTags = await this.generateSchemaTagsOptions(tagsValues, context)
if (schemaTags) {
- tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
+ tagsStr += `<script type="application/ld+json">${escapeHtmlJSONStr(JSON.stringify(schemaTags))}</script>`
}
// Rel Me