Skip to content

Commit 53fbc02

Browse files
authored
chore: security improvements (#53)
* chore: add safe redirect to login Signed-off-by: Michal Fiedorowicz <[email protected]> * chore: gha - add missing permissions Signed-off-by: Michal Fiedorowicz <[email protected]> * chore: gha - pin actions to commit hashes Signed-off-by: Michal Fiedorowicz <[email protected]> * tidy up Signed-off-by: Michal Fiedorowicz <[email protected]> --------- Signed-off-by: Michal Fiedorowicz <[email protected]>
1 parent 5f44578 commit 53fbc02

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.github/workflows/lint-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: |
4848
make docker-compose-netbox-plugin-test-cover
4949
- name: Coverage comment
50-
uses: orgoro/[email protected]
50+
uses: orgoro/coverage@3f13a558c5af7376496aa4848bf0224aead366ac # v3.2
5151
with:
5252
coverageFile: ./docker/coverage/report.xml
5353
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/manifest-modified.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ concurrency:
1010
group: ${{ github.workflow }}
1111
cancel-in-progress: false
1212

13+
permissions:
14+
contents: read
15+
1316
jobs:
1417
manifest-modified:
1518
uses: netboxlabs/public-workflows/.github/workflows/reusable-plugin-manifest-modified.yml@release

.github/workflows/release.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
with:
4343
node-version: "lts/*"
4444
- name: Write package.json
45-
uses: DamianReeves/write-file-action@master
45+
uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
4646
with:
4747
path: ./package.json
4848
write-mode: overwrite
@@ -56,7 +56,7 @@ jobs:
5656
}
5757
}
5858
- name: Write .releaserc.json
59-
uses: DamianReeves/write-file-action@master
59+
uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
6060
with:
6161
path: ./.releaserc.json
6262
write-mode: overwrite
@@ -166,7 +166,7 @@ jobs:
166166
retention-days: 30
167167
if-no-files-found: error
168168
- name: Publish release distributions to PyPI
169-
uses: pypa/gh-action-pypi-publish@release/v1
169+
uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70 # v.1.12.3
170170
with:
171171
packages-dir: dist
172172

@@ -181,7 +181,7 @@ jobs:
181181
with:
182182
node-version: "lts/*"
183183
- name: Write package.json
184-
uses: DamianReeves/write-file-action@master
184+
uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
185185
with:
186186
path: ./package.json
187187
write-mode: overwrite
@@ -195,7 +195,7 @@ jobs:
195195
}
196196
}
197197
- name: Write .releaserc.json
198-
uses: DamianReeves/write-file-action@master
198+
uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
199199
with:
200200
path: ./.releaserc.json
201201
write-mode: overwrite

netbox_diode_plugin/views.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from django.contrib import messages
88
from django.contrib.auth import get_user, get_user_model
99
from django.core.cache import cache
10+
from django.http import HttpResponseRedirect
1011
from django.shortcuts import redirect, render
12+
from django.utils.http import url_has_allowed_host_and_scheme
1113
from django.views.generic import View
1214
from netbox.plugins import get_plugin_config
1315
from netbox.views import generic
@@ -28,6 +30,17 @@
2830
User = get_user_model()
2931

3032

33+
def redirect_to_login(request):
34+
"""Redirect to login view."""
35+
redirect_url = netbox_settings.LOGIN_URL
36+
target = request.path
37+
38+
if target and url_has_allowed_host_and_scheme(target, allowed_hosts=None):
39+
redirect_url = f"{netbox_settings.LOGIN_URL}?next={target}"
40+
41+
return HttpResponseRedirect(redirect_url)
42+
43+
3144
class IngestionLogsView(View):
3245
"""Ingestion logs view."""
3346

@@ -36,7 +49,7 @@ class IngestionLogsView(View):
3649
def get(self, request):
3750
"""Render ingestion logs template."""
3851
if not request.user.is_authenticated or not request.user.is_staff:
39-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
52+
return redirect_to_login(request)
4053

4154
netbox_to_diode_username = get_diode_username_for_user_type("netbox_to_diode")
4255
try:
@@ -118,7 +131,7 @@ class SettingsView(View):
118131
def get(self, request):
119132
"""Render settings template."""
120133
if not request.user.is_authenticated or not request.user.is_staff:
121-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
134+
return redirect_to_login(request)
122135

123136
diode_target_override = get_plugin_config(
124137
"netbox_diode_plugin", "diode_target_override"
@@ -187,7 +200,7 @@ class SettingsEditView(generic.ObjectEditView):
187200
def get(self, request, *args, **kwargs):
188201
"""GET request handler."""
189202
if not request.user.is_authenticated or not request.user.is_staff:
190-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
203+
return redirect_to_login(request)
191204

192205
diode_target_override = get_plugin_config(
193206
"netbox_diode_plugin", "diode_target_override"
@@ -207,7 +220,7 @@ def get(self, request, *args, **kwargs):
207220
def post(self, request, *args, **kwargs):
208221
"""POST request handler."""
209222
if not request.user.is_authenticated or not request.user.is_staff:
210-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
223+
return redirect_to_login(request)
211224

212225
diode_target_override = get_plugin_config(
213226
"netbox_diode_plugin", "diode_target_override"
@@ -272,7 +285,7 @@ def _retrieve_users(self):
272285
def get(self, request):
273286
"""GET request handler."""
274287
if not request.user.is_authenticated or not request.user.is_staff:
275-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
288+
return redirect_to_login(request)
276289

277290
users = self._retrieve_users()
278291

@@ -285,7 +298,7 @@ def get(self, request):
285298
def post(self, request):
286299
"""POST request handler."""
287300
if not request.user.is_authenticated or not request.user.is_staff:
288-
return redirect(f"{netbox_settings.LOGIN_URL}?next={request.path}")
301+
return redirect_to_login(request)
289302

290303
users = self._retrieve_users()
291304

0 commit comments

Comments
 (0)