Skip to content

Test isLoggedIn()'s returned User, not the return itself - #2110

Merged
fog-workflows[bot] merged 5 commits into
dev-branchfrom
fix/isloggedin-object-return
Nov 18, 2026
Merged

fog-workflows[bot] merged 5 commits into
dev-branchfrom
fix/isloggedin-object-return

Conversation

@mastacontrola

Copy link
Copy Markdown
Member

Follow-up to #2109 (issue #2107), stacked on it. #2109 entered the merge queue before this fix could be pushed to its branch. Until #2109 merges, this diff also shows #2109's commits; the change here is the last commit.

The bug

User::isLoggedIn() returns an object: $this, or an invalid User(0) on refusal. Its docblock said bool. Two callers tested it as a boolean, isLoggedIn() && isValid():

  • the remember-me replay in ProcessLogin::processMainLogin()
  • management/other/index.php

An object is always truthy. So the replay accepted every account its cookie named, and the refusals inside _isLoggedIn() never took effect on that path. That covers both the API-only refusal and the role network refusal from #2109.

How it was found

In the live lab test of #2109, a remember-me cookie kept a user signed in after their role's networks stopped covering the address. With this fix, the same request shows the login form. Every other path in that test passed: password sign-in, an open session, the API, the self-lockout guard, and iPXE and FOS still working.

The change

  • Both callers now test isLoggedIn()->isValid().
  • The docblock now says @return User. PHPStan flagged the first caller fix against the old docblock.
  • tests/role-allowed-networks.test.php scans every PHP file under packages/, with comments stripped, for a boolean use of the return. It goes red on the old code.

For a user who is signed in normally, or signed out, nothing changes. Both expressions call _isLoggedIn() once, so its side effects are the same.

🤖 Generated with Claude Code

mastacontrola and others added 3 commits November 18, 2026 10:10
Issue #2107. A role gets an Allowed Networks list (addresses and CIDR
ranges, IPv4 and IPv6). A user whose roles carry lists may sign in to
the web UI and use the REST API only from an address in the union of
those lists. A role with no list does not lift another role's limit.
Empty everywhere means no restriction, which is every existing install.

The iPXE menu login, the advanced menu, and FOS checkcredentials are
not limited: they prove the credential through passwordValidate() /
authenticate(), which carry no network check, so a technician can still
start a task from the boot menu on any network.

Gates:
- establishSession(): a new sign-in, any provider. After the FOGUser
  rebind, so it tests the account the session is stamped with.
- LoadGlobals: an existing session, on every request. Nothing
  downstream re-checked the account, so a session (or a replayed
  session cookie) kept working from anywhere. This also drops an
  existing session of an account marked API-only, which had the same
  gap.
- _isLoggedIn() plus ProcessLogin: the remember-me replay. ProcessLogin
  now unbinds the cookie's account when isLoggedIn() refuses it; before,
  the pages rendered anyway, because they test only isValid().
- Route: token, Bearer and basic-auth API clients get 403.

validatePw() now mints the remember-me cookie only after
establishSession() accepts the sign-in, so a refused sign-in leaves no
userAuths row behind.

REMOTE_ADDR only, never X-Forwarded-For. Loopback gets no exception: a
reverse proxy on the server would make every request loopback and the
limit would silently pass everyone. Role::save() refuses an edit that
would lock out the user making it.

Schema 461 adds roles.rAllowedNetworks varchar(2048).

OpenAPI: no hand edit. The new role field is in $databaseFields and the
manifest, so _entitySchema() emits it. The new 403 on the token paths is
covered by the generic '403' response every operation already
documents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
User::isLoggedIn() returns an object, $this or an invalid User(0), and
its docblock said bool. The remember-me replay in ProcessLogin and
management/other/index.php tested `isLoggedIn() && isValid()`; an
object is always truthy, so every account the cookie named was
accepted and the API-only and role network refusals in _isLoggedIn()
never took effect there.

Found by the live lab test of #2107: a remember-me cookie kept a user
signed in after their role's networks stopped covering the address.
After the fix the same request shows the login form.

Both callers now test isLoggedIn()->isValid(), the docblock says
@return User, and the test scans every PHP file under packages/ for a
boolean use of the return.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fog-workflows
fog-workflows Bot enabled auto-merge November 18, 2026 16:21
mastacontrola and others added 2 commits November 18, 2026 10:23
The leading-! branch flagged the correct form, !$u->isLoggedIn()->isValid(),
so a caller writing the right code got a red build. Both branches now
skip a call followed by ->. The before-the-call branch also catches &&,
|| and (bool), so an assignment like $a = $x && $u->isLoggedIn() is
flagged. Probed both correct forms green and five bad shapes red.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
global-class-prefix.test.php requires a leading backslash on built-in
classes; the new isLoggedIn() scan used three without one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fog-workflows
fog-workflows Bot added this pull request to the merge queue Nov 18, 2026
Merged via the queue into dev-branch with commit f2928ba Nov 18, 2026
11 checks passed
@fog-workflows
fog-workflows Bot deleted the fix/isloggedin-object-return branch November 18, 2026 16:47
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