mirror of
https://github.com/hugo-fixit/FixIt.git
synced 2026-08-24 15:28:57 +00:00
fix: add HTMLEscape function to fix XSS attack in search feature (#687)
This commit is contained in:
+2
-2
@@ -282,7 +282,7 @@ class FixIt {
|
|||||||
const results = {};
|
const results = {};
|
||||||
window._index.search(query).forEach(({ item, refIndex, matches }) => {
|
window._index.search(query).forEach(({ item, refIndex, matches }) => {
|
||||||
let title = item.title;
|
let title = item.title;
|
||||||
let content = item.content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
let content = item.content;
|
||||||
matches.forEach(({ indices, value, key }) => {
|
matches.forEach(({ indices, value, key }) => {
|
||||||
if (key === 'content') {
|
if (key === 'content') {
|
||||||
content = this._applyHighlightToText(content, indices, highlightTag);
|
content = this._applyHighlightToText(content, indices, highlightTag);
|
||||||
@@ -343,7 +343,7 @@ class FixIt {
|
|||||||
templates: {
|
templates: {
|
||||||
suggestion: ({ title, uri, date, context }) =>
|
suggestion: ({ title, uri, date, context }) =>
|
||||||
`<div><a href="${uri}"><span class="suggestion-title">${title}</span></a><span class="suggestion-date">${date}</span></div><div class="suggestion-context">${context}</div>`,
|
`<div><a href="${uri}"><span class="suggestion-title">${title}</span></a><span class="suggestion-date">${date}</span></div><div class="suggestion-context">${context}</div>`,
|
||||||
empty: ({ query }) => `<div class="search-empty">${searchConfig.noResultsFound}: <span class="search-query">"${query}"</span></div>`,
|
empty: ({ query }) => `<div class="search-empty">${searchConfig.noResultsFound}: <span class="search-query">"${this.util.HTMLEscape(query)}"</span></div>`,
|
||||||
footer: ({ }) => {
|
footer: ({ }) => {
|
||||||
let searchType, icon, href;
|
let searchType, icon, href;
|
||||||
switch (searchConfig.type) {
|
switch (searchConfig.type) {
|
||||||
|
|||||||
@@ -133,4 +133,14 @@ export default class Util {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HTMLEscape(str) {
|
||||||
|
return str.replace(/[&<>"']/g, char => ({
|
||||||
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'"': '"',
|
||||||
|
"'": '''
|
||||||
|
})[char]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,8 +55,8 @@
|
|||||||
{{- $anchor = replace (index (split $contentm ">") 0) `"` "" -}}
|
{{- $anchor = replace (index (split $contentm ">") 0) `"` "" -}}
|
||||||
{{- $contentm = printf "<h6 id=%v" $contentm -}}
|
{{- $contentm = printf "<h6 id=%v" $contentm -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{/* Plainify, unescape and remove (\n, \t) */}}
|
{{/* Plainify and remove (\n, \t) */}}
|
||||||
{{- $contentj = $contentj | plainify | htmlUnescape | replaceRE `[\n\t ]+` " " -}}
|
{{- $contentj = $contentj | plainify | replaceRE `[\n\t ]+` " " -}}
|
||||||
{{- if gt $.Site.Params.search.contentLength 0 -}}
|
{{- if gt $.Site.Params.search.contentLength 0 -}}
|
||||||
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
|
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
@@ -80,8 +80,8 @@
|
|||||||
{{- if gt $j 0 -}}
|
{{- if gt $j 0 -}}
|
||||||
{{- $contentj = printf "<h3 id=%v" $contentj -}}
|
{{- $contentj = printf "<h3 id=%v" $contentj -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{/* Plainify, unescape and remove (\n, \t) */}}
|
{{/* Plainify and remove (\n, \t) */}}
|
||||||
{{- $contentj = $contentj | plainify | htmlUnescape | replaceRE `[\n\t ]+` " " -}}
|
{{- $contentj = $contentj | plainify | replaceRE `[\n\t ]+` " " -}}
|
||||||
{{- if gt $.Site.Params.search.contentLength 0 -}}
|
{{- if gt $.Site.Params.search.contentLength 0 -}}
|
||||||
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
|
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|||||||
Reference in New Issue
Block a user