Skip to content

Commit 54ec053

Browse files
committed
Restrict modifications to staff users authed via session
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 217a466 commit 54ec053

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

vulnerabilities/api_v2.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from rest_framework import serializers
1919
from rest_framework import status
2020
from rest_framework import viewsets
21+
from rest_framework.authentication import SessionAuthentication
2122
from rest_framework.decorators import action
22-
from rest_framework.permissions import IsAdminUser
23+
from rest_framework.permissions import BasePermission
2324
from rest_framework.response import Response
2425
from rest_framework.reverse import reverse
2526

@@ -628,6 +629,17 @@ class CreateListRetrieveUpdateViewSet(
628629
pass
629630

630631

632+
class IsAdminWithSessionAuth(BasePermission):
633+
"""Permit only staff users authenticated via session (not token)."""
634+
635+
def has_permission(self, request, view):
636+
is_authenticated = request.user and request.user.is_authenticated
637+
is_staff = request.user and request.user.is_staff
638+
is_session_auth = isinstance(request.successful_authenticator, SessionAuthentication)
639+
640+
return is_authenticated and is_staff and is_session_auth
641+
642+
631643
class PipelineRunAPISerializer(serializers.HyperlinkedModelSerializer):
632644
status = serializers.SerializerMethodField()
633645
execution_time = serializers.SerializerMethodField()
@@ -653,7 +665,8 @@ def get_status(self, run):
653665
return run.status
654666

655667
def get_execution_time(self, run):
656-
return round(run.execution_time, 2)
668+
if run.execution_time:
669+
return round(run.execution_time, 2)
657670

658671
def get_log(self, run):
659672
"""Return only last 5000 character of log."""
@@ -719,7 +732,7 @@ def get_serializer_class(self):
719732
return super().get_serializer_class()
720733

721734
def get_permissions(self):
722-
"""Restrict modifications to admin users."""
735+
"""Restrict addition and modifications to staff users authenticated via session."""
723736
if self.action not in ["list", "retrieve"]:
724-
return [IsAdminUser()]
737+
return [IsAdminWithSessionAuth()]
725738
return super().get_permissions()

vulnerabilities/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,12 @@ def pipeline_class(self):
21972197
if self.pipeline_id in IMPORTERS_REGISTRY:
21982198
return IMPORTERS_REGISTRY.get(self.pipeline_id)
21992199

2200+
@property
2201+
def description(self):
2202+
"""Return the pipeline class."""
2203+
if self.pipeline_class:
2204+
return self.pipeline_class.__doc__
2205+
22002206
@property
22012207
def all_runs(self):
22022208
"""Return all the previous run instances for this pipeline."""
@@ -2208,14 +2214,14 @@ def latest_run(self):
22082214

22092215
@property
22102216
def earliest_run(self):
2211-
return self.pipelineruns.earliest("created_date") if self.pipelineruns.exists() else None
2217+
return self.pipelineruns.earliest("run_start_date") if self.pipelineruns.exists() else None
22122218

22132219
@property
22142220
def latest_run_date(self):
22152221
if not self.pipelineruns.exists():
22162222
return
2217-
latest_run = self.pipelineruns.values("created_date").first()
2218-
return latest_run["created_date"]
2223+
latest_run = self.pipelineruns.values("run_start_date").first()
2224+
return latest_run["run_start_date"]
22192225

22202226
@property
22212227
def next_run_date(self):

vulnerablecode/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@
215215
# Django restframework
216216

217217
REST_FRAMEWORK = {
218-
"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework.authentication.TokenAuthentication",),
218+
"DEFAULT_AUTHENTICATION_CLASSES": (
219+
"rest_framework.authentication.SessionAuthentication",
220+
"rest_framework.authentication.TokenAuthentication",
221+
),
219222
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
220223
"DEFAULT_RENDERER_CLASSES": (
221224
"rest_framework.renderers.JSONRenderer",

0 commit comments

Comments
 (0)