Skip to content

Commit

Permalink
Adjust regex SAFE_URL_PATTERN for use with test method of regexes. (#…
Browse files Browse the repository at this point in the history
…33136)

The test method on regexes behaves different than the match method on strings in the presence of the global modifier.
Add a unit test for sanitizing the same template twice.

Co-authored-by: XhmikosR <[email protected]>
  • Loading branch information
nikonthethird and XhmikosR authored Feb 19, 2021
1 parent 454d8ae commit e8f08d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/src/util/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
*
* Shoutout to Angular 7 https://github.com/angular/angular/blob/7.2.4/packages/core/src/sanitization/url_sanitizer.ts
*/
const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file):|[^#&/:?]*(?:[#/?]|$))/gi
const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file):|[^#&/:?]*(?:[#/?]|$))/i

/**
* A pattern that matches safe data URLs. Only matches image, video and audio types.
Expand Down
10 changes: 10 additions & 0 deletions js/tests/unit/util/sanitizer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,15 @@ describe('Sanitizer', () => {
expect(result).toEqual(template)
expect(DOMParser.prototype.parseFromString).not.toHaveBeenCalled()
})

it('should allow multiple sanitation passes of the same template', () => {
const template = '<img src="test.jpg">'

const firstResult = sanitizeHtml(template, DefaultAllowlist, null)
const secondResult = sanitizeHtml(template, DefaultAllowlist, null)

expect(firstResult).toContain('src')
expect(secondResult).toContain('src')
})
})
})

0 comments on commit e8f08d1

Please sign in to comment.