def search_post(): """Handle form submit.""" try: post_data = flask.request.form['domains'] except KeyError: app.logger.info( 'Missing "domains" key from POST: {}'.format(flask.request.form) ) return flask.redirect('/error/2') if post_data is None or post_data.strip() == '': app.logger.info( 'No data in "domains" key in POST' ) return flask.redirect('/error/2') # We currently don't support Unicode in searches. try: post_data.decode('ascii') except UnicodeEncodeError: app.logger.info( 'Unicode search requested' ) return flask.redirect('/error/3') search_domains = tools.parse_post_data(post_data) valid_domains = sorted(list(set(filter(None, map(tools.parse_domain, search_domains))))) if len(valid_domains) == 0: app.logger.info( 'No valid domains in POST {}'.format(flask.request.form) ) suggestion = tools.suggest_domain(search_domains) if suggestion is not None: encoded_suggestion = binascii.hexlify(suggestion) return flask.redirect( '/error/0?suggestion={}'.format(encoded_suggestion) ) return flask.redirect('/error/0') # Attempt to create a <= 200 character GET parameter from the domains so # we can redirect to that (allows bookmarking). As in '/api/analysis/ip' # we use hex to hide the domains from firewalls that already block some of # them. path = ','.join(map(binascii.hexlify, search_domains)) if len(path) <= 200: return flask.redirect('/search/{}'.format(path)) # If there's a ton of domains, just to the report. return html_render(search_domains)
def search(search_domains, fmt=None): """Handle redirect from form submit.""" # We currently don't support Unicode in searches. try: search_domains.decode('ascii') except UnicodeEncodeError: app.logger.info( 'Unicode search requested' ) return flask.redirect('/error/3') # Try to parse out the list of domains try: valid_domains = sorted(list(set(filter(None, map( tools.parse_domain, search_domains.split(',') ))))) except: app.logger.info('Unable to decode valid domains from path') return flask.redirect('/error/0') if len(valid_domains) == 0: app.logger.info( 'No valid domains in GET' ) suggestion = tools.suggest_domain(search_domains.split(',')) if suggestion is not None: encoded_suggestion = binascii.hexlify(suggestion) return flask.redirect( '/error/0?suggestion={}'.format(encoded_suggestion) ) return flask.redirect('/error/0') if fmt is None: return html_render(valid_domains, search_domains) elif fmt == 'json': return json_render(valid_domains) elif fmt == 'csv': return csv_render(valid_domains) else: flask.abort(400, 'Unknown export format: {}'.format(fmt))
def handle_invalid_domain(search_term_as_hex): """Called when no valid domain found in GET param, creates a suggestion to return to the user. """ decoded_search = None try: decoded_search = bytes.fromhex(search_term_as_hex).decode('ascii') except: pass if decoded_search is not None: suggestion = tools.suggest_domain(decoded_search) if suggestion is not None: app.logger.info( 'Not a valid domain in GET: {}, suggesting: {}'.format( search_term_as_hex, suggestion)) return flask.redirect('/error/0?suggestion={}'.format( Domain(suggestion).to_hex())) app.logger.info('Not a valid domain in GET: {}'.format(search_term_as_hex)) return flask.redirect('/error/0')