-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Showing
4 changed files
with
112 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @fileoverview Sanitizer module in order to prevent XSS attacks. | ||
* @author NHN FE Development Lab <[email protected]> | ||
*/ | ||
'use strict'; | ||
|
||
var DOMPurify = require('dompurify'); | ||
|
||
// For temporarily saving original target value | ||
var TEMP_TARGET_ATTRIBUTE = 'data-target-temp'; | ||
|
||
/** | ||
* Add DOMPurify hook to handling exceptional rules for certain HTML attributes. | ||
* Should be set when the calendar instance is created. | ||
*/ | ||
function addAttributeHooks() { | ||
DOMPurify.addHook('beforeSanitizeAttributes', function(node) { | ||
var targetValue; | ||
// Preserve default target attribute value | ||
if (node.tagName === 'A') { | ||
targetValue = node.getAttribute('target'); | ||
|
||
if (targetValue) { | ||
node.setAttribute(TEMP_TARGET_ATTRIBUTE, targetValue); | ||
} else { | ||
// set default value | ||
node.setAttribute('target', '_self'); | ||
} | ||
} | ||
}); | ||
|
||
DOMPurify.addHook('afterSanitizeAttributes', function(node) { | ||
if (node.tagName === 'A' && node.hasAttribute(TEMP_TARGET_ATTRIBUTE)) { | ||
node.setAttribute('target', node.getAttribute(TEMP_TARGET_ATTRIBUTE)); | ||
node.removeAttribute(TEMP_TARGET_ATTRIBUTE); | ||
|
||
// Additionally set `rel="noopener"` to prevent another security issue. | ||
if (node.getAttribute('target') === '_blank') { | ||
node.setAttribute('rel', 'noopener'); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Remove all attribute sanitizing hooks. | ||
* Use it in `Calendar#destroy`. | ||
*/ | ||
function removeAttributeHooks() { | ||
DOMPurify.removeAllHooks(); | ||
} | ||
|
||
/** | ||
* Prevent XSS attack by sanitizing input string values via DOMPurify | ||
* @param {string} str target string value | ||
* @returns {string} sanitized string | ||
*/ | ||
function sanitize(str) { | ||
return DOMPurify.sanitize(str); | ||
} | ||
|
||
module.exports = { | ||
sanitize: sanitize, | ||
addAttributeHooks: addAttributeHooks, | ||
removeAttributeHooks: removeAttributeHooks | ||
}; |
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,39 @@ | ||
'use strict'; | ||
|
||
var sanitizer = require('../../../src/js/common/sanitizer'); | ||
|
||
describe('common/sanitizer', function() { | ||
beforeEach(function() { | ||
sanitizer.addAttributeHooks(); | ||
}); | ||
|
||
afterEach(function() { | ||
sanitizer.removeAttributeHooks(); | ||
}); | ||
|
||
it("should leave target='_blank' attribute and add 'rel=noopener'", function() { | ||
var targetStr = '<a href="https://example.com" target="_blank">A link to open in a new tab</a>'; | ||
var expected = '<a href="https://example.com" target="_blank" rel="noopener">A link to open in a new tab</a>'; | ||
|
||
var result = sanitizer.sanitize(targetStr); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
|
||
it('should preserve other target attribute values', function() { | ||
var targetStr = '<a href="https://example.com" target="_self"></a>'; | ||
|
||
var result = sanitizer.sanitize(targetStr); | ||
|
||
expect(result).toBe(targetStr); | ||
}); | ||
|
||
it('should allow a target attribute for anchor tags only', function() { | ||
var targetStr = '<form href="https://example.com" target="_blank"></form>'; | ||
var expected = '<form href="https://example.com"></form>'; | ||
|
||
var result = sanitizer.sanitize(targetStr); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
}); |