Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geoip2.errors.AddressNotFoundError results in analysis failing #27

Open
rowansmithau opened this issue Apr 22, 2021 · 1 comment
Open

Comments

@rowansmithau
Copy link

Stood up the latest build using python on localhost and attempting to analyse some headers that contain an IP which seemingly isn't present in the geoip database causes the analysis to outright fail:

Traceback (most recent call last):
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/user/virt/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/user/virt/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/user/virt/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/user/email-header-analyzer/mha/server.py", line 199, in index
    return render_template(
  File "/Users/user/virt/lib/python3.8/site-packages/flask/templating.py", line 137, in render_template
    return _render(
  File "/Users/user/virt/lib/python3.8/site-packages/flask/templating.py", line 120, in _render
    rv = template.render(context)
  File "/Users/user/virt/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/Users/user/virt/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/Users/user/virt/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/Users/user/email-header-analyzer/mha/templates/index.html", line 109, in top-level template code
    <td>{{ v.Direction.0 }}{% if country(v.Direction.0).iso_code %}<span class="pull-right"><a href="#"
  File "/Users/user/email-header-analyzer/mha/server.py", line 37, in getCountryForIP
    r = reader.country(ip).country
  File "/Users/user/virt/lib/python3.8/site-packages/geoip2/database.py", line 126, in country
    Country, self._model_for(geoip2.models.Country, "Country", ip_address)
  File "/Users/user/virt/lib/python3.8/site-packages/geoip2/database.py", line 239, in _model_for
    (record, prefix_len) = self._get(types, ip_address)
  File "/Users/user/virt/lib/python3.8/site-packages/geoip2/database.py", line 228, in _get
    raise geoip2.errors.AddressNotFoundError(
geoip2.errors.AddressNotFoundError: The address x.x.x.x is not in the database.

IP address in question can be provided privately if required. It is a real world routable IP.

@denmase
Copy link

denmase commented Jul 2, 2021

IMHO this is geoip problem, however if you want to, you can handle the exception within server.py,
change the code starting from line 37

           r = reader.country(ip).country
            if r.iso_code and r.name:
                return {
                    'iso_code': r.iso_code.lower(),
                    'country_name': r.name
                }

to

         try:
                r = reader.country(ip).country
                if r.iso_code and r.name:
                    return {
                        'iso_code': r.iso_code.lower(),
                        'country_name': r.name
                    }
            except:
                pass

ebdavison added a commit to ebdavison/email-header-analyzer that referenced this issue Apr 11, 2023
Despite the fact that it is a problem with GeoIP lookup, it should not crash the code.
jpbarraca added a commit to gcsuaveiro/email-header-analyzer that referenced this issue Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants