enhancement
major
minor
defect
critical
major
minor
major
#30125
Agent access: /agent-api answers a request without a valid credential with a redirect to the login page instead of 401
Related: #30048 (agent access follow-ups).
Behavior (8.0.0-alpha12)
An application depending on tl-layout-view-agent (here E-Home, started with exec:java, Jetty 12.1). A request to the agent endpoint with a forged, expired or no bearer token:
curl -s -D - -o /dev/null -H "Authorization: Bearer tlagt_x_forged" http://localhost:8080/ehome/agent-api/windows HTTP/1.1 302 Found Location: /ehome/view/?sessionCheckInProgress=true
The same without any Authorization header gives 302 as well. A valid token gives 200 with {"windows": [...]}.
Expected
The article view-layer/agent-access says: "A credential of an ended session admits nobody (401)", and AgentServlet sends SC_UNAUTHORIZED "No session." when it finds no session. The answer never gets there: the session check of TopLogicServlet (SESSION_CHECK, sessionCheckInProgress) redirects first, because the request has no session cookie.
An agent is no browser: it follows the redirect, gets the HTML of the view page with status 200 and has to guess from the content type that it was not admitted. The endpoint should answer every request it does not admit with 401 (and a JSON {"error": ...} like its other errors), without a redirect, so that a client can tell "token not accepted" from a server error.
Reproduction
Any application with tl-layout-view-agent, no login: curl -i <app>/agent-api/windows → 302 to /view/?sessionCheckInProgress=true.
The refusal itself is safe (no data are delivered); only the status is wrong. The integration test of #30048 (6.) should cover it.
Lösung
AgentServlet nutzt den vorhandenen Einstiegspunkt von TopLogicServlet (wie ReactServlet und AJAXServlet):
- isCookieCheckRequired() liefert false: Der Cookie-Test ist nur für die Navigation eines Browsers sinnvoll, ein Agent kann ihn nicht bestehen.
- handleNoSession() antwortet mit 401 und dem JSON-Fehlerobjekt {"error": ...} des Endpunkts, ohne Weiterleitung. Ohne diese Überschreibung endete eine Anfrage ohne Session nach dem Abschalten des Tests mit einer leeren Antwort 200.
Eine Anfrage mit Session-Cookie (Browser) ist nicht betroffen. Ein ServletUnit-Test prüft: kein Authorization-Header, gefälschtes Token und Token einer beendeten Session werden jeweils mit 401, JSON-Rumpf und ohne Location beantwortet. Der Artikel view-layer/agent-access beschreibt die Antwort. Nebenbei: die JavaDoc von SignedInContentServlet behauptete, der Cookie-Test sei nicht abschaltbar; sie nennt jetzt isCookieCheckRequired() und den eigentlichen Grund, ihn dort beizubehalten.