Skip to content

Commit 802893c

Browse files
authored
Update server.js
1 parent 31459f8 commit 802893c

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/npm/server.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,47 @@ res.send(`
9999
100100
<div class="comment-section">
101101
<h3>Comments</h3>
102-
${wiki.comments.slice(-7).map(comment => {
103-
return `
102+
const htmlOutput = wiki.comments.slice(-7).map(comment => {
103+
return `
104104
<div class="comment" id="comment-${comment.id}">
105-
<div class="comment-author">${comment.author} <small>(${new Date(comment.createdAt).toLocaleString()})</small></div>
106-
<div class="comment-content">${comment.content}</div>
105+
<div class="comment-author">
106+
${comment.author} <small>(${new Date(comment.createdAt).toLocaleString()})</small>
107+
</div>
108+
<div class="comment-content">${escapeHtml(comment.content)}</div>
107109
${comment.replies.length > 0 ? comment.replies.map(reply => {
108110
return `
109111
<div class="comment-reply">
110-
<strong>${reply.author}</strong>: ${reply.content} <small>(${new Date(reply.createdAt).toLocaleString()})</small>
112+
<strong>${reply.author}</strong>: ${escapeHtml(reply.content)}
113+
<small>(${new Date(reply.createdAt).toLocaleString()})</small>
111114
</div>
112115
`;
113116
}).join('') : ''}
114117
</div>
115118
`;
119+
}).join('');
120+
121+
// Use the escapeHtml function to safely escape user input
122+
function escapeHtml(str) {
123+
return str.replace(/[&<>"'`]/g, function(match) {
124+
const escape = {
125+
'&': '&amp;',
126+
'<': '&lt;',
127+
'>': '&gt;',
128+
'"': '&quot;',
129+
"'": '&#039;',
130+
"`": '&#x60;'
131+
};
132+
return escape[match];
133+
});
134+
}
135+
136+
// Output the HTML
137+
console.log(htmlOutput);
138+
139+
`;
140+
}).join('') : ''}
141+
</div>
142+
`;
116143
}).join('')})
117144
</div>
118145
`;

0 commit comments

Comments
 (0)