Skip to content

Commit 2f31bf9

Browse files
Merge pull request #16 from ImranullahKhann/add-security
added security features.
2 parents 0a0de91 + bbb58da commit 2f31bf9

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ coverage==6.3.2
2727

2828
# Utilities
2929
httpie==3.2.1
30+
31+
# security
32+
Flask-Talisman
33+
Flask-Cors

service/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
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)
17+
talisman = Talisman(app)
18+
CORS(app)
1519

1620
# Import the routes After the Flask app is created
1721
# pylint: disable=wrong-import-position, cyclic-import, wrong-import-order

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):
@@ -169,4 +172,24 @@ def test_get_account_list(self):
169172
def test_method_not_allowed(self):
170173
"""It should not allow an illegal method call"""
171174
resp = self.client.delete(BASE_URL)
172-
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
175+
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
176+
177+
def test_security_headers(self):
178+
"""It should return security headers"""
179+
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
180+
self.assertEqual(response.status_code, status.HTTP_200_OK)
181+
headers = {
182+
'X-Frame-Options': 'SAMEORIGIN',
183+
'X-Content-Type-Options': 'nosniff',
184+
'Content-Security-Policy': 'default-src \'self\'; object-src \'none\'',
185+
'Referrer-Policy': 'strict-origin-when-cross-origin'
186+
}
187+
for key, value in headers.items():
188+
self.assertEqual(response.headers.get(key), value)
189+
190+
def test_cors_security(self):
191+
"""It should return a CORS header"""
192+
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
193+
self.assertEqual(response.status_code, status.HTTP_200_OK)
194+
# Check for the CORS header
195+
self.assertEqual(response.headers.get('Access-Control-Allow-Origin'), '*')

0 commit comments

Comments
 (0)