2019-07-02 06:32:26 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
Process incoming requests to the client api server to validate they are legitimate
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2019-07-01 20:38:55 +00:00
|
|
|
import hmac
|
2019-08-13 22:28:53 +00:00
|
|
|
from flask import Blueprint, request, abort, g
|
2019-07-02 06:32:26 +00:00
|
|
|
from onionrservices import httpheaders
|
2019-08-14 23:44:29 +00:00
|
|
|
from . import pluginwhitelist
|
|
|
|
|
2019-07-01 20:38:55 +00:00
|
|
|
# Be extremely mindful of this. These are endpoints available without a password
|
2019-08-14 23:44:29 +00:00
|
|
|
whitelist_endpoints = ['www', 'staticfiles.homedata', 'staticfiles.sharedContent',
|
2019-10-08 21:14:30 +00:00
|
|
|
'staticfiles.friends', 'staticfiles.friendsindex', 'siteapi.site', 'staticfiles.onionrhome',
|
|
|
|
'themes.getTheme', 'staticfiles.onboarding', 'staticfiles.onboardingIndex']
|
2019-07-01 20:38:55 +00:00
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
class ClientAPISecurity:
|
|
|
|
def __init__(self, client_api):
|
|
|
|
client_api_security_bp = Blueprint('clientapisecurity', __name__)
|
|
|
|
self.client_api_security_bp = client_api_security_bp
|
|
|
|
self.client_api = client_api
|
2019-08-14 23:44:29 +00:00
|
|
|
pluginwhitelist.load_plugin_security_whitelist_endpoints(whitelist_endpoints)
|
2019-07-02 06:32:26 +00:00
|
|
|
|
|
|
|
@client_api_security_bp.before_app_request
|
|
|
|
def validate_request():
|
|
|
|
'''Validate request has set password and is the correct hostname'''
|
|
|
|
# For the purpose of preventing DNS rebinding attacks
|
|
|
|
if request.host != '%s:%s' % (client_api.host, client_api.bindPort):
|
2019-07-01 20:38:55 +00:00
|
|
|
abort(403)
|
2019-08-14 23:44:29 +00:00
|
|
|
|
|
|
|
# Add shared objects
|
|
|
|
try:
|
|
|
|
g.too_many = self.client_api._too_many
|
|
|
|
except KeyError:
|
|
|
|
g.too_many = None
|
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
if request.endpoint in whitelist_endpoints:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if not hmac.compare_digest(request.headers['token'], client_api.clientToken):
|
|
|
|
if not hmac.compare_digest(request.form['token'], client_api.clientToken):
|
|
|
|
abort(403)
|
|
|
|
except KeyError:
|
|
|
|
if not hmac.compare_digest(request.form['token'], client_api.clientToken):
|
|
|
|
abort(403)
|
2019-07-01 20:38:55 +00:00
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
@client_api_security_bp.after_app_request
|
|
|
|
def after_req(resp):
|
|
|
|
# Security headers
|
|
|
|
resp = httpheaders.set_default_onionr_http_headers(resp)
|
2019-08-09 20:07:32 +00:00
|
|
|
if request.endpoint == 'siteapi.site':
|
2019-07-02 06:32:26 +00:00
|
|
|
resp.headers['Content-Security-Policy'] = "default-src 'none'; style-src data: 'unsafe-inline'; img-src data:"
|
|
|
|
else:
|
2019-09-21 05:06:49 +00:00
|
|
|
resp.headers['Content-Security-Policy'] = "default-src 'none'; script-src 'self'; object-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; media-src 'none'; frame-src 'none'; font-src 'self'; connect-src 'self'"
|
2019-07-02 06:32:26 +00:00
|
|
|
return resp
|