Skip to content

fix: improve authentication error handling in check_auth#1069

Open
Vidhushaaa30 wants to merge 2 commits intofossasia:masterfrom
Vidhushaaa30:improve-auth-error-response
Open

fix: improve authentication error handling in check_auth#1069
Vidhushaaa30 wants to merge 2 commits intofossasia:masterfrom
Vidhushaaa30:improve-auth-error-response

Conversation

@Vidhushaaa30
Copy link

@Vidhushaaa30 Vidhushaaa30 commented Mar 20, 2026

Description

Improved authentication handling in check_auth by returning a proper HTTP 401 status code along with a descriptive error message when a user is not authenticated.

Motivation and Context

Previously, unauthorized access returned a 400 status with no message, which made it unclear for clients. This change improves API clarity and follows standard HTTP practices.

How Has This Been Tested?

  • Verified that unauthorized requests now return HTTP 401 with an error message.
  • Confirmed that authenticated requests continue to function as expected.

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Code refactor or cleanup

Checklist:

  • I adapted the version number under py/visdom/VERSION
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Summary by Sourcery

Bug Fixes:

  • Return HTTP 401 with an error message when authentication is required but the user is not logged in, instead of a generic 400 with no message.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 20, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the _check_auth wrapper to return a proper HTTP 401 response with a JSON error body when authentication is required but missing, instead of a generic 400 with no message.

Sequence diagram for updated _check_auth unauthorized response handling

sequenceDiagram
    actor Client
    participant VisdomServerHandler as VisdomServerHandler
    participant _check_auth as _check_auth
    participant ProtectedHandler as ProtectedHandler

    Client->>VisdomServerHandler: HTTP request to protected endpoint
    VisdomServerHandler->>_check_auth: call _check_auth(handler, *args, **kwargs)
    _check_auth->>_check_auth: update handler.last_access
    alt login_enabled and no current_user
        _check_auth->>VisdomServerHandler: set_status(401)
        _check_auth->>VisdomServerHandler: write({error: Authentication required})
        _check_auth-->>VisdomServerHandler: return
        VisdomServerHandler-->>Client: HTTP 401 with JSON error body
    else authenticated or login disabled
        _check_auth->>ProtectedHandler: call f(handler, *args, **kwargs)
        ProtectedHandler-->>VisdomServerHandler: normal response
        VisdomServerHandler-->>Client: HTTP 2xx/other success response
    end
Loading

File-Level Changes

Change Details Files
Adjust authentication failure handling to use HTTP 401 and include an error payload.
  • Change unauthorized status code from 400 to 401 when login is enabled and no current user is present.
  • Write a JSON error response body indicating that authentication is required before returning from the auth check.
  • Keep the control flow the same so that authenticated requests still pass through to the wrapped handler function.
py/visdom/utils/server_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new lines inside _check_auth are misindented relative to the surrounding block; align them with the existing handler.set_status indentation to maintain consistency and avoid potential syntax issues.
  • Consider also setting a WWW-Authenticate header (e.g., handler.set_header('WWW-Authenticate', 'Basic realm="..."')) along with the 401 response to fully conform with common HTTP authentication practices if applicable to this handler.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new lines inside `_check_auth` are misindented relative to the surrounding block; align them with the existing `handler.set_status` indentation to maintain consistency and avoid potential syntax issues.
- Consider also setting a `WWW-Authenticate` header (e.g., `handler.set_header('WWW-Authenticate', 'Basic realm="..."')`) along with the 401 response to fully conform with common HTTP authentication practices if applicable to this handler.

## Individual Comments

### Comment 1
<location path="py/visdom/utils/server_utils.py" line_range="56-57" />
<code_context>
         if handler.login_enabled and not handler.current_user:
-            handler.set_status(400)
-            return
+           handler.set_status(401)
+           handler.write({"error": "Authentication required"})
+           return
         f(handler, *args, **kwargs)
</code_context>
<issue_to_address>
**issue (bug_risk):** The indentation on these new lines is inconsistent with the surrounding block and may cause an `IndentationError`.

The new lines under `if handler.login_enabled and not handler.current_user:` have one fewer leading space than the original ones. In Python, that mismatch within the same block can raise an `IndentationError`. Please align the indentation of the new `set_status`, `write`, and `return` lines exactly with the original ones.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +56 to +57
handler.set_status(401)
handler.write({"error": "Authentication required"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The indentation on these new lines is inconsistent with the surrounding block and may cause an IndentationError.

The new lines under if handler.login_enabled and not handler.current_user: have one fewer leading space than the original ones. In Python, that mismatch within the same block can raise an IndentationError. Please align the indentation of the new set_status, write, and return lines exactly with the original ones.

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 this pull request may close these issues.

1 participant