-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #525 from metrico/fix/security_issues_4_9
fix: Incomplete HTML attribute sanitization
- Loading branch information
Showing
7 changed files
with
115 additions
and
63 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ server | |
.env | ||
*.cookie | ||
packages/main/.env | ||
codeql-results | ||
codeqldb |
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
107 changes: 107 additions & 0 deletions
107
packages/main/src/components/DataViews/components/Traces/Jaeger-ui/src/utils/jsonMarkup.ts
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,107 @@ | ||
// this is a fixed version of the json-markup npm package | ||
'use strict' | ||
|
||
const INDENTATION = ' ' | ||
|
||
function createInlineStyle(rules: Record<string, string> | undefined): string { | ||
let inlineStyle = '' | ||
rules && Object.keys(rules).forEach((property) => { | ||
inlineStyle += `${property}:${rules[property]};` | ||
}) | ||
return inlineStyle | ||
} | ||
|
||
function createStyleApplier(styleFile: Record<string, Record<string, string>> | undefined) { | ||
function applyClass(cssClassName: string): string { | ||
return `class="${cssClassName}"` | ||
} | ||
|
||
function applyInlineStyle(cssClassName: string): string { | ||
return `style="${createInlineStyle(styleFile?.['.' + cssClassName])}"` | ||
} | ||
|
||
return styleFile ? applyInlineStyle : applyClass | ||
} | ||
|
||
function determineType(value: any): string { | ||
if (value === null) return 'null' | ||
if (Array.isArray(value)) return 'array' | ||
if (typeof value === 'string' && /^https?:/.test(value)) return 'link' | ||
if (typeof value === 'object' && typeof value.toISOString === 'function') return 'date' | ||
|
||
return typeof value | ||
} | ||
|
||
function escapeHtml(unsafeString: string): string { | ||
return unsafeString | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, ''') | ||
} | ||
|
||
export default function renderJsonMarkup( | ||
jsonObject: any, | ||
styleFile?: Record<string, Record<string, string>> | undefined | ||
): string { | ||
let currentIndentation = '' | ||
const applyStyle = createStyleApplier(styleFile) | ||
|
||
function renderList( | ||
items: any[], | ||
startDelimiter: string, | ||
endDelimiter: string, | ||
renderItem: (item: any) => string | ||
): string { | ||
if (!items.length) return `${startDelimiter} ${endDelimiter}` | ||
|
||
let result = startDelimiter + '\n' | ||
|
||
currentIndentation += INDENTATION | ||
items.forEach((item, index) => { | ||
result += currentIndentation + renderItem(item) + (index < items.length - 1 ? ',' : '') + '\n' | ||
}) | ||
currentIndentation = currentIndentation.slice(0, -INDENTATION.length) | ||
|
||
return result + currentIndentation + endDelimiter | ||
} | ||
|
||
function renderValue(value: any): string { | ||
if (value === undefined) return '' | ||
|
||
switch (determineType(value)) { | ||
case 'boolean': | ||
return `<span ${applyStyle('json-markup-bool')}>${value}</span>` | ||
|
||
case 'number': | ||
return `<span ${applyStyle('json-markup-number')}>${value}</span>` | ||
|
||
case 'date': | ||
return `<span class="json-markup-string">"${escapeHtml(value.toISOString())}"</span>` | ||
|
||
case 'null': | ||
return `<span ${applyStyle('json-markup-null')}>null</span>` | ||
|
||
case 'string': | ||
return `<span ${applyStyle('json-markup-string')}>"${escapeHtml(value.replace(/\n/g, '\n' + currentIndentation))}"</span>` | ||
|
||
case 'link': | ||
return `<span ${applyStyle('json-markup-string')}>"<a href="${escapeHtml(value)}">${escapeHtml(value)}</a>"</span>` | ||
|
||
case 'array': | ||
return renderList(value, '[', ']', renderValue) | ||
|
||
case 'object': | ||
const keys = Object.keys(value).filter((key) => value[key] !== undefined) | ||
|
||
return renderList(keys, '{', '}', (key) => | ||
`<span ${applyStyle('json-markup-key')}>"${escapeHtml(key)}":</span> ${renderValue(value[key])}` | ||
) | ||
} | ||
|
||
return '' | ||
} | ||
|
||
return `<div ${applyStyle('json-markup')}>${renderValue(jsonObject)}</div>` | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.