Skip to content

Commit 33f7288

Browse files
JelteFFennemAI
andauthored
Only allow modifying patches through POST requests (#127)
Changing the status of a patch, closing or moving it, becoming reviewer or committer, and (un)subscribing were all done through plain links, i.e. GET requests. Crawlers follow all of those links on every patch page, and since they're not logged in each of them resulted in a redirect to the login page, generating a lot of useless load. It also meant that anyone could trick a logged in user into modifying a patch by getting them to click a link, since GET requests aren't protected against CSRF. These buttons are now forms that POST, with a CSRF token. A GET request to one of these URLs now redirects to the patch page without doing anything, also when not logged in, so crawlers that still have the old URLs don't end up at the login page. The staff-only `importUser` AJAX call, which creates a user, now also uses POST. Fixes #116 --------- Co-authored-by: Jelterminator FennemAI-Neo <fennemai@jeltef.nl>
1 parent cf6df2b commit 33f7288

6 files changed

Lines changed: 82 additions & 56 deletions

File tree

‎media/commitfest/js/commitfest.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ function flagCommitted(committer) {
260260
);
261261
return;
262262
}
263-
document.location.href = `close/committed/?c=${c}`;
263+
const form = $("#commitForm");
264+
form.attr("action", `close/committed/?c=${encodeURIComponent(c)}`);
265+
form.submit();
264266
});
265267
return false;
266268
}
@@ -317,7 +319,7 @@ function search_and_store_user() {
317319
}
318320

319321
/* Create this user locally */
320-
$.get("/ajax/importUser/", {
322+
$.post("/ajax/importUser/", {
321323
u: $("#searchUserList").val(),
322324
})
323325
.success((data) => {

‎pgcommitfest/commitfest/ajax.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ def importUser(request):
306306
if not request.user.is_staff:
307307
raise Http404()
308308

309-
if request.GET.get("u", ""):
310-
u = user_search(userid=request.GET["u"])
309+
if request.POST.get("u", ""):
310+
u = user_search(userid=request.POST["u"])
311311
if len(u) != 1:
312312
return "Internal error, duplicate user found"
313313

‎pgcommitfest/commitfest/templates/patch.html‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,20 @@
125125
<th>Reviewers</th>
126126
<td>
127127
{{patch.reviewers_string}}
128-
{%if is_reviewer%}<a href="reviewer/remove/" class="btn btn-secondary float-end">Remove from reviewers</a>{%else%}<a href="reviewer/become/" class="btn btn-secondary float-end">Become reviewer</a>{%endif%}
129-
{%if patch.can_remove_all_reviewers and patch.reviewers.all%}<a href="reviewer/remove_all/" class="btn btn-secondary float-end me-1">Remove all reviewers</a>{%endif%}
128+
{%if not user.is_authenticated%}
129+
<a class="btn btn-secondary float-end" href="/account/login/?next=/patch/{{patch.id}}/">Log in to become reviewer</a>
130+
{%else%}
131+
<form method="post">{%csrf_token%}
132+
{%if is_reviewer%}<button formaction="reviewer/remove/" class="btn btn-secondary float-end">Remove from reviewers</button>{%else%}<button formaction="reviewer/become/" class="btn btn-secondary float-end">Become reviewer</button>{%endif%}
133+
{%if patch.can_remove_all_reviewers and patch.reviewers.all%}<button formaction="reviewer/remove_all/" class="btn btn-secondary float-end me-1">Remove all reviewers</button>{%endif%}
134+
</form>
135+
{%endif%}
130136
</td>
131137
</tr>
132138
<tr>
133139
<th>Committer</th>
134140
<td>{%if patch.committer%}{{patch.committer.fullname}}{%endif%}
135-
{%if is_committer%}<a href="committer/{{is_this_committer|yesno:"remove,become"}}/" class="btn btn-secondary float-end">{{is_this_committer|yesno:"Unclaim as committer,Claim as committer"}}</a>{%endif%}
141+
{%if is_committer%}<form method="post" action="committer/{{is_this_committer|yesno:"remove,become"}}/">{%csrf_token%}<button class="btn btn-secondary float-end">{{is_this_committer|yesno:"Unclaim as committer,Claim as committer"}}</button></form>{%endif%}
136142
</td>
137143
</tr>
138144
<tr>
@@ -225,7 +231,7 @@ <h4>Annotations</h4>
225231
</table>
226232
</div>
227233
{%if user.is_authenticated%}
228-
<a href="{{is_subscribed|yesno:"unsubscribe,subscribe"}}/" class="btn btn-secondary">{{is_subscribed|yesno:"Unsubscribe from patch update emails,Subscribe to patch update emails"}}</a>
234+
<form method="post" action="{{is_subscribed|yesno:"unsubscribe,subscribe"}}/">{%csrf_token%}<button class="btn btn-secondary">{{is_subscribed|yesno:"Unsubscribe from patch update emails,Subscribe to patch update emails"}}</button></form>
229235
{%endif%}
230236
</td>
231237
</tr>
@@ -260,6 +266,7 @@ <h4 class="modal-title">Flag as committed</h4>
260266
<div class="modal-footer">
261267
<a href="#" class="btn btn-secondary" data-bs-dismiss="modal">Close</a>
262268
<a href="#" class="btn btn-primary" id="doCommitButton">Flag as committed</a>
269+
<form id="commitForm" method="post">{%csrf_token%}</form>
263270
</div>
264271
</div>
265272
</div>
Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,62 @@
1-
<div style="margin-bottom:5px;">
2-
<a class="btn btn-secondary" href="edit/">Edit</a>
3-
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#reviewModal">Review</button>
1+
<form method="post" style="margin-bottom:5px;">{%csrf_token%}
2+
{%if not user.is_authenticated%}
3+
{%comment%}
4+
Most of these commands are POSTs, which can't be replayed after logging in,
5+
so send anonymous users to log in first instead of letting them submit.
6+
{%endcomment%}
7+
<a class="btn btn-secondary" href="/account/login/?next=/patch/{{patch.id}}/">Log in to edit</a>
8+
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#reviewModal">Review</button>
9+
{%else%}
10+
<a class="btn btn-secondary" href="edit/">Edit</a>
11+
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#reviewModal">Review</button>
412

5-
<div class="btn-group">
6-
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Change Status</button>
7-
<ul class="dropdown-menu">
8-
<li><h6 class="dropdown-header">Open statuses</h6></li>
9-
<li><a class="dropdown-item" href="status/review/">Needs review</a></li>
10-
<li><a class="dropdown-item" href="status/author/">Waiting on Author</a></li>
11-
<li><a class="dropdown-item" href="status/committer/">Ready for Committer</a></li>
12-
<li><hr class="dropdown-divider"></li>
13-
<li><h6 class="dropdown-header">Closed statuses</h6></li>
14-
<li><a class="dropdown-item" href="close/reject/" onclick="return verify_reject()">Rejected</a></li>
15-
<li><a class="dropdown-item" href="close/withdrawn/" onclick="return verify_withdrawn()">Withdrawn</a></li>
16-
<li><a class="dropdown-item" href="close/feedback/" onclick="return verify_returned()">Returned with feedback</a></li>
17-
<li><a class="dropdown-item" href="close/committed/" onclick="return flagCommitted({%if patch.committer%}'{{patch.committer}}'{%elif is_committer%}'{{user.username}}'{%else%}null{%endif%})">Committed</a></li>
18-
<li><hr class="dropdown-divider"></li>
19-
<li><h6 class="dropdown-header">Move to</h6></li>
20-
{%if not cf.is_open_regular %}
21-
<li>
22-
<a class="dropdown-item"
23-
href="move/?from_cf_id={{cf.id}}&to_cf_id={{cfs.open.id}}">
24-
Next CF: {{cfs.open.name}}</a>
25-
</li>
26-
{%endif%}
27-
{%if not cf.is_open_draft %}
28-
<li>
29-
<a class="dropdown-item"
30-
href="move/?from_cf_id={{cf.id}}&to_cf_id={{cfs.draft.id}}">
31-
{%if cf.draft %}
32-
Next Drafts:
33-
{%else%}
34-
Draft:
35-
{%endif%}
36-
{{cfs.draft.name}}</a>
37-
</li>
38-
{%endif%}
39-
</ul>
40-
</div>
41-
42-
{%if request.user.is_staff%}
4313
<div class="btn-group">
44-
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Send private mail</button>
14+
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Change Status</button>
4515
<ul class="dropdown-menu">
46-
<li><a class="dropdown-item" href="send_email/?authors={{patch.id}}">Send mail to authors</a></li>
47-
<li><a class="dropdown-item" href="send_email/?reviewers={{patch.id}}">Send mail to reviewers</a></li>
48-
<li><a class="dropdown-item" href="send_email/?authors={{patch.id}}&reviewers={{patch.id}}">Send mail to authors and reviewers</a></li>
16+
<li><h6 class="dropdown-header">Open statuses</h6></li>
17+
<li><button class="dropdown-item" formaction="status/review/">Needs review</button></li>
18+
<li><button class="dropdown-item" formaction="status/author/">Waiting on Author</button></li>
19+
<li><button class="dropdown-item" formaction="status/committer/">Ready for Committer</button></li>
20+
<li><hr class="dropdown-divider"></li>
21+
<li><h6 class="dropdown-header">Closed statuses</h6></li>
22+
<li><button class="dropdown-item" formaction="close/reject/" onclick="return verify_reject()">Rejected</button></li>
23+
<li><button class="dropdown-item" formaction="close/withdrawn/" onclick="return verify_withdrawn()">Withdrawn</button></li>
24+
<li><button class="dropdown-item" formaction="close/feedback/" onclick="return verify_returned()">Returned with feedback</button></li>
25+
<li><button class="dropdown-item" type="button" onclick="return flagCommitted({%if patch.committer%}'{{patch.committer}}'{%elif is_committer%}'{{user.username}}'{%else%}null{%endif%})">Committed</button></li>
26+
<li><hr class="dropdown-divider"></li>
27+
<li><h6 class="dropdown-header">Move to</h6></li>
28+
{%if not cf.is_open_regular %}
29+
<li>
30+
<button class="dropdown-item"
31+
formaction="move/?from_cf_id={{cf.id}}&to_cf_id={{cfs.open.id}}">
32+
Next CF: {{cfs.open.name}}</button>
33+
</li>
34+
{%endif%}
35+
{%if not cf.is_open_draft %}
36+
<li>
37+
<button class="dropdown-item"
38+
formaction="move/?from_cf_id={{cf.id}}&to_cf_id={{cfs.draft.id}}">
39+
{%if cf.draft %}
40+
Next Drafts:
41+
{%else%}
42+
Draft:
43+
{%endif%}
44+
{{cfs.draft.name}}</button>
45+
</li>
46+
{%endif%}
4947
</ul>
5048
</div>
49+
50+
{%if request.user.is_staff%}
51+
<div class="btn-group">
52+
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Send private mail</button>
53+
<ul class="dropdown-menu">
54+
<li><a class="dropdown-item" href="send_email/?authors={{patch.id}}">Send mail to authors</a></li>
55+
<li><a class="dropdown-item" href="send_email/?reviewers={{patch.id}}">Send mail to reviewers</a></li>
56+
<li><a class="dropdown-item" href="send_email/?authors={{patch.id}}&reviewers={{patch.id}}">Send mail to authors and reviewers</a></li>
57+
</ul>
58+
</div>
59+
{%endif%}
5160
{%endif%}
5261

53-
</div>
62+
</form>

‎pgcommitfest/commitfest/tests/test_remove_reviewer.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_remove_all_reviewers(client, open_cf, alice, bob, charlie):
3232
)
3333

3434
client.force_login(alice)
35-
response = client.get(f"/patch/{patch.id}/reviewer/remove_all/")
35+
response = client.post(f"/patch/{patch.id}/reviewer/remove_all/")
3636
assert response.status_code == 302
3737
assert list(patch.reviewers.all()) == []
3838
assert (
@@ -54,6 +54,6 @@ def test_remove_all_reviewers_forbidden_without_tag(client, open_cf, alice, bob)
5454
)
5555

5656
client.force_login(alice)
57-
response = client.get(f"/patch/{patch.id}/reviewer/remove_all/")
57+
response = client.post(f"/patch/{patch.id}/reviewer/remove_all/")
5858
assert response.status_code == 403
5959
assert bob in patch.reviewers.all()

‎pgcommitfest/commitfest/views.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.shortcuts import get_object_or_404, render
1616
from django.utils import timezone
1717
from django.views.decorators.csrf import csrf_exempt
18+
from django.views.decorators.http import require_POST
1819

1920
import collections
2021
import functools
@@ -1113,6 +1114,7 @@ def newpatch(request, cfid):
11131114
)
11141115

11151116

1117+
@require_POST
11161118
@login_required
11171119
@transaction.atomic
11181120
def status(request, patchid, status):
@@ -1141,6 +1143,7 @@ def status(request, patchid, status):
11411143
return HttpResponseRedirect("/patch/%s/" % (poc.patch.id))
11421144

11431145

1146+
@require_POST
11441147
@login_required
11451148
@transaction.atomic
11461149
def close(request, patchid, status):
@@ -1235,6 +1238,7 @@ def int_param_or_none(request, param):
12351238
return None
12361239

12371240

1241+
@require_POST
12381242
@login_required
12391243
@transaction.atomic
12401244
def move(request, patchid):
@@ -1260,6 +1264,7 @@ def move(request, patchid):
12601264
return HttpResponseRedirect(f"/patch/{patchid}/")
12611265

12621266

1267+
@require_POST
12631268
@login_required
12641269
@transaction.atomic
12651270
def reviewer(request, patchid, status):
@@ -1286,6 +1291,7 @@ def reviewer(request, patchid, status):
12861291
return HttpResponseRedirect("../../")
12871292

12881293

1294+
@require_POST
12891295
@login_required
12901296
@transaction.atomic
12911297
def remove_all_reviewers(request, patchid):
@@ -1311,6 +1317,7 @@ def remove_all_reviewers(request, patchid):
13111317
return HttpResponseRedirect(f"/patch/{patchid}/")
13121318

13131319

1320+
@require_POST
13141321
@login_required
13151322
@transaction.atomic
13161323
def committer(request, patchid, status):
@@ -1344,6 +1351,7 @@ def committer(request, patchid, status):
13441351
return HttpResponseRedirect("../../")
13451352

13461353

1354+
@require_POST
13471355
@login_required
13481356
@transaction.atomic
13491357
def subscribe(request, patchid, sub):

0 commit comments

Comments
 (0)