Skip to content

Commit

Permalink
fix: escaping all html/xml characters
Browse files Browse the repository at this point in the history
  • Loading branch information
technikhil314 committed Dec 2, 2021
1 parent abf0166 commit 66fb4d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ export function urlDecode(_encoded: string): string {
const encoded = undoUrlSafeBase64(_encoded)
return globalThis.atob(encoded)
}

export function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;')
}
10 changes: 7 additions & 3 deletions pages/diff.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

<script>
import pako from 'pako'
import { undoUrlSafeBase64 } from '../helpers/utils'
import { undoUrlSafeBase64, escapeHtml } from '../helpers/utils'
export default {
layout: 'main',
data() {
Expand All @@ -107,7 +107,9 @@ export default {
if (hunkState === -1 || hunkState === 0) {
const className =
hunkState === -1 ? 'isModified bg-red-300 dark:bg-yellow-900' : ''
return `<span class="break-all inline p-0 m-0 ${className}">${item[1]}</span>`
return `<span class="break-all inline p-0 m-0 ${className}">${escapeHtml(
item[1]
)}</span>`
}
return false
})
Expand All @@ -120,7 +122,9 @@ export default {
if (hunkState === 1 || hunkState === 0) {
const className =
hunkState === 1 ? 'isModified bg-green-300 dark:bg-green-900' : ''
return `<span class="break-all inline p-0 m-0 ${className}">${item[1]}</span>`
return `<span class="break-all inline p-0 m-0 ${className}">${escapeHtml(
item[1]
)}</span>`
}
return false
})
Expand Down

0 comments on commit 66fb4d8

Please sign in to comment.