Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
beeMapsoftwebinc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Softweb Inc
beeMapsoftwebinc
Merge requests
!58
Csrf prevention
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Csrf prevention
backend-csrf
into
staging
Overview
0
Commits
2
Pipelines
1
Changes
8
Merged
roarusko
requested to merge
backend-csrf
into
staging
4 years ago
Overview
0
Commits
2
Pipelines
1
Changes
8
Expand
Added simple csrf prevention to fix issue
#5 (closed)
:
The server generates a random, unique csrf token, stores it in the session and appends the token on the map and admin pages
The token is added to every POST, PUT and DELETE request inside 'x-csrf-token' header
Likewise, for every POST, PUT and DELETE request, the server checks that the token stored in the session matches the token from the header
Edited
4 years ago
by
roarusko
0
0
Merge request reports
Compare
staging
staging (base)
and
latest version
latest version
586f48f8
2 commits,
4 years ago
8 files
+
61
−
23
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
8
Search (e.g. *.vue) (Ctrl+P)
api/services/csrf.py
0 → 100644
+
35
−
0
Options
from
flask
import
session
,
request
,
Response
from
uuid
import
uuid4
def
checkCsrfToken
():
"""
Checks that a valid csrf token exists in requests that can alter data
"""
# no csrf check needed for static requests
if
request
.
endpoint
==
'
static
'
:
return
None
# check if a token exists in session
sessionToken
=
session
.
get
(
'
csrf
'
)
# issue new one if not
if
not
sessionToken
:
print
(
"
issuing new token
"
)
return
issueToken
()
# no csrf check needed for get requests
if
request
.
method
==
'
GET
'
:
return
None
# get token from request
headerToken
=
request
.
headers
.
get
(
'
x-csrf-token
'
)
# check that the two tokens match
if
headerToken
!=
sessionToken
:
return
Response
(
"
Invalid csrf key
"
,
403
)
def
issueToken
():
"""
Generates a new CSRF token and stores it for current session
"""
# issue new if not
sessionToken
=
str
(
uuid4
())
session
[
'
csrf
'
]
=
sessionToken
Loading