fix(worldapi): ensure native session login routes and token response contract
* fix(worldapi): register native session endpoints explicitly ahead of fallback routing * fix(worldapi): never report successful login without a minted bearer token * fix(worldapi): reject wrong HTTP 200 login payload and show actionable routing error * fix(worldapi): validate admin dashboard session response schema before accepting HTTP 200 * ci(worldapi): guard session response contract and exact auth route precedence * test(worldapi): assert explicit session routing wins over variable /api handler
This commit is contained in:
1 parent
54792655ca
commit
6986f344da
6 files changed
+64
-6
No files matched your search
@@ -725,11 +725,14 @@ async function adminLogin(){
|
||||
method:'POST',headers:{'Content-Type':'application/json','Accept':'application/json'},
|
||||
body:JSON.stringify(body),cache:'no-store',credentials:'same-origin'
|
||||
});
|
||||
const data=await res.json();
|
||||
if(!res.ok)throw new Error('HTTP '+res.status+' ('+(data.error||'login_failed')+')');
|
||||
const data=await res.json().catch(()=>null);
|
||||
if(!res.ok)throw new Error('HTTP '+res.status+' ('+(data&&data.error||'login_failed')+')');
|
||||
if(!data||typeof data!=='object'||Array.isArray(data)||!Number.isInteger(data.user_level))
|
||||
throw new Error('Ungültige World-API-Login-Antwort (HTTP '+res.status+'). /api/v1/auth/session und Reverse-Proxy-Routing prüfen.');
|
||||
if(Number(data.user_level)<200)
|
||||
throw new Error('Zugriff verweigert: UserLevel 200 erforderlich.');
|
||||
if(!data.access_token)throw new Error('Der Server hat kein Sitzungstoken zurückgegeben.');
|
||||
if(typeof data.access_token!=='string'||data.access_token.length<24)
|
||||
throw new Error('World API: Login-Antwort ohne gültiges access_token (HTTP '+res.status+'). Backend und Proxy prüfen.');
|
||||
adminSessionToken=data.access_token;
|
||||
adminSessionExpiry=Date.now()+Math.max(0,Number(data.expires_in||0)-30)*1000;
|
||||
if(!adminIsActive())throw new Error('Administratorsitzung bereits abgelaufen.');
|
||||
|
||||
@@ -1791,6 +1791,17 @@ namespace NexVerse.Server.Api
|
||||
scopes,
|
||||
user.AccountStateChanged);
|
||||
|
||||
// A successful 200 response must NEVER omit the signed token.
|
||||
// Fail closed before recording a session or login success if a
|
||||
// future token implementation cannot issue a usable bearer.
|
||||
if (string.IsNullOrWhiteSpace(accessToken))
|
||||
{
|
||||
WriteError(response, HttpStatusCode.ServiceUnavailable,
|
||||
"session_token_unavailable",
|
||||
"The authentication service could not issue a session token.");
|
||||
return;
|
||||
}
|
||||
|
||||
NexSecuritySession securitySession = m_Security?.CreateSession(
|
||||
user.PrincipalId,
|
||||
requireAdmin ? "native-world-api-admin" : "native-world-api");
|
||||
|
||||
@@ -985,6 +985,21 @@ namespace NexVerse.Server.Api
|
||||
"NexVerse Session Revocation"));
|
||||
}
|
||||
|
||||
// Register authentication as exact routes rather than relying
|
||||
// only on /api variable-path fallback. A wrong handler or
|
||||
// reverse-proxy fallback can otherwise respond HTTP 200 without
|
||||
// the access_token contract expected by the Control Center.
|
||||
// The existing userRouter still validates POST, credentials,
|
||||
// TOTP, permissions and issues the signed bearer token.
|
||||
server.AddSimpleStreamHandler(new SimpleStreamHandler(
|
||||
"/api/v1/auth/session",
|
||||
apiGate.Wrap(userRouter.Handle),
|
||||
"OpenGenesisLINK native resident session"));
|
||||
server.AddSimpleStreamHandler(new SimpleStreamHandler(
|
||||
"/api/v1/auth/admin/session",
|
||||
apiGate.Wrap(userRouter.Handle),
|
||||
"OpenGenesisLINK native administrator session"));
|
||||
|
||||
server.AddSimpleStreamHandler(
|
||||
new SimpleStreamHandler("/api", apiGate.Wrap(userRouter.Handle), "NexVerse World API privileged router"),
|
||||
true);
|
||||
|
||||
@@ -202,10 +202,16 @@ form.addEventListener('submit',async event=>{
|
||||
if(form.elements.namedItem('totp').value.trim())credentials.totp=form.elements.namedItem('totp').value.trim();
|
||||
const login=await fetch('/api/v1/auth/session',{method:'POST',headers:{'Content-Type':'application/json','Accept':'application/json'},body:JSON.stringify(credentials),cache:'no-store',credentials:'same-origin'});
|
||||
form.elements.namedItem('password').value='';form.elements.namedItem('totp').value='';
|
||||
const data=await login.json().catch(()=>({}));
|
||||
if(!login.ok)throw Error(login.status===401?'Anmeldung fehlgeschlagen. Benutzername, Passwort oder TOTP überprüfen.':'Anmeldung fehlgeschlagen (HTTP '+login.status+').');
|
||||
const data=await login.json().catch(()=>null);
|
||||
if(!login.ok)throw Error(login.status===401?'Anmeldung fehlgeschlagen. Benutzername, Passwort oder TOTP überprüfen.':'Anmeldung fehlgeschlagen (HTTP '+login.status+', '+(data&&data.error||'api_error')+').');
|
||||
// An unrelated JSON HTTP 200 (proxy fallback, stale backend, or wrong
|
||||
// route) is NOT a successful GridUser login. Never treat a missing
|
||||
// user_level as an administrator: Number(undefined) is NaN.
|
||||
if(!data||typeof data!=='object'||Array.isArray(data)||!Number.isInteger(data.user_level))
|
||||
throw Error('Ungültige World-API-Login-Antwort (HTTP '+login.status+'). Prüfe /api/v1/auth/session und das Reverse-Proxy-Routing.');
|
||||
if(Number(data.user_level)<200)throw Error('Zugriff verweigert: UserLevel 200 erforderlich.');
|
||||
if(typeof data.access_token!=='string'||!data.access_token)throw Error('Der Server hat kein Sitzungstoken zurückgegeben.');
|
||||
if(typeof data.access_token!=='string'||data.access_token.length<24)
|
||||
throw Error('World API: Login-Antwort ohne gültiges access_token (HTTP '+login.status+'). Bitte Login-Backend und Proxy prüfen.');
|
||||
token=data.access_token;
|
||||
expires=Date.now()+Math.max(0,Number(data.expires_in||0)-30)*1000;
|
||||
if(!expires||expires<=Date.now())throw Error('Die Administratorsitzung ist bereits abgelaufen.');
|
||||
|
||||
@@ -33,6 +33,18 @@ must(access,
|
||||
"frame.srcdoc=html", "event.source!==frame.contentWindow",
|
||||
'frame.contentWindow.postMessage(', "token='';expires=0",
|
||||
)
|
||||
# The UI must distinguish an unrelated HTTP 200 JSON response from a
|
||||
# successful native login: Number(undefined) is NaN, not <200.
|
||||
for login_ui in (access, dashboard):
|
||||
must(login_ui, "!Number.isInteger(data.user_level)",
|
||||
"typeof data.access_token!=='string'",
|
||||
"/api/v1/auth/session", "Reverse-Proxy-Routing")
|
||||
must(user,
|
||||
'"session_token_unavailable"',
|
||||
"if (string.IsNullOrWhiteSpace(accessToken))")
|
||||
assert api.count('"/api/v1/auth/session"') == 1, "Native session exact registration missing"
|
||||
assert api.count('"/api/v1/auth/admin/session"') == 1, "Admin session exact registration missing"
|
||||
assert api.index('"/api/v1/auth/session"') < api.index('"/api", apiGate.Wrap(userRouter.Handle)')
|
||||
assert "if(!data.administrator" not in access
|
||||
assert "window.isSecureContext" not in access
|
||||
assert "IsSecureTransport(request)" not in access
|
||||
|
||||
@@ -54,6 +54,8 @@ exact_routes = {
|
||||
"/api/v1": "root",
|
||||
"/api/v1/health": "health",
|
||||
"/api/v1/docs": "docs",
|
||||
"/api/v1/auth/session": "native-session",
|
||||
"/api/v1/auth/admin/session": "admin-session",
|
||||
}
|
||||
variable_routes = {
|
||||
"/api": "users",
|
||||
@@ -69,6 +71,9 @@ examples = {
|
||||
"/api/v1": "root",
|
||||
"/api/v1/health": "health",
|
||||
"/api/v1/docs": "docs",
|
||||
"/api/v1/auth/session": "native-session",
|
||||
"/api/v1/auth/admin/session": "admin-session",
|
||||
"/api/v1/auth/clients": "users",
|
||||
"/api/v1/experiences": "experiences",
|
||||
"/api/v1/experiences/script/resolve": "experiences",
|
||||
"/api/v1/experiences/script/details": "experiences",
|
||||
@@ -82,6 +87,12 @@ examples = {
|
||||
"/api/v1/experiences-other": "users", # no partial-segment match
|
||||
"/apix/v1/experiences": None, # no partial-segment match
|
||||
}
|
||||
# Both exact login routes must bypass the broad /api fallback.
|
||||
assert connector.count('"/api/v1/auth/session"') == 1
|
||||
assert connector.count('"/api/v1/auth/admin/session"') == 1
|
||||
assert connector.index('"/api/v1/auth/session"') < connector.index(
|
||||
'"/api", apiGate.Wrap(userRouter.Handle)')
|
||||
|
||||
for path, expected in examples.items():
|
||||
actual = match(path, exact_routes, variable_routes)
|
||||
assert actual == expected, f"{path}: expected {expected}, got {actual}"
|
||||
|
||||
Reference in new issue
Block a user