Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
common-web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
protokollsystem
common-web
Commits
c7eac74c
Commit
c7eac74c
authored
7 years ago
by
Robin Sonnabend
Browse files
Options
Downloads
Patches
Plain Diff
Add decorators db_lookup and protect_csrf
parent
3dfc2b71
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
csrf.py
+21
-0
21 additions, 0 deletions
csrf.py
database.py
+42
-0
42 additions, 0 deletions
database.py
with
63 additions
and
0 deletions
csrf.py
0 → 100644
+
21
−
0
View file @
c7eac74c
from
functools
import
wraps
from
hmac
import
compare_digest
from
flask
import
request
,
abort
,
session
def
get_csrf_token
():
if
"
_csrf
"
not
in
session
:
session
[
"
_csrf
"
]
=
hashlib
.
sha1
(
os
.
urandom
(
64
)).
hexdigest
()
return
session
[
"
_csrf
"
]
def
protect_csrf
(
function
):
@wraps
(
function
)
def
_decorated_function
(
*
args
,
**
kwargs
):
token
=
request
.
args
.
get
(
"
csrf_token
"
)
true_token
=
get_csrf_token
()
if
token
is
None
or
not
compare_digest
(
token
,
true_token
):
abort
(
400
)
return
function
(
*
args
,
**
kwargs
)
return
_decorated_function
This diff is collapsed.
Click to expand it.
database.py
0 → 100644
+
42
−
0
View file @
c7eac74c
from
flask
import
flash
from
functools
import
wraps
from
.
import
back
ID_KEY
=
"
id
"
KEY_NOT_PRESENT_MESSAGE
=
"
Missing {}_id.
"
OBJECT_DOES_NOT_EXIST_MESSAGE
=
"
There is no {} with id {}.
"
def
default_redirect
():
return
back
.
redirect
()
def
login_redirect
():
return
back
.
redirect
(
"
login
"
)
def
db_lookup
(
*
models
,
check_exists
=
True
):
def
_decorator
(
function
):
@wraps
(
function
)
def
_decorated_function
(
*
args
,
**
kwargs
):
for
model
in
models
:
key
=
model
.
__model_name__
id_key
=
"
{}_{}
"
.
format
(
key
,
ID_KEY
)
if
id_key
not
in
kwargs
:
flash
(
KEY_NOT_PRESENT_MESSAGE
.
format
(
key
),
"
alert-error
"
)
return
default_redirect
()
obj_id
=
kwargs
[
id_key
]
obj
=
model
.
query
.
filter_by
(
id
=
obj_id
).
first
()
if
check_exists
and
obj
is
None
:
model_name
=
model
.
__class__
.
__name__
flash
(
OBJECT_DOES_NOT_EXIST_MESSAGE
.
format
(
model_name
,
obj_id
),
"
alert-error
"
)
return
default_redirect
()
kwargs
[
key
]
=
obj
kwargs
.
pop
(
id_key
)
return
function
(
*
args
,
**
kwargs
)
return
_decorated_function
return
_decorator
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment