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

Using request.url_for throws KeyError: 'SERVER_NAME' in 19.9.0 #1707

Closed
roemba opened this issue Oct 23, 2019 · 3 comments · Fixed by #1708
Closed

Using request.url_for throws KeyError: 'SERVER_NAME' in 19.9.0 #1707

roemba opened this issue Oct 23, 2019 · 3 comments · Fixed by #1708
Labels

Comments

@roemba
Copy link

roemba commented Oct 23, 2019

Describe the bug
Running Sanic behind a proxy and trying to generate an url using request.url_for throws a KeyError. This used to work but now Sanic suddenly requires a SERVER_NAME in order to do this. I get the following trace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/sanic/app.py", line 942, in handle_request
    response = await response
  File "/app/tsauth/views/activate.py", line 41, in init_activate
    request=request)
  File "/usr/local/lib/python3.7/site-packages/mako/template.py", line 476, in render
    return runtime._render(self, self.callable_, args, data)
  File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 883, in _render
    **_kwargs_for_callable(callable_, data)
  File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 920, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 947, in _exec_template
    callable_(context, *args, **kwargs)
  File "transaction_mako", line 60, in render_body
  File "/usr/local/lib/python3.7/site-packages/sanic/request.py", line 522, in url_for
    if "//" in self.app.config.SERVER_NAME:
  File "/usr/local/lib/python3.7/site-packages/sanic/config.py", line 54, in __getattr__
    raise AttributeError("Config has no '{}'".format(ke.args[0]))
AttributeError: Config has no 'SERVER_NAME'
[2019-10-23 12:38:09 +0000] - (sanic.access)[INFO][<redacted>:33328]: GET <redacted>  500 144
10/23/2019 12:38:09 PM ERROR Exception occurred while handling uri: '<redacted>'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/sanic/config.py", line 52, in __getattr__
    return self[attr]
KeyError: 'SERVER_NAME'

Code snippet
request.url_for("static", name='static', filename="badge.png")

Expected behavior
request.url_for should generate a full url with the given path, e.g. https://hostname:port/static/badge.png. As I am running my server behind a proxy, I do not want to hardcode the hostname in the SERVER_NAME. This used to work with Sanic version 19.6.3.

Environment (please complete the following information):

  • Python 3.7 Docker container, Kubernetes v1.16.2 with nginx-ingress 0.26.1
  • Sanic version 19.9.0

Additional context
The calling code to request.url_for has not changed since the Sanic version update.
The documentation also does not speak a word about why the SERVER_NAME variable needs to be configured...

@sjsadowski sjsadowski added the bug label Oct 23, 2019
@harshanarayana
Copy link
Contributor

if "//" in self.app.config.SERVER_NAME:
    return self.app.url_for(view_name, _external=True, **kwargs)

I believe this got introduced as part of 1e4b1c4

This can easily be mitigated with the help of a get method. Opening a PR for the same.

@Tronic
Copy link
Member

Tronic commented Oct 24, 2019

@roemba As a workaround until the fix gets released, you may define app.config.SERVER_NAME = "".

@roemba
Copy link
Author

roemba commented Oct 25, 2019

Thanks for the quick response! Will try out the workaround today.

Edit: Works great!

harshanarayana added a commit to harshanarayana/sanic that referenced this issue Oct 26, 2019
yunstanford pushed a commit that referenced this issue Nov 1, 2019
…#1708)

* 🐛 fix SERVER_NAME enforcement in url_for

fixes #1707

* 💡 add additional documentation for using request.args

fixes #1704

* ✅ add additional test to check url_for without SERVER_NAME

* 📝 add changelog for fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants