Sanitize user text input

This commit is contained in:
rueeggeandre committed 2026-06-07 23:08:29 +02:00
1 parent a8207c4f7b
commit 86c40aee91
9 files changed
+179 -59

No files matched your search

+15 -1
View File
@@ -104,14 +104,28 @@ async function logout() {
function escapeHtml(str) {
if (!str) return "";
return str.replace(/[&<>]/g, function (m) {
return String(str).replace(/[&<>"']/g, function (m) {
if (m === "&") return "&amp;";
if (m === "<") return "&lt;";
if (m === ">") return "&gt;";
if (m === '"') return "&quot;";
if (m === "'") return "&#39;";
return m;
});
}
function escapeAttribute(str) {
return escapeHtml(str);
}
function sanitizeDisplayUrl(url) {
const value = String(url || "").trim().replace(/[\u0000-\u001f\u007f\s]+/g, "");
if (/^(https?:\/\/|\/uploads\/|uploads\/)/i.test(value)) {
return value;
}
return "";
}
function showToast(msg, isError = false) {
const toast = document.getElementById("toast");
if (!toast) return;
+7 -4
View File
@@ -25,11 +25,13 @@ function renderJourneys(journeys) {
return;
}
container.innerHTML = journeys.map(journey => `
container.innerHTML = journeys.map(journey => {
const imageUrl = sanitizeDisplayUrl(journey.image || '');
return `
<article class="post-card">
${journey.image ? `<img class="post-card-image" src="${journey.image}" alt="${journey.title}">` : '<div class="post-card-image" style="background: var(--surface-3); display: flex; align-items: center; justify-content: center;"><i class="fas fa-image" style="font-size: 3rem; color: var(--gray-5);"></i></div>'}
${imageUrl ? `<img class="post-card-image" src="${escapeAttribute(imageUrl)}" alt="${escapeAttribute(journey.title)}">` : '<div class="post-card-image" style="background: var(--surface-3); display: flex; align-items: center; justify-content: center;"><i class="fas fa-image" style="font-size: 3rem; color: var(--gray-5);"></i></div>'}
<div class="post-card-content">
<h2 class="post-card-title"><a href="blog-post.html?id=${journey.id}">${escapeHtml(journey.title)}</a></h2>
<h2 class="post-card-title"><a href="blog-post.html?id=${encodeURIComponent(journey.id)}">${escapeHtml(journey.title)}</a></h2>
<div class="post-card-meta">
<i class="fas fa-calendar-alt"></i> ${new Date(journey.created_at).toLocaleDateString()}
${journey.markers ? `<span style="margin-left: 12px;"><i class="fas fa-map-marker-alt"></i> ${journey.markers.length} chapters</span>` : ''}
@@ -38,7 +40,8 @@ function renderJourneys(journeys) {
<div class="post-card-excerpt">${escapeHtml(journey.description || journey.markers?.[0]?.text?.substring(0, 150) + '…')}</div>
</div>
</article>
`).join('');
`;
}).join('');
}
function getJourneyBadges(journey) {
+7 -6
View File
@@ -36,7 +36,7 @@ function renderJourney() {
marker.images = getMarkerImages(marker);
const images = getMarkerImagesHtml(marker.images, idx, canEdit);
chaptersHtml += `
<div class="chapter" data-marker-id="${marker.id || idx}">
<div class="chapter" data-marker-id="${idx}">
<div class="chapter-header">
<h3>${escapeHtml(title)}</h3>
${date}
@@ -60,7 +60,7 @@ function renderJourney() {
${currentJourney.visibility === 'public' ? '<span class="badge">Public</span>' : ''}
${currentJourney.visibility === 'shared' && !isOwner ? `<span class="badge">${canEdit ? 'Shared edit' : 'Shared'}</span>` : ''}
</div>
${currentJourney.image ? `<img class="post-image" src="${currentJourney.image}" alt="${currentJourney.title}">` : ''}
${currentJourney.image ? `<img class="post-image" src="${escapeAttribute(sanitizeDisplayUrl(getUploadUrl(currentJourney.image)))}" alt="${escapeAttribute(currentJourney.title)}">` : ''}
<div class="post-description markdown-content">${renderMarkdown(currentJourney.description)}</div>
${chaptersHtml}
${canEdit || isOwner ? `
@@ -82,7 +82,7 @@ function renderJourney() {
function getUploadUrl(path) {
if (!path) return '';
if (path.startsWith('http')) return path;
if (path.startsWith('http')) return sanitizeDisplayUrl(path);
return API_BASE.replace('/api', path);
}
@@ -115,10 +115,11 @@ function getMarkerImagesHtml(images, markerIndex, canEdit) {
const imageHtml = images.map((image, imageIndex) => {
const url = typeof image === 'string' ? image : image.url;
const alt = typeof image === 'string' ? 'Journey image' : image.originalName || 'Journey image';
if (!url) return '';
const displayUrl = sanitizeDisplayUrl(getUploadUrl(url));
if (!displayUrl) return '';
return `
<div class="chapter-image-item">
<img src="${escapeAttribute(getUploadUrl(url))}" alt="${escapeAttribute(alt)}">
<img src="${escapeAttribute(displayUrl)}" alt="${escapeAttribute(alt)}">
${canEdit ? `
<button type="button" class="remove-chapter-image" data-marker-index="${markerIndex}" data-image-index="${imageIndex}" aria-label="Remove image">
<i class="fas fa-times"></i>
@@ -294,7 +295,7 @@ function renderComments(comments) {
function getCommentFormHtml() {
return `
<div class="comment-form">
<textarea id="comment-text" rows="3" placeholder="Write a comment..."></textarea>
<textarea id="comment-text" rows="3" maxlength="2000" placeholder="Write a comment..."></textarea>
<button id="submit-comment" class="btn btn-sm" style="margin-top: var(--size-2);"><i class="fas fa-paper-plane"></i> Post Comment</button>
</div>
`;
+7 -6
View File
@@ -21,7 +21,7 @@ function showToast(message, isError = false) {
function getUploadUrl(path) {
if (!path) return '';
if (path.startsWith('http')) return path;
if (path.startsWith('http')) return sanitizeDisplayUrl(path);
return API_BASE.replace('/api', path);
}
@@ -130,10 +130,11 @@ function renderMarkerImages(marker, idx) {
${images.map((image, imageIdx) => {
const url = typeof image === 'string' ? image : image.url;
const alt = typeof image === 'string' ? 'Chapter image' : image.originalName || 'Chapter image';
if (!url) return '';
const displayUrl = sanitizeDisplayUrl(getUploadUrl(url));
if (!displayUrl) return '';
return `
<div class="image-preview">
<img src="${escapeAttribute(getUploadUrl(url))}" alt="${escapeAttribute(alt)}">
<img src="${escapeAttribute(displayUrl)}" alt="${escapeAttribute(alt)}">
<button type="button" class="remove-image" data-index="${idx}" data-image-index="${imageIdx}" aria-label="Remove image">
<i class="fas fa-times"></i>
</button>
@@ -183,15 +184,15 @@ function renderMarkers() {
</div>
<div class="form-group">
<label>Chapter Title</label>
<input type="text" class="marker-title" data-index="${idx}" value="${escapeHtml(marker.title || '')}" placeholder="Title">
<input type="text" class="marker-title" data-index="${idx}" value="${escapeHtml(marker.title || '')}" maxlength="200" placeholder="Title">
</div>
<div class="form-group">
<label>Date (optional)</label>
<input type="date" class="marker-date" data-index="${idx}" value="${marker.date || ''}">
<input type="date" class="marker-date" data-index="${idx}" value="${escapeAttribute(marker.date || '')}">
</div>
<div class="form-group">
<label>Description</label>
<textarea class="marker-description" data-index="${idx}" rows="5" placeholder="Describe this chapter. You can use Markdown like **bold**, [link](https://example.com), or ![image](https://example.com/image.jpg).">${escapeHtml(marker.description || '')}</textarea>
<textarea class="marker-description" data-index="${idx}" rows="5" maxlength="10000" placeholder="Describe this chapter. You can use Markdown like **bold**, [link](https://example.com), or ![image](https://example.com/image.jpg).">${escapeHtml(marker.description || '')}</textarea>
<div class="markdown-preview markdown-content" data-preview-index="${idx}">
${renderMarkdownPreview(marker.description)}
</div>
+10 -5
View File
@@ -59,7 +59,7 @@
}).addTo(map);
marker.bindPopup(
`<strong>${content.title || "Untitled"}</strong>`,
`<strong>${escapeHtml(content.title || "Untitled")}</strong>`,
);
marker.on("click", () => openMarkerEditor(marker));
marker.on("dragend", () => {
@@ -90,7 +90,7 @@
div.dataset.lat = latlng.lat;
div.dataset.lng = latlng.lng;
div.innerHTML = `
<div class="marker-title">${content.title || "Untitled"}</div>
<div class="marker-title">${escapeHtml(content.title || "Untitled")}</div>
<div class="marker-coords">${latlng.lat.toFixed(4)}, ${latlng.lng.toFixed(4)}</div>
`;
div.addEventListener("click", () => {
@@ -110,7 +110,7 @@
function getUploadUrl(path) {
if (!path) return "";
if (path.startsWith("http")) return path;
if (path.startsWith("http")) return sanitizeDisplayUrl(path);
return API_BASE.replace("/api", path);
}
@@ -121,8 +121,13 @@
images.forEach((image, index) => {
const item = document.createElement("div");
item.className = "image-preview";
const imagePath = typeof image === "string" ? image : image.url;
const imageName = typeof image === "string" ? "Marker image" : image.originalName;
const url = sanitizeDisplayUrl(getUploadUrl(imagePath));
const alt = escapeAttribute(imageName || "Marker image");
if (!url) return;
item.innerHTML = `
<img src="${getUploadUrl(image.url)}" alt="${image.originalName || "Marker image"}">
<img src="${escapeAttribute(url)}" alt="${alt}">
<button type="button" class="remove-image-btn" data-index="${index}" aria-label="Remove image">
<i class="fas fa-times"></i>
</button>
@@ -208,7 +213,7 @@
// Update marker's tooltip title and popup
activeMarker.options.title = title;
activeMarker.setPopupContent(`<strong>${title}</strong>`);
activeMarker.setPopupContent(`<strong>${escapeHtml(title)}</strong>`);
// Store content for saving
activeMarker._content = { title, date, description, images };