Skip to content

Commit 03a37cb

Browse files
authored
Merge pull request #98 from numbersprotocol/copilot/fix-xss-vulnerabilities-wix-examples
Fix XSS vulnerabilities in Wix integration examples
2 parents 3de30a1 + 0904419 commit 03a37cb

3 files changed

Lines changed: 94 additions & 49 deletions

File tree

examples/wix/capture-eye-element.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ class CaptureEyeElement extends HTMLElement {
22
connectedCallback() {
33
const nid = this.getAttribute('nid');
44
const thumbnail = this.getAttribute('thumbnail');
5-
this.innerHTML = `<capture-eye nid="${nid}"><media-viewer src="${thumbnail}"/></capture-eye>`;
5+
// Clear existing content to prevent duplicates on re-connection
6+
this.replaceChildren();
7+
const captureEye = document.createElement('capture-eye');
8+
captureEye.setAttribute('nid', nid);
9+
const mediaViewer = document.createElement('media-viewer');
10+
mediaViewer.setAttribute('src', thumbnail);
11+
captureEye.appendChild(mediaViewer);
12+
this.appendChild(captureEye);
613
}
714
}
815
customElements.define('capture-eye-element', CaptureEyeElement);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
function updateElement(elem) {
2+
const _nid = elem.getAttribute('nid');
3+
const nid = `${_nid.slice(0, 5)}...${_nid.slice(-5)}`;
4+
const thumbnail = elem.getAttribute('thumbnail');
5+
const creator = elem.getAttribute('creator') || 'N/A';
6+
const headline = elem.getAttribute('headline');
7+
const _captureUpdatedDate = new Date(elem.getAttribute('captureUpdatedDate'));
8+
// Format the date to "Jul 25 2024 15:05:45" in GMT+8 timezone
9+
const options = {
10+
year: 'numeric',
11+
month: 'short',
12+
day: 'numeric',
13+
hour: '2-digit',
14+
minute: '2-digit',
15+
second: '2-digit',
16+
hour12: false,
17+
timeZone: 'Asia/Taipei',
18+
};
19+
const captureUpdatedDate = _captureUpdatedDate.toLocaleString(
20+
'en-US',
21+
options
22+
);
23+
24+
// Clear existing content safely
25+
elem.replaceChildren();
26+
27+
// Build DOM safely without innerHTML to prevent XSS
28+
const captureEye = document.createElement('capture-eye');
29+
captureEye.setAttribute('nid', _nid);
30+
31+
const container = document.createElement('div');
32+
container.className = 'container';
33+
34+
const img = document.createElement('img');
35+
img.setAttribute('src', thumbnail);
36+
img.setAttribute('alt', 'Image');
37+
img.className = 'image';
38+
39+
const textContainer = document.createElement('div');
40+
textContainer.className = 'text-container';
41+
42+
const createParagraph = (className, text) => {
43+
const p = document.createElement('p');
44+
p.className = className;
45+
p.textContent = text;
46+
return p;
47+
};
48+
49+
textContainer.appendChild(createParagraph('title', '註冊時間'));
50+
textContainer.appendChild(createParagraph('description', captureUpdatedDate));
51+
textContainer.appendChild(createParagraph('title', '創建者'));
52+
textContainer.appendChild(createParagraph('description', creator));
53+
textContainer.appendChild(createParagraph('title', '簡介'));
54+
textContainer.appendChild(createParagraph('description', headline));
55+
textContainer.appendChild(createParagraph('title', '影像 Nid'));
56+
57+
const nidP = createParagraph('nid', nid);
58+
nidP.style.cursor = 'pointer';
59+
nidP.addEventListener('click', () => {
60+
window.open(
61+
`https://asset.captureapp.xyz/${encodeURIComponent(_nid)}`,
62+
'_blank',
63+
'noopener'
64+
);
65+
});
66+
textContainer.appendChild(nidP);
67+
68+
container.appendChild(img);
69+
container.appendChild(textContainer);
70+
captureEye.appendChild(container);
71+
elem.appendChild(captureEye);
72+
}
73+
74+
class CaptureEyeStyleElement extends HTMLElement {
75+
static observedAttributes = ['creator'];
76+
77+
connectedCallback() {
78+
updateElement(this);
79+
}
80+
81+
attributeChangedCallback(name, oldValue, newValue) {
82+
updateElement(this);
83+
}
84+
}
85+
86+
customElements.define('capture-eye-style-element', CaptureEyeStyleElement);

examples/wix/capture-eye-sytle-element.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)