Skip to content

Commit

Permalink
Allow XMLHttpRequests to prevent password dialogs
Browse files Browse the repository at this point in the history
When presented a WWW-Authenticate header with "Basic", a browser will
always ask the user for a password, before returning the 401 response to
the web application issuing the XHR in the first place.
Returning a fake authentication method instead seems to be the common
workaround pattern.
  • Loading branch information
mdellweg committed Jan 2, 2025
1 parent ecf2e6c commit 3e64f38
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES/+xmlhttp.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added a check to the basic auth module to respect the `X-Requested-With: "XMLHttpRequest"` header.
In return the signature of the `WWW-Authenticate` header is changed so browsers will not pop up a password dialog.
15 changes: 14 additions & 1 deletion pulpcore/app/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@

from django.contrib.auth import authenticate
from django.contrib.auth.backends import RemoteUserBackend
from rest_framework.authentication import BaseAuthentication, RemoteUserAuthentication
from rest_framework.authentication import (
BaseAuthentication,
RemoteUserAuthentication,
BasicAuthentication as OrigBasicAuthentication,
)
from rest_framework.exceptions import AuthenticationFailed

from pulpcore.app import settings

_logger = logging.getLogger(__name__)


class BasicAuthentication(OrigBasicAuthentication):
def authenticate_header(self, request):
xrw_header = request.headers.get("X-Requested-With")

if xrw_header is not None and xrw_header.lower() == "xmlhttprequest":
return "x" + super().authenticate_header(request)
return super().authenticate_header(request)


class PulpRemoteUserAuthentication(RemoteUserAuthentication):
header = settings.REMOTE_USER_ENVIRON_NAME

Expand Down
2 changes: 1 addition & 1 deletion pulpcore/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"PAGE_SIZE": 100,
"DEFAULT_PERMISSION_CLASSES": ("pulpcore.app.access_policy.AccessPolicyFromDB",),
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.BasicAuthentication",
"pulpcore.app.authentication.BasicAuthentication",
"rest_framework.authentication.SessionAuthentication",
),
"UPLOADED_FILES_USE_URL": False,
Expand Down

0 comments on commit 3e64f38

Please sign in to comment.