Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Encode unsafe characters as HTML entities #1698

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib/serializers/Html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ class HtmlSerializer implements Serializer {
return `<DL><p>\n${this._serializeFolder(folder, '')}</DL><p>\n`
}

_htmlentities_encode(string) {
return string.replace(/[\u00A0-\u9999<>&"]/g, char => '&#' + char.charCodeAt(0) + ';')
}

_serializeFolder(folder, indent) {
return folder.children
.map(child => {
if (child instanceof Bookmark) {
return (
`${indent}<DT><A HREF="${child.url}" TAGS="${''}" ID="${child.id}">${child.title}</A>\n`
`${indent}<DT><A HREF="${this._htmlentities_encode(child.url)}" TAGS="${''}" ID="${child.id}">${this._htmlentities_encode(child.title)}</A>\n`
)
} else if (child instanceof Folder) {
const nextIndent = indent + ' '
return (
`${indent}<DT><H3 ID="${child.id}">${child.title}</H3>\n` +
`${indent}<DT><H3 ID="${child.id}">${this._htmlentities_encode(child.title)}</H3>\n` +
`${indent}<DL><p>\n${this._serializeFolder(
child,
nextIndent
Expand Down
4 changes: 2 additions & 2 deletions src/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ describe('Floccus', function() {
parentId: localRoot
})
const barFolder = await browser.bookmarks.create({
title: "bar=?*'Ä_:-^;",
title: "bar=?*'Ä_:-^;<script>",
parentId: fooFolder.id
})
const bookmark = await browser.bookmarks.create({
Expand All @@ -1129,7 +1129,7 @@ describe('Floccus', function() {
title: 'foo!"§$%&/()=?"',
children: [
new Folder({
title: "bar=?*'Ä_:-^;",
title: "bar=?*'Ä_:-^;<script>",
children: [
new Bookmark({
title: 'url|!"=)/§_:;Ä\'*ü"',
Expand Down
Loading