#34394 closed Bug (fixed)

ASGIRequest doesn't respect settings.FORCE_SCRIPT_NAME.

Reported by: julyzergcn Owned by: Sarah Boyce
Component: HTTP handling Version: 4.1
Severity: Normal Keywords: asgi SCRIPT_NAME
Cc: Carlton Gibson, Andrew Godwin, Sarah Boyce Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For example, I have settings.FORCE_SCRIPT_NAME = '/some-prefix'
I start a django server with command: daphne django_project.asgi:application
And I navigate to the http://localhost:8000/admin/login, and see the login form action url is "/admin/login" which is wrong, which should be "/some-prefix/admin/login"

Attachments (1)

Screenshot from 2023-03-16 00-43-06.png (42.4 KB ) - added by Ayush Bisht 20 months ago.
FORCE_SCRIPT_NAME doesn't working as expected for both ASGI and WSGI application.

Download all attachments as: .zip

Change History (10)

comment:1 by Mariusz Felisiak, 21 months ago

Cc: Carlton Gibson Andrew Godwin added
Component: Core (URLs)HTTP handling
Summary: django.core.handlers.asgi.ASGIRequest.script_name not work with settings.FORCE_SCRIPT_NAMEASGIRequest doesn't respect settings.FORCE_SCRIPT_NAME.
Triage Stage: UnreviewedAccepted

Thanks for the report. It seems that ASGIRequest should take FORCE_SCRIPT_NAME into account (as WSGIRequest), e.g.

  • django/core/handlers/asgi.py

    diff --git a/django/core/handlers/asgi.py b/django/core/handlers/asgi.py
    index 569157b277..c5eb87c712 100644
    a b class ASGIRequest(HttpRequest):  
    4040        self._post_parse_error = False
    4141        self._read_started = False
    4242        self.resolver_match = None
    43         self.script_name = self.scope.get("root_path", "")
     43        self.script_name = get_script_prefix(scope)
    4444        if self.script_name:
    4545            # TODO: Better is-prefix checking, slash handling?
    4646            self.path_info = scope["path"].removeprefix(self.script_name)
    class ASGIHandler(base.BaseHandler):  
    169169        except RequestAborted:
    170170            return
    171171        # Request is complete and can be served.
    172         set_script_prefix(self.get_script_prefix(scope))
     172        set_script_prefix(get_script_prefix(scope))
    173173        await signals.request_started.asend(sender=self.__class__, scope=scope)
    174174        # Get the request and check for basic issues.
    175175        request, error_response = self.create_request(scope, body_file)
    class ASGIHandler(base.BaseHandler):  
    310310            )
    311311            position += cls.chunk_size
    312312
    313     def get_script_prefix(self, scope):
    314         """
    315         Return the script prefix to use from either the scope or a setting.
    316         """
    317         if settings.FORCE_SCRIPT_NAME:
    318             return settings.FORCE_SCRIPT_NAME
    319         return scope.get("root_path", "") or ""
     313
     314def get_script_prefix(scope):
     315    """
     316    Return the script prefix to use from either the scope or a setting.
     317    """
     318    if settings.FORCE_SCRIPT_NAME:
     319        return settings.FORCE_SCRIPT_NAME
     320    return scope.get("root_path", "") or ""

Would you like to prepare a patch via GitHub PR? (a regression test is required.)

comment:2 by Mariusz Felisiak, 20 months ago

Cc: Sarah Boyce added

comment:3 by Ayush Bisht, 20 months ago

Owner: changed from nobody to Ayush Bisht
Status: newassigned

by Ayush Bisht, 20 months ago

FORCE_SCRIPT_NAME doesn't working as expected for both ASGI and WSGI application.

comment:4 by Ayush Bisht, 20 months ago

Here, in the above attachment, I've created a simple Django app and set this FORCE_SCRIPT_NAME to /ayush.
On running with both WSGI and ASGI applications, it shows the above-mentioned error.
Also, the queried URL is different as compared to what Django is looking for. There is the repetition of FORCE_SCRIPT_NAME

comment:5 by Ayush Bisht, 20 months ago

Seems like, FORCE_SCRIPT_NAME is also causing some issues with the WSGI application too, after someone's logout the session.
@Mariusz Felisiak, your solution for ASGI is working for me.
Please correct me, If I'm wrong...

in reply to:  5 comment:6 by Mariusz Felisiak, 20 months ago

Replying to Ayush Bisht:

Please correct me, If I'm wrong...

FORCE_SCRIPT_NAME is not intended to work with dev server, see #7930. It works fine when you try with e.g. daphne.

comment:7 by Sarah Boyce, 19 months ago

Has patch: set
Owner: changed from Ayush Bisht to Sarah Boyce

comment:8 by Mariusz Felisiak, 19 months ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 19 months ago

Resolution: fixed
Status: assignedclosed

In 041b0a3:

Fixed #34394 -- Added FORCE_SCRIPT_NAME handling to ASGIRequest.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@…>

Note: See TracTickets for help on using tickets.
Back to Top