Skip to content

Commit 7656ea9

Browse files
Added security and CORS headers
1 parent e273e1b commit 7656ea9

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ python-dotenv==0.20.0
1111
# Runtime dependencies
1212
gunicorn==20.1.0
1313
honcho==1.1.0
14+
flask-talisman==0.7.0
15+
flask-cors==5.0.1
1416

1517
# Code quality
1618
pylint==2.14.0

service/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
from flask import Flask
99
from service import config
1010
from service.common import log_handlers
11+
from flask_talisman import Talisman
12+
from flask_cors import CORS
1113

1214
# Create Flask application
1315
app = Flask(__name__)
1416
app.config.from_object(config)
1517

18+
talisman = Talisman(app)
19+
CORS(app)
20+
1621
# Import the routes After the Flask app is created
1722
# pylint: disable=wrong-import-position, cyclic-import, wrong-import-order
1823
from service import routes, models # noqa: F401 E402

tests/test_routes.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
from service.common import status # HTTP Status Codes
1313
from service.models import db, Account, init_db
1414
from service.routes import app
15+
from service import talisman
1516

1617
DATABASE_URI = os.getenv(
1718
"DATABASE_URI", "postgresql://postgres:postgres@localhost:5432/postgres"
1819
)
1920

2021
BASE_URL = "/accounts"
22+
HTTPS_ENVIRON = {'wsgi.url_scheme': 'https'}
2123

2224

2325
######################################################################
@@ -34,6 +36,7 @@ def setUpClass(cls):
3436
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
3537
app.logger.setLevel(logging.CRITICAL)
3638
init_db(app)
39+
talisman.force_https = False
3740

3841
@classmethod
3942
def tearDownClass(cls):
@@ -172,4 +175,24 @@ def test_delete_account(self):
172175
def test_method_not_allowed(self):
173176
"""It should not allow an illegal method call"""
174177
resp = self.client.delete(BASE_URL)
175-
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
178+
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
179+
180+
def test_security_headers(self):
181+
"""It should return security headers"""
182+
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
183+
self.assertEqual(response.status_code, status.HTTP_200_OK)
184+
headers = {
185+
'X-Frame-Options': 'SAMEORIGIN',
186+
'X-Content-Type-Options': 'nosniff',
187+
'Content-Security-Policy': 'default-src \'self\'',
188+
'Referrer-Policy': 'strict-origin-when-cross-origin'
189+
}
190+
for key, value in headers.items():
191+
self.assertEqual(response.headers.get(key), value)
192+
193+
def test_cors_security(self):
194+
"""It should return a CORS header"""
195+
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
196+
self.assertEqual(response.status_code, status.HTTP_200_OK)
197+
# Check for the CORS header
198+
self.assertEqual(response.headers.get('Access-Control-Allow-Origin'), '*')

0 commit comments

Comments
 (0)