本文整理匯總了Python中flask_talisman.Talisman方法的典型用法代碼示例。如果您正苦於以下問題:Python flask_talisman.Talisman方法的具體用法?Python flask_talisman.Talisman怎麽用?Python flask_talisman.Talisman使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類flask_talisman
的用法示例。
在下文中一共展示了flask_talisman.Talisman方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testFeaturePolicy
# 需要導入模塊: import flask_talisman [as 別名]
# 或者: from flask_talisman import Talisman [as 別名]
def testFeaturePolicy(self):
self.talisman.feature_policy['geolocation'] = '\'none\''
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
feature_policy = response.headers['Feature-Policy']
self.assertIn('geolocation \'none\'', feature_policy)
self.talisman.feature_policy['fullscreen'] = '\'self\' example.com'
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
feature_policy = response.headers['Feature-Policy']
self.assertIn('fullscreen \'self\' example.com', feature_policy)
# string policy at initialization
app = flask.Flask(__name__)
Talisman(app, feature_policy='vibrate \'none\'')
response = app.test_client().get('/', environ_overrides=HTTPS_ENVIRON)
self.assertIn('vibrate \'none\'', response.headers['Feature-Policy'])
示例2: setUp
# 需要導入模塊: import flask_talisman [as 別名]
# 或者: from flask_talisman import Talisman [as 別名]
def setUp(self):
self.app = flask.Flask(__name__)
self.talisman = Talisman(self.app)
self.client = self.app.test_client()
self.app.route('/')(hello_world)
self.app.route('/with_nonce')(with_nonce)
示例3: testContentSecurityPolicyOptions
# 需要導入模塊: import flask_talisman [as 別名]
# 或者: from flask_talisman import Talisman [as 別名]
def testContentSecurityPolicyOptions(self):
self.talisman.content_security_policy['image-src'] = '*'
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
csp = response.headers['Content-Security-Policy']
self.assertEqual(csp, "default-src 'self'; image-src *")
self.talisman.content_security_policy['image-src'] = [
'\'self\'',
'example.com'
]
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
csp = response.headers['Content-Security-Policy']
self.assertIn('default-src \'self\'', csp)
self.assertIn('image-src \'self\' example.com', csp)
# string policy
self.talisman.content_security_policy = 'default-src \'foo\' spam.eggs'
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertEqual(response.headers['Content-Security-Policy'],
'default-src \'foo\' spam.eggs')
# no policy
self.talisman.content_security_policy = False
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertNotIn('Content-Security-Policy', response.headers)
# string policy at initialization
app = flask.Flask(__name__)
Talisman(app, content_security_policy='default-src \'foo\' spam.eggs')
response = app.test_client().get('/', environ_overrides=HTTPS_ENVIRON)
self.assertIn(
'default-src \'foo\' spam.eggs',
response.headers['Content-Security-Policy']
)
示例4: testContentSecurityPolicyOptionsReport
# 需要導入模塊: import flask_talisman [as 別名]
# 或者: from flask_talisman import Talisman [as 別名]
def testContentSecurityPolicyOptionsReport(self):
# report-only policy
self.talisman.content_security_policy_report_only = True
self.talisman.content_security_policy_report_uri = \
'https://example.com'
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertIn('Content-Security-Policy-Report-Only', response.headers)
self.assertIn(
'X-Content-Security-Policy-Report-Only', response.headers)
self.assertIn(
'report-uri',
response.headers['Content-Security-Policy-Report-Only']
)
self.assertNotIn('Content-Security-Policy', response.headers)
self.assertNotIn('X-Content-Security-Policy', response.headers)
override_report_uri = 'https://report-uri.io/'
self.talisman.content_security_policy = {
'report-uri': override_report_uri,
}
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertIn(
'Content-Security-Policy-Report-Only', response.headers)
self.assertIn(
override_report_uri,
response.headers['Content-Security-Policy-Report-Only']
)
# exception on missing report-uri when report-only
self.assertRaises(ValueError, Talisman, self.app,
content_security_policy_report_only=True)
示例5: build_flask_app
# 需要導入模塊: import flask_talisman [as 別名]
# 或者: from flask_talisman import Talisman [as 別名]
def build_flask_app(project_name, app_name, openapi):
"""
Create a new Flask backend application
app_name is the Python application name, used as Flask import_name
project_name is a "nice" name, used to identify the application
"""
assert os.path.exists(openapi), "Missing openapi file {}".format(openapi)
logger.debug("Initializing", app=app_name, openapi=openapi)
# Start OpenAPI app
app = connexion.App(import_name=app_name)
app.name = project_name
app.add_api(openapi)
# Enable security
security = flask_talisman.Talisman()
security.init_app(app.app, **TALISMAN_CONFIG)
# Enable wildcard CORS
cors = flask_cors.CORS()
cors.init_app(app.app, origins=["*"])
# Add exception Json renderer
for code, exception in werkzeug.exceptions.default_exceptions.items():
app.app.register_error_handler(exception, handle_default_exceptions)
# Redirect root to API
app.add_url_rule(
"/", "root", lambda: flask.redirect(app.options.openapi_console_ui_path)
)
# Dockerflow checks
app.add_url_rule("/__heartbeat__", view_func=heartbeat_response)
app.add_url_rule("/__lbheartbeat__", view_func=lbheartbeat_response)
app.add_url_rule("/__version__", view_func=get_version)
logger.debug("Initialized", app=app.name)
return app