From 10ba2be93f2218ac4016b89bdbe652e1b666ff15 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 6 Oct 2020 02:53:56 +0000 Subject: [PATCH] small bug fixes, added more cors headers for pub api browser requests --- src/httpapi/apiutils/getblockdata.py | 4 +++- src/httpapi/miscpublicapi/getblocks.py | 5 ++--- src/httpapi/security/public.py | 6 +++++- src/sneakernet/__init__.py | 6 +++++- static-data/www/shared/sidebar/sidebar.js | 1 - 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/httpapi/apiutils/getblockdata.py b/src/httpapi/apiutils/getblockdata.py index fc9a615d..9da1ee59 100644 --- a/src/httpapi/apiutils/getblockdata.py +++ b/src/httpapi/apiutils/getblockdata.py @@ -8,7 +8,9 @@ class GetBlockData: return def get_block_data(self, bHash, decrypt=False, raw=False, headerOnly=False): - if not stringvalidators.validate_hash(bHash): raise onionrexceptions.InvalidHexHash("block hash not valid hash format") + if not stringvalidators.validate_hash(bHash): + raise onionrexceptions.InvalidHexHash( + "block hash not valid hash format") bl = onionrblockapi.Block(bHash) if decrypt: bl.decrypt() diff --git a/src/httpapi/miscpublicapi/getblocks.py b/src/httpapi/miscpublicapi/getblocks.py index 6e6773af..adaceb91 100755 --- a/src/httpapi/miscpublicapi/getblocks.py +++ b/src/httpapi/miscpublicapi/getblocks.py @@ -54,7 +54,7 @@ def get_block_data(public_API, b_hash): if not config.get('general.hide_created_blocks', True) \ or b_hash not in public_API.hideBlocks: if b_hash in public_API._too_many.get(BlockList).get(): - block = apiutils.GetBlockData().get_block_b_hash( + block = apiutils.GetBlockData().get_block_data( b_hash, raw=True, decrypt=False) try: # Encode in case data is binary @@ -62,11 +62,10 @@ def get_block_data(public_API, b_hash): except AttributeError: if len(block) == 0: abort(404) - block = bytesconverter.str_to_bytes(block) + #block = bytesconverter.str_to_bytes(block) resp = block if len(resp) == 0: abort(404) resp = "" # Has to be octet stream, otherwise binary data fails hash check return Response(resp, mimetype='application/octet-stream') - \ No newline at end of file diff --git a/src/httpapi/security/public.py b/src/httpapi/security/public.py index ac171342..74d7e0cd 100644 --- a/src/httpapi/security/public.py +++ b/src/httpapi/security/public.py @@ -64,11 +64,15 @@ class PublicAPISecurity: # Network API version resp.headers['X-API'] = public_api.API_VERSION resp.headers['Access-Control-Allow-Origin'] = "*" + resp.headers['Access-Control-Allow-Headers'] = "*" + resp.headers['Access-Control-Allow-Methods'] = "POST, GET, OPTIONS" # Delete some HTTP headers for Onionr user agents NON_NETWORK_HEADERS = ( 'Content-Security-Policy', 'X-Frame-Options', 'X-Content-Type-Options', 'Feature-Policy', - 'Clear-Site-Data', 'Referrer-Policy') + 'Clear-Site-Data', 'Referrer-Policy', + 'Access-Control-Allow-Origin', 'Access-Control-Allow-Headers', + 'Access-Control-Allow-Methods') # For other nodes, we don't need to waste bits on the above headers try: diff --git a/src/sneakernet/__init__.py b/src/sneakernet/__init__.py index e32b64af..123e8997 100644 --- a/src/sneakernet/__init__.py +++ b/src/sneakernet/__init__.py @@ -11,6 +11,7 @@ import config from filepaths import block_data_location from etc.onionrvalues import BLOCK_EXPORT_FILE_EXT from onionrblocks.blockimporter import import_block_from_data +import onionrexceptions """ 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 @@ -37,7 +38,10 @@ class _Importer(FileSystemEventHandler): if not event.src_path.endswith(BLOCK_EXPORT_FILE_EXT): return with open(event.src_path, 'rb') as block_file: - import_block_from_data(block_file.read()) + try: + import_block_from_data(block_file.read()) + except onionrexceptions.DataExists: + return if block_data_location in event.src_path: try: os.remove(event.src_path) diff --git a/static-data/www/shared/sidebar/sidebar.js b/static-data/www/shared/sidebar/sidebar.js index 861892db..854bddc0 100644 --- a/static-data/www/shared/sidebar/sidebar.js +++ b/static-data/www/shared/sidebar/sidebar.js @@ -20,7 +20,6 @@ function sidebarAddPeerRegister(){ }}) .then((resp) => resp.text()) .then(function(data) { - alert(data) if (data == "success"){ PNotify.success({ text: 'Peer added'