def handle_blocking(self, ctx, config): """ Returns the appropriate response parameters according to blocking parameters :param PxContext ctx: :param PxConfig config: :return (string, array, string): response string, headers array, and status string """ logger = config.logger action = ctx.block_action status = '403 Forbidden' is_json_response = self.is_json_response(ctx) if is_json_response: content_type = 'application/json' else: content_type = 'text/html' headers = {'Content-Type': content_type} if action is px_constants.ACTION_CHALLENGE: logger.debug('Enforcing action: Challenge page is served') blocking_props = ctx.block_action_data blocking_response = blocking_props elif action is px_constants.ACTION_RATELIMIT: logger.debug('Enforcing action: Rate limit page is served') blocking_props = None blocking_response = self.ratelimit_rendered_page status = '429 Too Many Requests' else: # block logger.debug('Enforcing action: Block page is served') blocking_props = self.prepare_properties(ctx, config) blocking_response = self.mustache_renderer.render( px_template.get_template(px_constants.BLOCK_TEMPLATE), blocking_props) if ctx.is_mobile: page_response = json.dumps({ 'action': parse_action(ctx.block_action), 'uuid': ctx.uuid, 'vid': ctx.vid, 'appId': config.app_id, 'page': base64.b64encode(blocking_response), 'collectorURL': 'https://' + config.collector_host }) return page_response, headers, status if is_json_response: logger.debug('Serving advanced blocking response') blocking_response = json.dumps(blocking_props) blocking_response = str(blocking_response) return blocking_response, headers, status
def handle_verification(self, ctx, config, environ, start_response): score = ctx.get('risk_score', -1) if score < config['blocking_score']: return self.pass_traffic(environ, start_response, ctx) if config.get('custom_block_handler', False): px_activities_client.send_block_activity(ctx, config) return config['custom_block_handler'](ctx, start_response) elif config.get('module_mode', 'active_monitoring') == 'active_blocking': vid = ctx.get('vid', '') uuid = ctx.get('uuid', '') template = 'block' if config.get('captcha_enabled', False): template = 'captcha' body = px_template.get_template(template, self.config, uuid, vid) px_activities_client.send_block_activity(ctx, config) start_response("403 Forbidden", [('Content-Type', 'text/html')]) return [str(body)] else: return self.pass_traffic(environ, start_response, ctx)
def __init__(self): self.mustache_renderer = pystache.Renderer() self.ratelimit_rendered_page = self.mustache_renderer.render( px_template.get_template(px_constants.RATELIMIT_TEMPLATE), {})