Ejemplo n.º 1
0
def _IsServicingPrivilegedRequest():
    """Checks whether the request is considered privileged."""
    try:
        request = webapp2.get_request()
    except AssertionError:
        # This only happens in unit tests, when the code gets called outside of
        # a request.
        return False
    if (not request or hasattr(request, 'path')
            and request.path.startswith('/mapreduce')):
        # Running a mapreduce.
        return True
    if request.registry.get('privileged', False):
        return True
    whitelist = utils.GetIpWhitelist()
    if whitelist and hasattr(request, 'remote_addr'):
        return request.remote_addr in whitelist
    return False
Ejemplo n.º 2
0
def _IsServicingPrivilegedRequest():
    """Checks whether the request is considered privileged."""
    try:
        request = webapp2.get_request()
    except AssertionError:
        # This happens in unit tests, when code gets called outside of a request.
        return False
    path = getattr(request, 'path', '')
    if path.startswith('/mapreduce'):
        return True
    if path.startswith('/_ah/queue/deferred'):
        return True
    if request.registry.get('privileged', False):
        return True
    if request.registry.get('single_privileged', False):
        request.registry['single_privileged'] = False
        return True
    whitelist = utils.GetIpWhitelist()
    if whitelist and hasattr(request, 'remote_addr'):
        return request.remote_addr in whitelist
    return False
Ejemplo n.º 3
0
    def _CheckIpAgainstWhitelist(self):
        """Checks the remote address of the request against the IP whitelist.

    Returns:
      True if whitelisted, False otherwise.
    """
        whitelist = utils.GetIpWhitelist()
        if not whitelist or self.request.remote_addr in whitelist:
            return True
        # Try to log some info about the post data that is not whitelisted.
        # This could be totally bogus data, so ignore huge postdata and swallow
        # exceptions.
        try:
            data_param = self.request.get('data')
            if data_param and len(data_param) < 10000:
                # Log the start of the data; it may give clues about who is sending
                # the data and who to contact.
                logging.warn('Received data: %s...', data_param[:200])
        except Exception:  # pylint: disable=broad-except
            pass
        self.ReportError(
            'IP address %s not in IP whitelist!' % self.request.remote_addr,
            403)
        return False