MSS icon indicating copy to clipboard operation
MSS copied to clipboard

better solution for "owslib doesn't recognize 401 Error Messages"

Open ReimarBauer opened this issue 9 months ago • 2 comments

Currently, our system has a limited way of recognizing unauthorized access errors (401). It looks for specific text in the error message returned by the server.
This works well with our current Apache2 setup, but it's not flexible enough to handle different types of 401 responses – particularly those from Nginx or other web servers that might use a different format. For example, if the error message doesn’t start with “401” or include "Error 401", our system won't detect it correctly. We need to improve this detection method for broader compatibility.

see https://github.com/Open-MSS/MSS/blob/develop/mslib/msui/wms_control.py#L795

Example for nginx to set the specific text

 location @custom_401 {
    add_header Content-Type application/json;
    return 401 '{"error":"401 Unauthorized","message":"Authentication missing or
 invalid"}';
  }

ReimarBauer avatar Feb 26 '26 08:02 ReimarBauer

Proposed solution: Currently, unauthorized access is detected by checking specific text inside the error message. This is fragile because different servers (like Nginx) return different formats for 401 errors. A more reliable approach would be: First, detect unauthorized access using the HTTP status code: If response.status_code == 401, treat it as unauthorized. If the status code is not available, check the response headers: If WWW-Authenticate header exists, it usually indicates a 401 response. Only as a fallback, inspect the response body text: Convert to lowercase and check for common patterns like "unauthorized" or "401". Avoid relying on strict string matching such as checking if the message starts with "401", since this breaks with custom HTML or JSON error pages. This would make the detection independent of server-specific error message formats and improve compatibility with Nginx and other web servers. I can prepare a patch for wms_control.py implementing this logic if this approach is acceptable.

MamathaGarudampalli avatar Feb 27 '26 09:02 MamathaGarudampalli

I would prefer that we solve the problem in owslib https://github.com/geopython/OWSLib itselfs. I don't want to lookup a message. We do use a QT frontend, so javascript does not help.

see https://github.com/geopython/OWSLib/issues/1033

ReimarBauer avatar Mar 02 '26 11:03 ReimarBauer