Skip to content

Commit

Permalink
bug fixes, upgrade deps, security fixes (#75)
Browse files Browse the repository at this point in the history
* Update README.md

* Update CONTRIBUTING.md

* fixes and more strict security headers
  • Loading branch information
eshaan7 authored Jul 10, 2020
1 parent a426638 commit 1f5634e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ venv/
*.pyc
.vscode/
*.db
.idea/
.idea/
.github/
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ install:
- "pip install -r src/requirements.txt"
- "python src/create_db.test.py"

before_script:
- black . --check
script:
- black . --check
- flake8 . --count --max-line-length=88 --show-source --statistics
4 changes: 0 additions & 4 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
}
],
"env": {
"SECRET_KEY": {
"description": "Flask app instance's SECRET_KEY",
"generator": "secret"
},
"ADMIN_PASS": {
"description": "Administrator password",
"generator": "secret"
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ services:
- "8000"
environment:
- DEBUG=False
- SECRET_KEY=changeme
- SSL_ENABLED=False
- DB_USER=eshaan
- DB_PASSWORD=eshaan
- DB_NAME=rtbctf
- DB_PORT=5432
- WORKERS=8
- WORKERS=4
- ADMIN_PASS=admin
depends_on:
- postgres
Expand Down
2 changes: 2 additions & 0 deletions src/FlaskRTBCTF/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
admin_manager,
mail,
inject_app_context,
inject_security_headers,
)
from FlaskRTBCTF.users.routes import users
from FlaskRTBCTF.ctf.routes import ctf
Expand All @@ -26,6 +27,7 @@ def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
app.context_processor(inject_app_context)
app.after_request(inject_security_headers)

for _ext in _extensions:
_ext.init_app(app)
Expand Down
13 changes: 9 additions & 4 deletions src/FlaskRTBCTF/config.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import os

from .utils import handle_secret_key
import secrets

# Flask related Configurations
# Note: DO NOT FORGET TO CHANGE 'SECRET_KEY' !


class Config:
DEBUG = True # Turn DEBUG OFF before deployment
SECRET_KEY = handle_secret_key()
DEBUG = False # Turn DEBUG OFF before deployment
SECRET_KEY = secrets.token_hex(16)
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or "sqlite:///site.db"
# For local use, one can simply use SQLlite with: 'sqlite:///site.db'
# For deployment on Heroku use: `os.environ.get('DATABASE_URL')`
# in all other cases: `os.environ.get('SQLALCHEMY_DATABASE_URI')`
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASK_ADMIN_SWATCH = ("journal", "paper", "yeti", "cosmo")[3]
# TEMPLATES_AUTO_RELOAD = True
# Session handling
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Strict"
SESSION_COOKIE_SECURE = (
True if os.environ.get("SSL_ENABLED", False) == "True" else False
)
MAIL_SERVER = "smtp.googlemail.com"
MAIL_PORT = 587
MAIL_USE_TLS = True
Expand Down
2 changes: 1 addition & 1 deletion src/FlaskRTBCTF/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def setup(self):
try:
settings = Settings.query.get(1)

settings.dummy = False
settings.ctf_name = self.ctf_name.data
settings.organization_name = self.organization_name.data
settings.from_date = self.from_date.data
settings.from_time = self.from_time.data
settings.to_date = self.to_date.data
settings.to_time = self.to_time.data
settings.dummy = False

db.session.commit()

Expand Down
10 changes: 8 additions & 2 deletions src/FlaskRTBCTF/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Favicon -->
<link rel='shortcut icon' type='image/x-icon' href="{{ url_for('static', filename='favicon.ico')}}"/>
<link rel="shortcut icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico')}}"/>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!-- Font Awesome -->
Expand Down Expand Up @@ -88,7 +88,13 @@ <h3>{{ settings.organization_name }}</h3>
<p class="text-muted">
<ul class="list-group">
{% for w in websites %}
<a target="_blank" href="{{ w.url }}" class="list-group-item list-group-item-action">{{ w.name }}</a>
<a target="_blank"
href="{{ w.url }}"
class="list-group-item list-group-item-action"
rel="noreferrer noopener"
>
{{ w.name }}
</a>
{% endfor %}
</ul>
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/FlaskRTBCTF/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from .helpers import (
handle_admin_pass,
handle_admin_email,
handle_secret_key,
is_past_running_time,
inject_app_context,
inject_security_headers,
clear_points_cache,
clear_rating_cache,
)
Expand Down
16 changes: 8 additions & 8 deletions src/FlaskRTBCTF/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
from ..main.models import Settings, Website


def handle_secret_key(default="you-will-never-guess"):
sk = os.environ.get("SECRET_KEY", default)
if not sk:
sk = secrets.token_hex(16)
os.environ["SECRET_KEY"] = sk
return sk


def handle_admin_pass(default="admin"):
passwd = os.environ.get("ADMIN_PASS", default)
if not passwd:
Expand All @@ -40,6 +32,14 @@ def inject_app_context():
return dict(settings=settings, websites=websites)


def inject_security_headers(response):
response.headers["X-Frame-Options"] = "SAMEORIGIN"
# response.headers["Content-Security-Policy"] = "default-src 'self'"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-XSS-Protection"] = "1; mode=block"
return response


@cache.cached(timeout=60, key_prefix="past_running_time")
def is_past_running_time():
end_date_time = Settings.get_settings().running_time_to
Expand Down
8 changes: 3 additions & 5 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
bcrypt==3.1.7
black==19.10b0
blinker==1.4
cffi==1.14.0
Expand All @@ -8,7 +7,7 @@ Flask-Admin==1.5.6
Flask-Bcrypt==0.7.1
Flask-Login==0.5.0
Flask-Mail==0.9.1
Flask-SQLAlchemy==2.4.1
Flask-SQLAlchemy==2.4.3
Flask-SSLify==0.1.5
Flask-WTF==0.14.3
flake8==3.7.9
Expand All @@ -20,9 +19,8 @@ psycopg2==2.8.5
pycparser==2.20
pytz==2019.3
six==1.14.0
SQLAlchemy==1.3.16
Werkzeug==1.0.1
WTForms==2.2.1
tablib==1.1.0
Flask-Caching==1.8.0
redis==3.4.1
Flask-Caching==1.9.0
redis==3.5.3

0 comments on commit 1f5634e

Please sign in to comment.