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

PATH_INFO set incorrectly by WsgiToAsgiInstance.build_environ() #442

Closed
srittau opened this issue Feb 15, 2024 · 1 comment · Fixed by #446
Closed

PATH_INFO set incorrectly by WsgiToAsgiInstance.build_environ() #442

srittau opened this issue Feb 15, 2024 · 1 comment · Fixed by #446

Comments

@srittau
Copy link

srittau commented Feb 15, 2024

See also #424 and especially this comment by @tiangolo for background.

PATH_INFO seems to be set incorrectly by WsgiToAsgiInstance.build_environ(). Currently, it's just copied over from the path ASGI scope:

asgiref/asgiref/wsgi.py

Lines 57 to 70 in 8cf847a

environ = {
"REQUEST_METHOD": scope["method"],
"SCRIPT_NAME": scope.get("root_path", "").encode("utf8").decode("latin1"),
"PATH_INFO": scope["path"].encode("utf8").decode("latin1"),
"QUERY_STRING": scope["query_string"].decode("ascii"),
"SERVER_PROTOCOL": "HTTP/%s" % scope["http_version"],
"wsgi.version": (1, 0),
"wsgi.url_scheme": scope.get("scheme", "http"),
"wsgi.input": body,
"wsgi.errors": BytesIO(),
"wsgi.multithread": True,
"wsgi.multiprocess": True,
"wsgi.run_once": False,
}

But path includes the root_path, while the WSGI spec (PEP 3333) says:

SCRIPT_NAME
The initial portion of the request URL’s “path” that corresponds to the application object, so that the application knows its virtual “location”. This may be an empty string, if the application corresponds to the “root” of the server.
PATH_INFO
The remainder of the request URL’s “path”, designating the virtual “location” of the request’s target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash.

A holdover from the CGI spec (RFC 3875).

When starlette fixed their path handling, they changed their WSGI adapter to account for that:

https://github.com/encode/starlette/blob/7533b61a34f8b0dcb5fc35c717d458439f0baf18/starlette/middleware/wsgi.py#L24-L42

    script_name = scope.get("root_path", "").encode("utf8").decode("latin1")
    path_info = scope["path"].encode("utf8").decode("latin1")
    if path_info.startswith(script_name):
        path_info = path_info[len(script_name) :]

    environ = {
        # ...
        "SCRIPT_NAME": script_name,
        "PATH_INFO": path_info,
        # ...
    }

I think asgiref's adapter should do the same.

@andrewgodwin
Copy link
Member

Yes, I agree. I likely made a shortcut in the initial version to get it working and then never came back and improved it.

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

Successfully merging a pull request may close this issue.

2 participants