Skip to content

Commit

Permalink
Set CORS headers only if set in config
Browse files Browse the repository at this point in the history
  • Loading branch information
python273 committed Oct 26, 2019
1 parent 99f6758 commit 5643f9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pwnagotchi/defaults.yml
Expand Up @@ -211,7 +211,7 @@ ui:
video:
enabled: true
address: '0.0.0.0'
origin: '*'
origin: null
port: 8080
# command to be executed when a new png frame is available
# for instance, to use with framebuffer based displays:
Expand Down
33 changes: 17 additions & 16 deletions pwnagotchi/ui/web.py
Expand Up @@ -75,7 +75,7 @@ def update_frame(img):


class Handler(BaseHTTPRequestHandler):
AllowedOrigin = '*'
AllowedOrigin = None # CORS headers are not sent

# suppress internal logging
def log_message(self, format, *args):
Expand All @@ -88,12 +88,13 @@ def _send_cors_headers(self):
self.send_header("X-XSS-Protection", "1; mode=block")
self.send_header("Referrer-Policy", "same-origin")
# cors
self.send_header("Access-Control-Allow-Origin", Handler.AllowedOrigin)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
self.send_header("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
self.send_header("Vary", "Origin")
if Handler.AllowedOrigin:
self.send_header("Access-Control-Allow-Origin", Handler.AllowedOrigin)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
self.send_header("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
self.send_header("Vary", "Origin")

# just render some html in a 200 response
def _html(self, html):
Expand Down Expand Up @@ -132,15 +133,18 @@ def _image(self):

# check the Origin header vs CORS
def _is_allowed(self):
if not Handler.AllowedOrigin or Handler.AllowedOrigin == '*':
return True

# TODO: FIX doesn't work with GET requests same-origin
origin = self.headers.get('origin')
if not origin and Handler.AllowedOrigin != '*':
if not origin:
logging.warning("request with no Origin header from %s" % self.address_string())
return False

if Handler.AllowedOrigin != '*':
if origin != Handler.AllowedOrigin:
logging.warning("request with blocked Origin from %s: %s" % (self.address_string(), origin))
return False
if origin != Handler.AllowedOrigin:
logging.warning("request with blocked Origin from %s: %s" % (self.address_string(), origin))
return False

return True

Expand Down Expand Up @@ -186,11 +190,8 @@ def __init__(self, config):
self._address = config['video']['address']
self._httpd = None

if 'origin' in config['video'] and config['video']['origin'] != '*':
if 'origin' in config['video']:
Handler.AllowedOrigin = config['video']['origin']
else:
logging.warning("THE WEB UI IS RUNNING WITH ALLOWED ORIGIN SET TO *, READ WHY YOU SHOULD CHANGE IT HERE " +
"https://developer.mozilla.org/it/docs/Web/HTTP/CORS")

if self._enabled:
_thread.start_new_thread(self._http_serve, ())
Expand Down
6 changes: 3 additions & 3 deletions pwnagotchi/utils.py
Expand Up @@ -79,7 +79,7 @@ def load_config(args):
elif config['ui']['display']['type'] in ('papirus', 'papi'):
config['ui']['display']['type'] = 'papirus'

elif config['ui']['display']['type'] in ('oledhat'):
elif config['ui']['display']['type'] in ('oledhat',):
config['ui']['display']['type'] = 'oledhat'

elif config['ui']['display']['type'] in ('ws_1', 'ws1', 'waveshare_1', 'waveshare1'):
Expand All @@ -91,9 +91,9 @@ def load_config(args):
elif config['ui']['display']['type'] in ('ws_27inch', 'ws27inch', 'waveshare_27inch', 'waveshare27inch'):
config['ui']['display']['type'] = 'waveshare27inch'

elif config['ui']['display']['type'] in ('lcdhat'):
elif config['ui']['display']['type'] in ('lcdhat',):
config['ui']['display']['type'] = 'lcdhat'

else:
print("unsupported display type %s" % config['ui']['display']['type'])
exit(1)
Expand Down

0 comments on commit 5643f9a

Please sign in to comment.