-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
s.teleshev
committed
Feb 27, 2024
1 parent
e27d5d1
commit 00864bd
Showing
5 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { sanitizeHtml } from './sanitizeHtml'; | ||
|
||
describe('sanitize', () => { | ||
it('should sanitize input', () => { | ||
const input = '<script>alert("some unsafe action")</script>'; | ||
const output = sanitizeHtml(input); | ||
expect(output).toBe(''); | ||
}); | ||
|
||
it('should sanitize input and keep safe tags', () => { | ||
const input = '<div>qwe</div><script>alert("some unsafe action")</script><div>asd</div>'; | ||
const output = sanitizeHtml(input); | ||
expect(output).toBe('<div>qwe</div><div>asd</div>'); | ||
}); | ||
|
||
it('should not sanitize safe input', () => { | ||
const input = 'Hello, world!'; | ||
const output = sanitizeHtml(input); | ||
expect(output).toBe(input); | ||
}); | ||
|
||
it('should sanitize unsafe input', () => { | ||
const input = '<img src="x" onerror="alert(1)">'; | ||
const output = sanitizeHtml(input); | ||
expect(output).toBe('<img src="x">'); | ||
}); | ||
|
||
it('should sanitize unsafe input with attributes', () => { | ||
const input = '<a href="javascript:alert(1)">click me</a>'; | ||
const output = sanitizeHtml(input); | ||
expect(output).toBe('<a>click me</a>'); | ||
}); | ||
|
||
it('should sanitize unsafe input with attributes', () => {}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import DOMPurify from 'dompurify'; | ||
|
||
export function sanitizeHtml<T extends string | HTMLElement>(dirty: T): T extends string ? string : HTMLElement { | ||
const purify = DOMPurify(window); | ||
|
||
return purify.sanitize(dirty, {}) as T extends string ? string : HTMLElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters