Prevent stored XSS in rendered content
This commit is contained in:
1 parent
2e74b1505b
commit
f30a3dcacb
7 files changed
+195
-88
No files matched your search
+22
-22
@@ -20,11 +20,11 @@ function renderAdminReports() {
|
||||
const postTitle = r.post_title || '无标题';
|
||||
html += `
|
||||
<div class="report-item">
|
||||
<div><strong>${r.reporter_name || '匿名'}</strong> 举报了帖子</div>
|
||||
<div style="font-size:13px;color:#64748b;margin:4px 0;">原因:${r.reason}</div>
|
||||
<div style="font-size:13px;color:#64748b;margin:4px 0;">帖子:${postTitle} — ${(r.post_content || '').substring(0, 30)}${(r.post_content || '').length > 30 ? '...' : ''}</div>
|
||||
<div style="font-size:13px;font-weight:600;">状态:${statusMap[r.status] || r.status}</div>
|
||||
${r.handler_name ? `<div style="font-size:12px;color:#94a3b8;">处理人:${r.handler_name}${r.handler_note ? ' (' + r.handler_note + ')' : ''}</div>` : ''}
|
||||
<div><strong>${escapeHTML(r.reporter_name || '匿名')}</strong> 举报了帖子</div>
|
||||
<div style="font-size:13px;color:#64748b;margin:4px 0;">原因:${escapeHTML(r.reason)}</div>
|
||||
<div style="font-size:13px;color:#64748b;margin:4px 0;">帖子:${escapeHTML(postTitle)} — ${escapeHTML((r.post_content || '').substring(0, 30))}${(r.post_content || '').length > 30 ? '...' : ''}</div>
|
||||
<div style="font-size:13px;font-weight:600;">状态:${statusMap[r.status] || escapeHTML(r.status)}</div>
|
||||
${r.handler_name ? `<div style="font-size:12px;color:#94a3b8;">处理人:${escapeHTML(r.handler_name)}${r.handler_note ? ' (' + escapeHTML(r.handler_note) + ')' : ''}</div>` : ''}
|
||||
${r.status === 'pending' ? `
|
||||
<div class="report-actions">
|
||||
<button class="btn-sm btn-danger" data-reportid="${r.id}" data-action="approve">
|
||||
@@ -40,7 +40,7 @@ function renderAdminReports() {
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
container.querySelectorAll('[data-action]').forEach(btn => {
|
||||
btn.addEventListener('click', function() {
|
||||
const id = this.dataset.reportid;
|
||||
@@ -88,7 +88,7 @@ function renderAdminUsers(page = 1, search = '') {
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;flex-wrap:wrap;gap:8px;">
|
||||
<span style="font-size:13px;color:#94a3b8;">共 <strong>${pagination.total}</strong> 位用户</span>
|
||||
<div style="display:flex;gap:6px;">
|
||||
<input type="text" id="userSearchInput" placeholder="搜索用户名..." value="${search}"
|
||||
<input type="text" id="userSearchInput" placeholder="搜索用户名..." value="${escapeHTML(search)}"
|
||||
style="padding:6px 12px;border:1px solid var(--border);border-radius:4px;font-size:13px;background:var(--bg);color:var(--text);" />
|
||||
<button id="userSearchBtn" class="btn-sm btn-primary" style="padding:6px 14px;">搜索</button>
|
||||
<button id="userClearSearchBtn" class="btn-sm btn-secondary" style="padding:6px 14px;">清空</button>
|
||||
@@ -117,9 +117,9 @@ function renderAdminUsers(page = 1, search = '') {
|
||||
<tr style="border-bottom:1px solid #f1f5f9;${isCurrentUser ? 'background:var(--primary-bg);' : ''}">
|
||||
<td style="padding:10px 12px;">
|
||||
<div style="display:flex;align-items:center;gap:8px;">
|
||||
<img src="${u.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(u.username) + '&background=6366f1&color=fff&size=64'}"
|
||||
<img src="${escapeHTML(safeURL(u.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(u.username) + '&background=6366f1&color=fff&size=64', { image: true }))}"
|
||||
style="width:28px;height:28px;border-radius:50%;object-fit:cover;" />
|
||||
<span style="font-weight:500;">${u.username}</span>
|
||||
<span style="font-weight:500;">${escapeHTML(u.username)}</span>
|
||||
${isCurrentUser ? '<span style="font-size:11px;color:#94a3b8;background:#eef2ff;padding:1px 8px;border-radius:4px;">你</span>' : ''}
|
||||
</div>
|
||||
</td>
|
||||
@@ -193,7 +193,7 @@ function renderAdminUsers(page = 1, search = '') {
|
||||
`;
|
||||
}
|
||||
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
// 绑定分页按钮事件
|
||||
container.querySelectorAll('.users-page-btn:not([disabled])').forEach(btn => {
|
||||
@@ -285,8 +285,8 @@ function renderAdminLogs(page = 1) {
|
||||
html += `
|
||||
<tr style="border-bottom:1px solid #f1f5f9;">
|
||||
<td style="padding:6px 0;">${new Date(l.created_at).toLocaleString('zh-CN')}</td>
|
||||
<td style="padding:6px 0;">${l.username || '系统'}</td>
|
||||
<td style="padding:6px 0;">${l.action}</td>
|
||||
<td style="padding:6px 0;">${escapeHTML(l.username || '系统')}</td>
|
||||
<td style="padding:6px 0;">${escapeHTML(l.action)}</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
@@ -340,7 +340,7 @@ function renderAdminLogs(page = 1) {
|
||||
`;
|
||||
}
|
||||
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
// 绑定分页按钮事件
|
||||
container.querySelectorAll('.logs-page-btn:not([disabled])').forEach(btn => {
|
||||
@@ -383,7 +383,7 @@ function renderAdminLinks() {
|
||||
links.forEach(l => {
|
||||
html += `
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;padding:8px 0;border-bottom:1px solid #f1f5f9;">
|
||||
<span><a href="${l.url}" target="_blank" style="color:#6366f1;text-decoration:none;">${l.title}</a></span>
|
||||
<span><a href="${escapeHTML(safeURL(l.url, { allowRelative: false }))}" target="_blank" rel="noopener noreferrer" style="color:#6366f1;text-decoration:none;">${escapeHTML(l.title)}</a></span>
|
||||
<button class="btn-sm btn-danger" data-linkid="${l.id}" style="background:rgba(239,68,68,0.08);border:1.5px solid #ef4444;color:#1a1a2e;padding:2px 10px;border-radius:6px;transition:all 0.2s;cursor:pointer;font-size:12px;font-weight:500;">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
|
||||
删除
|
||||
@@ -391,7 +391,7 @@ function renderAdminLinks() {
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
ul.innerHTML = html;
|
||||
ul.innerHTML = sanitizeHTML(html);
|
||||
ul.querySelectorAll('[data-linkid]').forEach(btn => {
|
||||
btn.addEventListener('click', function() {
|
||||
if (!confirm('确定删除该链接吗?')) return;
|
||||
@@ -624,9 +624,9 @@ function loadCustomPageList() {
|
||||
pages.forEach(p => {
|
||||
html += `
|
||||
<tr style="border-bottom:1px solid var(--border-light);">
|
||||
<td style="padding:8px 12px;"><code style="background:var(--bg);padding:2px 6px;border-radius:4px;font-size:12px;">${p.name}</code></td>
|
||||
<td style="padding:8px 12px;">${p.title}</td>
|
||||
<td style="padding:8px 12px;"><code style="background:var(--bg);padding:2px 6px;border-radius:4px;font-size:12px;">?custom=${p.name}</code></td>
|
||||
<td style="padding:8px 12px;"><code style="background:var(--bg);padding:2px 6px;border-radius:4px;font-size:12px;">${escapeHTML(p.name)}</code></td>
|
||||
<td style="padding:8px 12px;">${escapeHTML(p.title)}</td>
|
||||
<td style="padding:8px 12px;"><code style="background:var(--bg);padding:2px 6px;border-radius:4px;font-size:12px;">?custom=${escapeHTML(p.name)}</code></td>
|
||||
<td style="padding:8px 12px;"><span style="color:${p.enabled ? '#22c55e' : '#ef4444'};">${p.enabled ? `${getIcon('success')} 启用` : `${getIcon('error')} 禁用`}</span></td>
|
||||
<td style="padding:8px 12px;text-align:center;display:flex;gap:6px;justify-content:center;">
|
||||
<button class="btn-sm btn-secondary" data-id="${p.id}" data-action="edit">${getIcon('edit')}</button>
|
||||
@@ -636,7 +636,7 @@ function loadCustomPageList() {
|
||||
`;
|
||||
});
|
||||
html += '</tbody></table></div>';
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
container.querySelectorAll('[data-action="edit"]').forEach(btn => {
|
||||
btn.addEventListener('click', function() {
|
||||
@@ -673,13 +673,13 @@ function openCustomPageEditor(page) {
|
||||
<h2 style="margin-bottom:16px;">${isEdit ? getIcon('edit') + ' 编辑页面' : getIcon('add') + ' 添加页面'}</h2>
|
||||
<div style="margin-bottom:12px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">页面名称</label>
|
||||
<input type="text" id="editorPageName" value="${isEdit ? page.name : ''}" ${isEdit ? 'readonly style="background:var(--border-light);color:var(--text-light);"' : ''}
|
||||
<input type="text" id="editorPageName" value="${isEdit ? escapeHTML(page.name) : ''}" ${isEdit ? 'readonly style="background:var(--border-light);color:var(--text-light);"' : ''}
|
||||
placeholder="about (用于 URL: ?custom=about)" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);" />
|
||||
${isEdit ? '<div style="font-size:12px;color:var(--text-light);margin-top:2px;">⚠️ 名称不可修改</div>' : '<div style="font-size:12px;color:var(--text-light);margin-top:2px;">只允许字母、数字、短横线和下划线</div>'}
|
||||
</div>
|
||||
<div style="margin-bottom:12px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">导航栏显示名称</label>
|
||||
<input type="text" id="editorPageTitle" value="${isEdit ? page.title : ''}"
|
||||
<input type="text" id="editorPageTitle" value="${isEdit ? escapeHTML(page.title) : ''}"
|
||||
placeholder="关于我们" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);" />
|
||||
</div>
|
||||
<div style="margin-bottom:12px;">
|
||||
@@ -691,7 +691,7 @@ function openCustomPageEditor(page) {
|
||||
</div>
|
||||
<div style="margin-bottom:12px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">页面内容(HTML + CSS + JS)</label>
|
||||
<textarea id="editorContent" rows="12" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:13px;font-family:monospace;background:var(--bg);color:var(--text);resize:vertical;">${isEdit ? page.content : ''}</textarea>
|
||||
<textarea id="editorContent" rows="12" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:13px;font-family:monospace;background:var(--bg);color:var(--text);resize:vertical;">${isEdit ? escapeHTML(page.content) : ''}</textarea>
|
||||
<div style="font-size:12px;color:var(--text-light);margin-top:2px;">支持 HTML、CSS(<style>)、JS(<script>),内容会在独立的沙盒中渲染</div>
|
||||
</div>
|
||||
<button id="editorSaveBtn" class="btn-primary" style="padding:10px 24px;width:100%;">保存</button>
|
||||
|
||||
@@ -19,7 +19,7 @@ function showToast(message, type = 'success', duration = 3000) {
|
||||
|
||||
toast.innerHTML = `
|
||||
<span class="toast-icon">${icons[type] || icons.success}</span>
|
||||
${message}
|
||||
${escapeHTML(message)}
|
||||
`;
|
||||
|
||||
document.body.appendChild(toast);
|
||||
@@ -224,25 +224,30 @@ async function renderMessageList() {
|
||||
const unread = c.unread_count || 0;
|
||||
const lastMsg = c.last_message || '暂无消息';
|
||||
const time = c.last_message_time ? new Date(c.last_message_time).toLocaleString('zh-CN') : '';
|
||||
const avatar = c.other_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(c.other_username) + '&background=6366f1&color=fff&size=64';
|
||||
html += `
|
||||
<div class="message-list-item" style="display:flex;align-items:center;gap:12px;padding:12px 16px;border-bottom:1px solid var(--border);cursor:pointer;transition:background 0.15s;"
|
||||
onclick="openChat('${c.id}', '${c.other_user_id}', '${c.other_username}')">
|
||||
<img src="${c.other_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(c.other_username) + '&background=6366f1&color=fff&size=64'}"
|
||||
<div class="message-list-item" data-conversation-id="${escapeHTML(c.id)}" data-user-id="${escapeHTML(c.other_user_id)}" data-username="${escapeHTML(c.other_username)}" style="display:flex;align-items:center;gap:12px;padding:12px 16px;border-bottom:1px solid var(--border);cursor:pointer;transition:background 0.15s;">
|
||||
<img src="${escapeHTML(safeURL(avatar, { image: true }))}"
|
||||
style="width:40px;height:40px;border-radius:50%;object-fit:cover;" />
|
||||
<div style="flex:1;min-width:0;">
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;">
|
||||
<span style="font-weight:600;">${c.other_username}</span>
|
||||
<span style="font-weight:600;">${escapeHTML(c.other_username)}</span>
|
||||
<span style="font-size:12px;color:var(--text-light);">${time}</span>
|
||||
</div>
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;">
|
||||
<span style="font-size:13px;color:var(--text-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:200px;">${lastMsg}</span>
|
||||
<span style="font-size:13px;color:var(--text-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:200px;">${escapeHTML(lastMsg)}</span>
|
||||
${unread > 0 ? `<span style="background:#ef4444;color:#fff;border-radius:50%;padding:2px 8px;font-size:11px;font-weight:600;">${unread}</span>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
container.querySelectorAll('.message-list-item').forEach(item => {
|
||||
item.addEventListener('click', function() {
|
||||
openChat(this.dataset.conversationId, this.dataset.userId, this.dataset.username);
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:20px 0;">加载失败</div>';
|
||||
}
|
||||
@@ -294,12 +299,13 @@ async function renderMessages(conversationId, silent = false) {
|
||||
messages.forEach(m => {
|
||||
const isMine = m.sender_id === currentUser.id;
|
||||
const time = m.created_at ? new Date(m.created_at).toLocaleString('zh-CN') : '';
|
||||
const avatar = m.sender_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(m.sender_username) + '&background=6366f1&color=fff&size=64';
|
||||
html += `
|
||||
<div style="display:flex;${isMine ? 'justify-content:flex-end;' : 'justify-content:flex-start;'} margin-bottom:12px;">
|
||||
${!isMine ? `<img src="${m.sender_avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(m.sender_username) + '&background=6366f1&color=fff&size=64'}" style="width:32px;height:32px;border-radius:50%;object-fit:cover;margin-right:8px;flex-shrink:0;" />` : ''}
|
||||
${!isMine ? `<img src="${escapeHTML(safeURL(avatar, { image: true }))}" style="width:32px;height:32px;border-radius:50%;object-fit:cover;margin-right:8px;flex-shrink:0;" />` : ''}
|
||||
<div style="max-width:70%;">
|
||||
<div style="background:${isMine ? 'var(--primary)' : 'var(--surface)'};color:${isMine ? '#fff' : 'var(--text)'};padding:10px 14px;border-radius:12px;border:${isMine ? 'none' : '1px solid var(--border)'};word-break:break-word;">
|
||||
${m.content}
|
||||
${renderPlainText(m.content)}
|
||||
</div>
|
||||
<div style="font-size:11px;color:var(--text-light);margin-top:4px;${isMine ? 'text-align:right;' : ''}">
|
||||
${time} ${isMine ? (m.is_read ? '✓✓' : '✓') : ''}
|
||||
@@ -308,7 +314,7 @@ async function renderMessages(conversationId, silent = false) {
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
container.scrollTop = container.scrollHeight;
|
||||
} catch (err) {
|
||||
if (!silent) {
|
||||
@@ -452,7 +458,7 @@ function showCustomPage(pageName) {
|
||||
API.getCustomPage(pageName).then(page => {
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.style.cssText = 'width:100%;min-height:70vh;border:none;border-radius:8px;background:var(--surface);';
|
||||
iframe.sandbox = 'allow-scripts allow-modals allow-same-origin';
|
||||
iframe.sandbox = 'allow-scripts allow-modals';
|
||||
iframe.srcdoc = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -483,7 +489,7 @@ function showCustomPage(pageName) {
|
||||
el.style.color = el.dataset.custom === pageName ? 'var(--primary)' : 'var(--text-secondary)';
|
||||
});
|
||||
}).catch(err => {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">页面加载失败:' + err.message + '</div>';
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">页面加载失败:' + escapeHTML(err.message) + '</div>';
|
||||
});
|
||||
}
|
||||
|
||||
@@ -527,15 +533,15 @@ async function renderMessagesPage() {
|
||||
<div style="display:flex;align-items:flex-start;gap:12px;padding:14px 16px;border-bottom:1px solid var(--border-light);background:var(--surface);border-radius:6px;margin-bottom:6px;">
|
||||
<span style="font-size:20px;display:inline-flex;align-items:center;">${icon}</span>
|
||||
<div style="flex:1;min-width:0;">
|
||||
<div style="font-weight:600;font-size:14px;">${n.title}</div>
|
||||
<div style="color:var(--text-secondary);font-size:13px;margin-top:2px;">${n.content}</div>
|
||||
${n.link ? `<a href="${n.link}" style="color:var(--primary);font-size:13px;text-decoration:none;margin-top:4px;display:inline-block;">查看详情 →</a>` : ''}
|
||||
<div style="font-weight:600;font-size:14px;">${escapeHTML(n.title)}</div>
|
||||
<div style="color:var(--text-secondary);font-size:13px;margin-top:2px;">${escapeHTML(n.content)}</div>
|
||||
${n.link ? `<a href="${escapeHTML(safeURL(n.link))}" style="color:var(--primary);font-size:13px;text-decoration:none;margin-top:4px;display:inline-block;">查看详情 →</a>` : ''}
|
||||
<div style="font-size:12px;color:var(--text-light);margin-top:4px;">${time}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:20px 0;">加载失败</div>';
|
||||
}
|
||||
@@ -579,9 +585,9 @@ function renderLinks() {
|
||||
} else {
|
||||
let html = '';
|
||||
links.forEach(l => {
|
||||
html += '<li><a href="' + l.url + '" target="_blank">' + l.title + '</a></li>';
|
||||
html += '<li><a href="' + escapeHTML(safeURL(l.url, { allowRelative: false })) + '" target="_blank" rel="noopener noreferrer">' + escapeHTML(l.title) + '</a></li>';
|
||||
});
|
||||
ul.innerHTML = html;
|
||||
ul.innerHTML = sanitizeHTML(html);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -593,9 +599,9 @@ function renderLinks() {
|
||||
} else {
|
||||
let html = '';
|
||||
links.forEach(l => {
|
||||
html += '<li><a href="' + l.url + '" target="_blank">' + l.title + '</a></li>';
|
||||
html += '<li><a href="' + escapeHTML(safeURL(l.url, { allowRelative: false })) + '" target="_blank" rel="noopener noreferrer">' + escapeHTML(l.title) + '</a></li>';
|
||||
});
|
||||
ul2.innerHTML = html;
|
||||
ul2.innerHTML = sanitizeHTML(html);
|
||||
}
|
||||
}
|
||||
}).catch(() => {});
|
||||
@@ -640,15 +646,15 @@ async function renderSettingsPage(tab = 'profile') {
|
||||
<!-- 头像 -->
|
||||
<div style="text-align:center;margin-bottom:24px;">
|
||||
<div style="position:relative;display:inline-block;">
|
||||
<img id="avatarPreview" src="${user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128'}"
|
||||
<img id="avatarPreview" src="${escapeHTML(safeURL(user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128', { image: true }))}"
|
||||
style="width:100px;height:100px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
|
||||
<button id="avatarUploadBtn" style="position:absolute;bottom:0;right:0;background:var(--primary);color:#fff;border:none;border-radius:50%;width:32px;height:32px;cursor:pointer;font-size:16px;display:flex;align-items:center;justify-content:center;box-shadow:0 2px 8px rgba(99,102,241,0.4);">
|
||||
${getIcon('camera')}
|
||||
</button>
|
||||
</div>
|
||||
<input type="file" id="avatarFileInput" accept="image/*" style="display:none;" />
|
||||
<h2 style="margin:12px 0 4px;">${user.username}</h2>
|
||||
<p style="color:var(--text-secondary);font-size:14px;">${user.bio || '这个人很懒,什么都没写'}</p>
|
||||
<h2 style="margin:12px 0 4px;">${escapeHTML(user.username)}</h2>
|
||||
<p style="color:var(--text-secondary);font-size:14px;">${escapeHTML(user.bio || '这个人很懒,什么都没写')}</p>
|
||||
<div style="display:flex;justify-content:center;gap:16px;margin-top:8px;font-size:13px;color:var(--text-secondary);flex-wrap:wrap;">
|
||||
<span>${getIcon('calendar')} 加入 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
|
||||
<span>${getIcon('file')} ${postCount} 帖</span>
|
||||
@@ -661,15 +667,15 @@ async function renderSettingsPage(tab = 'profile') {
|
||||
<div style="background:var(--surface);border:1px solid var(--border);border-radius:8px;padding:20px;">
|
||||
<div style="margin-bottom:16px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">用户名</label>
|
||||
<input type="text" id="settingsUsername" value="${user.username}" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);" />
|
||||
<input type="text" id="settingsUsername" value="${escapeHTML(user.username)}" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);" />
|
||||
</div>
|
||||
<div style="margin-bottom:16px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">个人简介</label>
|
||||
<textarea id="settingsBio" rows="3" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);resize:vertical;">${user.bio || ''}</textarea>
|
||||
<textarea id="settingsBio" rows="3" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:14px;background:var(--bg);color:var(--text);resize:vertical;">${escapeHTML(user.bio || '')}</textarea>
|
||||
</div>
|
||||
<div style="margin-bottom:16px;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:4px;">帖子签名</label>
|
||||
<textarea id="settingsSignature" rows="2" placeholder="显示在每篇帖子底部,支持 Markdown" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:13px;background:var(--bg);color:var(--text);resize:vertical;font-family:inherit;">${user.signature || ''}</textarea>
|
||||
<textarea id="settingsSignature" rows="2" placeholder="显示在每篇帖子底部,支持 Markdown" style="width:100%;padding:8px 12px;border:1px solid var(--border);border-radius:4px;font-size:13px;background:var(--bg);color:var(--text);resize:vertical;font-family:inherit;">${escapeHTML(user.signature || '')}</textarea>
|
||||
<div style="font-size:12px;color:var(--text-light);margin-top:2px;">用 --- 分隔,支持 Markdown</div>
|
||||
</div>
|
||||
<button id="settingsSaveBtn" class="btn-primary" style="width:100%;padding:10px;">保存设置</button>
|
||||
@@ -916,8 +922,8 @@ function openEditModal(postId, currentTitle, currentContent) {
|
||||
<div class="modal-content" style="max-width:600px;">
|
||||
<span class="close" style="position:absolute;top:12px;right:16px;font-size:24px;cursor:pointer;color:var(--text-light);">×</span>
|
||||
<h2 style="margin-bottom:16px;">${getIcon('edit')} 编辑帖子</h2>
|
||||
<input type="text" id="editPostTitle" value="${currentTitle || ''}" placeholder="标题" style="width:100%;padding:10px 14px;border:1.5px solid var(--border);border-radius:6px;font-size:15px;font-weight:600;margin-bottom:12px;font-family:inherit;background:var(--bg);color:var(--text);" />
|
||||
<textarea id="editPostContent" rows="6" style="width:100%;padding:12px;border:1.5px solid var(--border);border-radius:6px;font-size:15px;font-family:inherit;resize:vertical;background:var(--bg);color:var(--text);">${currentContent || ''}</textarea>
|
||||
<input type="text" id="editPostTitle" value="${escapeHTML(currentTitle || '')}" placeholder="标题" style="width:100%;padding:10px 14px;border:1.5px solid var(--border);border-radius:6px;font-size:15px;font-weight:600;margin-bottom:12px;font-family:inherit;background:var(--bg);color:var(--text);" />
|
||||
<textarea id="editPostContent" rows="6" style="width:100%;padding:12px;border:1.5px solid var(--border);border-radius:6px;font-size:15px;font-family:inherit;resize:vertical;background:var(--bg);color:var(--text);">${escapeHTML(currentContent || '')}</textarea>
|
||||
<button id="editPostSaveBtn" class="btn-primary" style="padding:10px 24px;margin-top:12px;width:100%;">保存修改</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
+24
-10
@@ -38,18 +38,18 @@ function renderFeed() {
|
||||
if (p.images && p.images.length > 0) {
|
||||
imagesHtml = '<div class="post-images">';
|
||||
p.images.forEach(img => {
|
||||
imagesHtml += '<img src="' + img + '" class="post-image" onclick="openImageViewer(this.src)" style="cursor:pointer;" />';
|
||||
imagesHtml += '<img src="' + escapeHTML(safeURL(img, { image: true })) + '" class="post-image" style="cursor:pointer;" />';
|
||||
});
|
||||
imagesHtml += '</div>';
|
||||
}
|
||||
const replyCount = p.reply_count || 0;
|
||||
|
||||
const renderedContent = marked ? marked.parse(p.content || '') : (p.content || '').replace(/\n/g, '<br>');
|
||||
const renderedContent = renderMarkdown(p.content || '');
|
||||
|
||||
// 签名渲染
|
||||
let signatureHtml = '';
|
||||
if (p.signature) {
|
||||
const sigContent = marked ? marked.parse(p.signature) : p.signature;
|
||||
const sigContent = renderMarkdown(p.signature);
|
||||
signatureHtml = `
|
||||
<div style="margin-top:10px;padding-top:8px;border-top:1px solid var(--border-light);font-size:12px;color:var(--text-secondary);">
|
||||
${sigContent}
|
||||
@@ -61,12 +61,12 @@ function renderFeed() {
|
||||
<div class="post-card" data-postid="${p.id}" style="cursor:pointer;">
|
||||
${p.is_pinned ? '<div style="font-size:12px;color:var(--primary);font-weight:600;margin-bottom:4px;">📌 置顶</div>' : ''}
|
||||
<div class="post-header">
|
||||
<img src="${avatar}" class="post-avatar" />
|
||||
<span class="post-username" style="cursor:pointer;color:var(--primary);" onclick="event.stopPropagation();switchPage('user','${username}')">${username}</span>
|
||||
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" class="post-avatar" />
|
||||
<span class="post-username" data-username="${escapeHTML(username)}" style="cursor:pointer;color:var(--primary);">${escapeHTML(username)}</span>
|
||||
<span class="post-time">${time}</span>
|
||||
${p.edited_at ? '<span style="font-size:11px;color:var(--text-light);margin-left:6px;">(已编辑)</span>' : ''}
|
||||
</div>
|
||||
<div class="post-title">${p.title || '无标题'}</div>
|
||||
<div class="post-title">${escapeHTML(p.title || '无标题')}</div>
|
||||
<div class="post-content">${renderedContent}</div>
|
||||
${imagesHtml}
|
||||
<div class="post-actions">
|
||||
@@ -87,7 +87,7 @@ function renderFeed() {
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
// 分页控件
|
||||
if (totalPages > 1) {
|
||||
@@ -167,6 +167,20 @@ function renderFeed() {
|
||||
});
|
||||
});
|
||||
|
||||
container.querySelectorAll('.post-username').forEach(username => {
|
||||
username.addEventListener('click', function(e) {
|
||||
e.stopPropagation();
|
||||
switchPage('user', this.dataset.username);
|
||||
});
|
||||
});
|
||||
|
||||
container.querySelectorAll('.post-image').forEach(image => {
|
||||
image.addEventListener('click', function(e) {
|
||||
e.stopPropagation();
|
||||
openImageViewer(this.src);
|
||||
});
|
||||
});
|
||||
|
||||
container.querySelectorAll('.action-report').forEach(btn => {
|
||||
btn.addEventListener('click', function(e) {
|
||||
e.stopPropagation();
|
||||
@@ -187,7 +201,7 @@ function renderFeed() {
|
||||
});
|
||||
});
|
||||
}).catch(err => {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + err.message +
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) +
|
||||
'</div>';
|
||||
});
|
||||
}
|
||||
@@ -210,9 +224,9 @@ function renderLinks() {
|
||||
}
|
||||
let html = '';
|
||||
links.forEach(l => {
|
||||
html += '<li><a href="' + l.url + '" target="_blank">' + l.title + '</a></li>';
|
||||
html += '<li><a href="' + escapeHTML(safeURL(l.url, { allowRelative: false })) + '" target="_blank" rel="noopener noreferrer">' + escapeHTML(l.title) + '</a></li>';
|
||||
});
|
||||
ul.innerHTML = html;
|
||||
ul.innerHTML = sanitizeHTML(html);
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
|
||||
+22
-19
@@ -49,26 +49,17 @@ async function renderPostDetail(postId) {
|
||||
if (post.images && post.images.length > 0) {
|
||||
imagesHtml = '<div class="post-images">';
|
||||
post.images.forEach(img => {
|
||||
imagesHtml += '<img src="' + img + '" class="post-image" onclick="openImageViewer(this.src)" style="cursor:pointer;" />';
|
||||
imagesHtml += '<img src="' + escapeHTML(safeURL(img, { image: true })) + '" class="post-image" style="cursor:pointer;" />';
|
||||
});
|
||||
imagesHtml += '</div>';
|
||||
}
|
||||
|
||||
let renderedContent = post.content || '';
|
||||
try {
|
||||
if (typeof marked !== 'undefined' && marked.parse) {
|
||||
renderedContent = marked.parse(renderedContent);
|
||||
} else {
|
||||
renderedContent = renderedContent.replace(/\n/g, '<br>');
|
||||
}
|
||||
} catch (e) {
|
||||
renderedContent = renderedContent.replace(/\n/g, '<br>');
|
||||
}
|
||||
const renderedContent = renderMarkdown(post.content || '');
|
||||
|
||||
// 签名渲染
|
||||
let signatureHtml = '';
|
||||
if (post.signature) {
|
||||
const sigContent = marked ? marked.parse(post.signature) : post.signature;
|
||||
const sigContent = renderMarkdown(post.signature);
|
||||
signatureHtml = `
|
||||
<div style="margin-top:16px;padding-top:12px;border-top:1px solid var(--border-light);font-size:12px;color:var(--text-secondary);">
|
||||
${sigContent}
|
||||
@@ -79,8 +70,8 @@ async function renderPostDetail(postId) {
|
||||
let html = `
|
||||
<div class="post-detail-card">
|
||||
<div class="post-header">
|
||||
<img src="${avatar}" class="post-avatar" />
|
||||
<span class="post-username" style="cursor:pointer;color:var(--primary);" onclick="switchPage('user','${username}')">${username}</span>
|
||||
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" class="post-avatar" />
|
||||
<span class="post-username" data-username="${escapeHTML(username)}" style="cursor:pointer;color:var(--primary);">${escapeHTML(username)}</span>
|
||||
<span class="post-time">${time}</span>
|
||||
${post.edited_at ? `<span style="font-size:12px;color:var(--text-light);margin-left:8px;">(已编辑 ${new Date(post.edited_at).toLocaleString('zh-CN')})</span>` : ''}
|
||||
${post.is_pinned ? '<span style="font-size:12px;color:var(--primary);margin-left:8px;">📌 置顶</span>' : ''}
|
||||
@@ -120,28 +111,40 @@ async function renderPostDetail(postId) {
|
||||
html += `
|
||||
<div class="reply-item" data-replyid="${r.id}">
|
||||
<div class="reply-header">
|
||||
<img src="${rAvatar}" class="reply-avatar" />
|
||||
<span class="reply-username" style="cursor:pointer;color:var(--primary);" onclick="switchPage('user','${rUsername}')">${rUsername}</span>
|
||||
<img src="${escapeHTML(safeURL(rAvatar, { image: true }))}" class="reply-avatar" />
|
||||
<span class="reply-username" data-username="${escapeHTML(rUsername)}" style="cursor:pointer;color:var(--primary);">${escapeHTML(rUsername)}</span>
|
||||
<span class="reply-time">${rTime}</span>
|
||||
${currentUser && currentUser.id === r.user_id ? `<button class="btn-sm btn-danger reply-delete-btn" data-replyid="${r.id}">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
|
||||
删除
|
||||
</button>` : ''}
|
||||
</div>
|
||||
<div class="reply-content">${(r.content || '').replace(/\n/g, '<br>')}</div>
|
||||
<div class="reply-content">${renderPlainText(r.content || '')}</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
replyArea.style.display = 'block';
|
||||
document.getElementById('replyContent').value = '';
|
||||
document.getElementById('replyCaptchaInput').value = '';
|
||||
refreshCaptcha('reply');
|
||||
|
||||
container.querySelectorAll('.post-username, .reply-username').forEach(usernameEl => {
|
||||
usernameEl.addEventListener('click', function() {
|
||||
switchPage('user', this.dataset.username);
|
||||
});
|
||||
});
|
||||
|
||||
container.querySelectorAll('.post-image').forEach(image => {
|
||||
image.addEventListener('click', function() {
|
||||
openImageViewer(this.src);
|
||||
});
|
||||
});
|
||||
|
||||
// 置顶按钮
|
||||
const pinBtn = document.getElementById('pinPostBtn');
|
||||
if (pinBtn) {
|
||||
@@ -210,7 +213,7 @@ async function renderPostDetail(postId) {
|
||||
currentPostId = postId;
|
||||
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + err.message +
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) +
|
||||
'</div>';
|
||||
replyArea.style.display = 'none';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
// ============================================================
|
||||
// Safe rendering helpers
|
||||
// ============================================================
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value ?? '')
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function safeURL(value, { allowRelative = true, image = false } = {}) {
|
||||
if (!value) return image ? '' : '#';
|
||||
|
||||
try {
|
||||
const raw = String(value).trim();
|
||||
const parsed = new URL(raw, window.location.origin);
|
||||
const isRelative = parsed.origin === window.location.origin && !/^[a-z][a-z0-9+.-]*:/i.test(raw);
|
||||
const allowedProtocol = parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
||||
|
||||
if (!allowedProtocol || (!allowRelative && isRelative)) return image ? '' : '#';
|
||||
return isRelative ? parsed.pathname + parsed.search + parsed.hash : parsed.href;
|
||||
} catch (error) {
|
||||
return image ? '' : '#';
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizeHTML(html) {
|
||||
const template = document.createElement('template');
|
||||
template.innerHTML = String(html ?? '');
|
||||
|
||||
// Application templates contain legitimate controls. Raw user HTML is escaped
|
||||
// before Markdown parsing, while this pass removes executable/embed elements.
|
||||
const forbidden = 'script,iframe,object,embed,link,meta,base,style';
|
||||
template.content.querySelectorAll(forbidden).forEach(node => node.remove());
|
||||
|
||||
template.content.querySelectorAll('*').forEach(node => {
|
||||
for (const attribute of Array.from(node.attributes)) {
|
||||
const name = attribute.name.toLowerCase();
|
||||
if (name.startsWith('on') || name === 'srcdoc') {
|
||||
node.removeAttribute(attribute.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (node.hasAttribute('href')) {
|
||||
const href = safeURL(node.getAttribute('href'));
|
||||
if (href === '#') node.removeAttribute('href');
|
||||
else node.setAttribute('href', href);
|
||||
}
|
||||
|
||||
if (node.hasAttribute('src')) {
|
||||
const src = safeURL(node.getAttribute('src'), { image: true });
|
||||
if (!src) node.removeAttribute('src');
|
||||
else node.setAttribute('src', src);
|
||||
}
|
||||
|
||||
if (node.tagName === 'A' && node.getAttribute('target') === '_blank') {
|
||||
node.setAttribute('rel', 'noopener noreferrer');
|
||||
}
|
||||
});
|
||||
|
||||
return template.innerHTML;
|
||||
}
|
||||
|
||||
function renderMarkdown(value) {
|
||||
const escaped = escapeHTML(value);
|
||||
if (typeof marked !== 'undefined' && marked.parse) {
|
||||
return sanitizeHTML(marked.parse(escaped));
|
||||
}
|
||||
return escaped.replace(/\r?\n/g, '<br>');
|
||||
}
|
||||
|
||||
function renderPlainText(value) {
|
||||
return escapeHTML(value).replace(/\r?\n/g, '<br>');
|
||||
}
|
||||
+14
-8
@@ -24,9 +24,9 @@ async function renderUserProfile(username) {
|
||||
|
||||
let html = `
|
||||
<div style="background:var(--surface);border:1px solid var(--border);border-radius:8px;padding:32px;text-align:center;">
|
||||
<img src="${avatar}" style="width:96px;height:96px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
|
||||
<h2 style="margin:16px 0 4px;font-size:24px;">${user.username}</h2>
|
||||
<p style="color:var(--text-secondary);font-size:14px;">${user.bio || '这个人很懒,什么都没写'}</p>
|
||||
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" style="width:96px;height:96px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
|
||||
<h2 style="margin:16px 0 4px;font-size:24px;">${escapeHTML(user.username)}</h2>
|
||||
<p style="color:var(--text-secondary);font-size:14px;">${escapeHTML(user.bio || '这个人很懒,什么都没写')}</p>
|
||||
<div style="display:flex;justify-content:center;gap:32px;margin-top:16px;font-size:14px;color:var(--text-secondary);flex-wrap:wrap;">
|
||||
<span>📅 加入于 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
|
||||
<span>📝 发了 ${posts.length} 个帖子</span>
|
||||
@@ -48,16 +48,22 @@ async function renderUserProfile(username) {
|
||||
posts.forEach(p => {
|
||||
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
|
||||
html += `
|
||||
<div class="post-card" style="cursor:pointer;" onclick="switchToPost('${p.id}')">
|
||||
<div class="post-title" style="font-size:16px;font-weight:600;">${p.title || '无标题'}</div>
|
||||
<div class="post-content" style="font-size:14px;color:var(--text-secondary);">${(p.content || '').substring(0, 100)}${(p.content || '').length > 100 ? '...' : ''}</div>
|
||||
<div class="post-card" data-postid="${escapeHTML(p.id)}" style="cursor:pointer;">
|
||||
<div class="post-title" style="font-size:16px;font-weight:600;">${escapeHTML(p.title || '无标题')}</div>
|
||||
<div class="post-content" style="font-size:14px;color:var(--text-secondary);">${escapeHTML((p.content || '').substring(0, 100))}${(p.content || '').length > 100 ? '...' : ''}</div>
|
||||
<div style="font-size:12px;color:var(--text-light);margin-top:8px;">${time}</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
container.innerHTML = html;
|
||||
container.innerHTML = sanitizeHTML(html);
|
||||
|
||||
container.querySelectorAll('.post-card[data-postid]').forEach(card => {
|
||||
card.addEventListener('click', function() {
|
||||
switchToPost(this.dataset.postid);
|
||||
});
|
||||
});
|
||||
|
||||
const dmBtn = document.getElementById('dmBtn');
|
||||
if (dmBtn) {
|
||||
@@ -67,7 +73,7 @@ async function renderUserProfile(username) {
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + err.message + '</div>';
|
||||
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) + '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user