Skip to content

Commit

Permalink
feat: svg as a background image does not execute JS code
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Oct 18, 2023
1 parent 10eddd0 commit 8313fb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion css/inspect/inspect.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ section sp-tabs sp-tab-panel {
margin-bottom: 30px;
}

.inspect sp-asset svg {
.inspect sp-asset .svg-placeholder {
width: 300px;
height: 300px;
margin-bottom: 40px;
background-repeat: no-repeat;
}
3 changes: 3 additions & 0 deletions inspect.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h3>Page preview</h3>
<sp-action-button for="inspect-select-logo" data-picker="img" disabled>
<sp-icon-anchor-select></sp-icon-anchor-select>
</sp-action-button>
<sp-help-text variant="negative" icon class="hidden" id="svg-security-risk">
Security risk: the logo you have selected is an SVG. It may contain malicious code (like Javascript code). You can download it but you should inspect it before using it.
</sp-help-text>
<sp-asset id="inspect-select-logo" class="hidden">
<div></div>
</sp-asset>
Expand Down
10 changes: 8 additions & 2 deletions js/inspect/inspect.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const VARS_FIELDS = document.querySelectorAll(`${VARS_PARENT_SELECTOR} sp-textfi
const PICKERS = document.querySelectorAll(`${VARS_PARENT_SELECTOR} sp-action-button`);
const LOGO_FIELD = document.querySelector(`${PARENT_SELECTOR} #inspect-select-logo`);
const LOGO_IMG_PLACEHOLDER = document.querySelector(`${PARENT_SELECTOR} #inspect-select-logo div`);
const LOGO_SVG_SECURITY_MSG = document.querySelector(`${PARENT_SELECTOR} #svg-security-risk`);

const config = {
vars: {},
Expand Down Expand Up @@ -127,7 +128,7 @@ const saveCapture = (event) => {
pickerField.handleChange();
} catch (e) {
// eslint-disable-next-line no-console
console.warning(`Error while trying to capture style: ${e.message}`);
console.warn(`Error while trying to capture style: ${e.message}`);
}
}
event.preventDefault();
Expand Down Expand Up @@ -244,8 +245,13 @@ const attachListeners = () => {
if (value && value !== 'none') {
LOGO_IMG_PLACEHOLDER.innerHTML = '';
if (value.startsWith('<svg')) {
LOGO_IMG_PLACEHOLDER.innerHTML = value;
LOGO_SVG_SECURITY_MSG.classList.remove('hidden');
const div = document.createElement('div');
div.style['background-image'] = `url('data:image/svg+xml;base64,${btoa(value)}')`;
div.classList.add('svg-placeholder');
LOGO_IMG_PLACEHOLDER.appendChild(div);
} else {
LOGO_SVG_SECURITY_MSG.classList.add('hidden');
const img = document.createElement('img');
img.src = value;
LOGO_IMG_PLACEHOLDER.appendChild(img);
Expand Down

0 comments on commit 8313fb4

Please sign in to comment.