Skip to content

Commit 141e064

Browse files
dcschreiberclaude
andcommitted
fix(modtools): add client-side CSRF token validation for CSV upload
Add explicit CSRF token check before sending the upload request, and handle 403 responses with a user-friendly message suggesting a page refresh. Complements the server-side @ensure_csrf_cookie fix. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3613afe commit 141e064

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

static/js/modtools/components/BulkUploadCSV.jsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,28 @@ function BulkUploadCSV() {
9494
}
9595

9696
try {
97+
const csrfToken = Cookies.get('csrftoken');
98+
if (!csrfToken) {
99+
setUploadError("Error - CSRF token not found. Try refreshing the page.");
100+
setUploading(false);
101+
return;
102+
}
103+
97104
const response = await fetch('/api/text-upload', {
98105
method: 'POST',
99106
headers: {
100-
'X-CSRFToken': Cookies.get('csrftoken')
107+
'X-CSRFToken': csrfToken
101108
},
102109
credentials: 'same-origin',
103110
body: formData
104111
});
105112

113+
if (response.status === 403) {
114+
setUploadError("Error - Permission denied (403). Try refreshing the page to get a new CSRF token.");
115+
setUploading(false);
116+
return;
117+
}
118+
106119
const data = await response.json();
107120

108121
if (data.status === "ok") {

0 commit comments

Comments
 (0)