Skip to content

Commit

Permalink
fix(xss): fixed double escaping (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 authored Nov 5, 2020
1 parent aad59b0 commit 9d37f70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/SemanticsEnforcer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sanitizeHtml from 'sanitize-html';
import { escape } from 'html-escaper';
import { escape, unescape } from 'html-escaper';

import { ContentScanner } from './ContentScanner';
import LibraryManager from './LibraryManager';
Expand Down Expand Up @@ -242,7 +242,9 @@ export default class SemanticsEnforcer {
} else {
log.debug('Filtering out all HTML tags');
// Escape all HTML tags if the field doesn't allow HTML in general.
newText = escape(newText);
// We avoid double escaping like & becoming & by
// unescaping first.
newText = escape(unescape(newText));
}

// Check if string has the required length
Expand Down
8 changes: 8 additions & 0 deletions test/SemanticsEnforcer.text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ describe('SemanticsEnforcer', () => {
await compare('Hello world!', 'Hello world!');
});

it("doesn't escape already escaped text.", async () => {
await compare('Hello & world!', 'Hello & world!');
});

it('escapes dangerous characters in simple text', async () => {
await compare('Hello & world!', 'Hello & world!');
});

it("strips html tags from text that doesn't allow html", async () => {
await compare(
'<script>alert("Danger!");</script>Hello world!',
Expand Down

0 comments on commit 9d37f70

Please sign in to comment.