Skip to content

Commit 9667bff

Browse files
authored
Merge pull request #115 from c4dt/block_admin_changes
Don't let non-form-owner add voters
2 parents f0fabf1 + a34cf3a commit 9667bff

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

scripts/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
formid.env

scripts/local_forms.sh

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
66
echo "add form"
77
RESP=$(curl -sk "$FRONTEND_URL/api/evoting/forms" -X POST -H 'Content-Type: application/json' -b cookies.txt --data-raw $'{"Configuration":{"Title":{"En":"Colours","Fr":"","De":""},"Scaffold":[{"ID":"A7GsJxVJ","Title":{"En":"Colours","Fr":"","De":""},"Order":["GhidLIfw"],"Ranks":[],"Selects":[{"ID":"GhidLIfw","Title":{"En":"RGB","Fr":"","De":"RGB"},"MaxN":3,"MinN":1,"Choices":["{\\"en\\":\\"Red\\",\\"de\\":\\"Rot\\"}","{\\"en\\":\\"Green\\",\\"de\\":\\"Gr\xfcn\\"}","{\\"en\\":\\"Blue\\",\\"de\\":\\"Blau\\"}"],"Hint":{"En":"","Fr":"","De":"RGB"}}],"Texts":[],"Subjects":[]}]}}')
88
FORMID=$(echo "$RESP" | jq -r .FormID)
9+
echo "FORMID=$FORMID" > "$SCRIPT_DIR/formid.env"
910

1011
echo "add permissions - it's normal to have a timeout error after this command"
1112
curl -k "$FRONTEND_URL/api/evoting/authorizations" -X PUT -H 'Content-Type: application/json' -b cookies.txt --data "$(jq -cn --arg FormID $FORMID '$ARGS.named')" -m 1
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# This script tests that an admin who is not the owner of a form
4+
# cannot add voters to the form.
5+
# It also tests that the admin who created the form can actually add
6+
# voters to the form.
7+
8+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
9+
"$SCRIPT_DIR/run_local.sh"
10+
11+
. "$SCRIPT_DIR/local_vars.sh"
12+
SECOND_ADMIN=123321
13+
echo "Adding $SECOND_ADMIN to admin"
14+
(cd web/backend && npx ts-node src/cli.ts addAdmin --sciper $SECOND_ADMIN | grep -v Executing)
15+
16+
"$SCRIPT_DIR/local_proxies.sh"
17+
"$SCRIPT_DIR/local_forms.sh"
18+
19+
. "$SCRIPT_DIR/formid.env"
20+
21+
tmp_dir=$(mktemp -d)
22+
trap 'rm -rf -- "tmpdir"' EXIT
23+
24+
tmp_cookie_owner="$tmp_dir/cookie_owner"
25+
curl -k "$FRONTEND_URL/api/get_dev_login/$REACT_APP_SCIPER_ADMIN" -X GET -c "$tmp_cookie_owner" -o /dev/null -s
26+
tmp_cookie_nonowner="$tmp_dir/cookie_nonowner"
27+
curl -k "$FRONTEND_URL/api/get_dev_login/$SECOND_ADMIN" -X GET -c "$tmp_cookie_nonowner" -o /dev/null -s
28+
29+
echo "This should fail with an error that we're not allowed"
30+
tmp_output="$tmp_dir/output"
31+
curl -s 'http://localhost:3000/api/add_role' \
32+
-H 'Content-Type: application/json' \
33+
--data-raw "{\"userId\":444555,\"subject\":\"$FORMID\",\"permission\":\"vote\"}" \
34+
-b "$tmp_cookie_nonowner" 2>&1 | tee "$tmp_output"
35+
echo
36+
37+
if ! grep -q "not owner of form" "$tmp_output"; then
38+
echo
39+
echo "ERROR: Reply should be 'not owner of form'"
40+
exit 1
41+
fi
42+
43+
echo "This should pass for the owner of the form"
44+
curl 'http://localhost:3000/api/add_role' \
45+
-H 'Content-Type: application/json' \
46+
--data-raw "{\"userId\":444555,\"subject\":\"$FORMID\",\"permission\":\"vote\"}" \
47+
-b "$tmp_cookie_owner"
48+
echo

web/backend/src/controllers/users.ts

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ usersRouter.post('/add_role', (req, res, next) => {
2828
return;
2929
}
3030

31+
if (req.body.permission === 'vote') {
32+
if (!isAuthorized(req.session.userId, req.body.subject, PERMISSIONS.ACTIONS.OWN)) {
33+
res.status(400).send('Unauthorized - not owner of form');
34+
}
35+
}
36+
3137
addPolicy(req.body.userId, req.body.subject, req.body.permission)
3238
.then(() => {
3339
res.set(200).send();

0 commit comments

Comments
 (0)