Skip to content

Commit a51c705

Browse files
authored
Merge pull request #323 from alphagov/fix-xss
Fix XSS vulnerability on search results page
2 parents 7c29396 + 60e6e2d commit a51c705

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
This change solves a potential security issue with HTML snippets. Pages indexed in search results have their entire contents indexed, including any HTML code snippets. These HTML snippets would appear in the search results unsanitised, making it possible to render arbitrary HTML or run arbitrary scripts.
6+
7+
You can see more detail about this issue at [#323: Fix XSS vulnerability on search results page](https://github.com/alphagov/tech-docs-gem/pull/323)
8+
59
## 3.3.0
610

711
### New features

lib/assets/javascripts/_modules/search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@
169169

170170
this.processContent = function processContent (content, query) {
171171
var output
172-
content = '<div>' + content + '</div>'
173-
content = $(content).mark(query)
172+
var sanitizedContent = $('<div></div>').text(content).html()
173+
content = $('<div>' + sanitizedContent + '</div>').mark(query)
174174

175175
// Split content by sentence.
176176
var sentences = content.html().replace(/(\.+|:|!|\?|\r|\n)("*|'*|\)*|}*|]*)/gm, '|').split('|')

spec/javascripts/search-spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,11 @@ describe('Search', function () {
9999
var expectedResults = ' … This is <mark data-markjs="true">test</mark> sentence one … This is <mark data-markjs="true">test</mark> sentence two … This is <mark data-markjs="true">test</mark> sentence three … This is <mark data-markjs="true">test</mark> sentence four … This is <mark data-markjs="true">test</mark> sentence five … '
100100
expect(processedContent).toEqual(expectedResults)
101101
})
102+
103+
it('sanitises HTML in the search results', function () {
104+
processedContent = module.processContent('It will render multiple `<input>` `<script>alert("uhoh")</script>` and its accompanying suggestions and `aria-live` region.', 'multi region')
105+
var expectedResults = ' … It will render <mark data-markjs="true">multi</mark>ple `&lt;input&gt;` `&lt;script&gt;alert("uhoh")&lt;/script&gt;` and its accompanying suggestions and `aria-live` <mark data-markjs="true">region</mark> … '
106+
expect(processedContent).toEqual(expectedResults)
107+
})
102108
})
103109
})

0 commit comments

Comments
 (0)