|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leantime\Domain\Status\Controllers; |
| 4 | + |
| 5 | +use Leantime\Core\Configuration\AppSettings; |
| 6 | +use Leantime\Core\Configuration\Environment; |
| 7 | +use Leantime\Core\Controller\Controller; |
| 8 | +use Leantime\Core\Http\IncomingRequest; |
| 9 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | + |
| 12 | +/** |
| 13 | + * Public, unauthenticated instance status / discovery endpoint — GET /status. |
| 14 | + * |
| 15 | + * The mobile app calls this at connect time to discover which login methods the |
| 16 | + * instance offers (password / ldap / oidc), so it can show the right affordances |
| 17 | + * — notably the OIDC "Sign in with SSO" redirect, which stays dormant in the app |
| 18 | + * until a backend advertises it here. See the mobile client's |
| 19 | + * connectionService::fetchPublicStatus and |
| 20 | + * docs/backend-mobile-auth-bridge-plan.md for the contract. |
| 21 | + * |
| 22 | + * SECURITY — this endpoint is UNAUTHENTICATED, so it returns ONLY the safe, |
| 23 | + * minimal tier: auth methods, the core version + instance name, provider labels, |
| 24 | + * and the min app version. It deliberately does NOT list installed plugins, |
| 25 | + * plugin versions, or the db version — an unauthenticated inventory of those is a |
| 26 | + * recon gift (CVE matching). Those stay behind auth (the authenticated |
| 27 | + * mobileStatus). Keep any `publicStatus` filter additions to this safe tier. |
| 28 | + * |
| 29 | + * Route 'status.index' is allow-listed public in AuthCheck. |
| 30 | + */ |
| 31 | +class Index extends Controller |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Keys that must NEVER appear in the unauthenticated response, even if a |
| 35 | + * publicStatus filter (or a future edit) adds them — a recon-risk inventory. |
| 36 | + * Stripped after the filter runs, as defense in depth. |
| 37 | + */ |
| 38 | + private const SENSITIVE_KEYS = ['plugins', 'pluginVersions', 'installedPlugins', 'dbVersion', 'db_version']; |
| 39 | + |
| 40 | + private Environment $config; |
| 41 | + |
| 42 | + private AppSettings $appSettings; |
| 43 | + |
| 44 | + private IncomingRequest $request; |
| 45 | + |
| 46 | + /** |
| 47 | + * init - inject config, app settings, and the incoming request. |
| 48 | + */ |
| 49 | + public function init(Environment $config, AppSettings $appSettings, IncomingRequest $request): void |
| 50 | + { |
| 51 | + $this->config = $config; |
| 52 | + $this->appSettings = $appSettings; |
| 53 | + $this->request = $request; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Return the public discovery payload: enabled auth methods, the OIDC login |
| 58 | + * URL (when enabled), instance name, core version, and min app version — the |
| 59 | + * safe unauthenticated tier only. Never plugin inventory / versions / db |
| 60 | + * version (see the class-level security note). |
| 61 | + */ |
| 62 | + public function get(array $params): Response |
| 63 | + { |
| 64 | + $oidcEnabled = (bool) $this->config->oidcEnable; |
| 65 | + $ldapEnabled = $this->config->useLdap === true && extension_loaded('ldap'); |
| 66 | + |
| 67 | + // password is always available; ldap/oidc only when configured. |
| 68 | + $authMethods = ['password']; |
| 69 | + if ($ldapEnabled) { |
| 70 | + $authMethods[] = 'ldap'; |
| 71 | + } |
| 72 | + if ($oidcEnabled) { |
| 73 | + $authMethods[] = 'oidc'; |
| 74 | + } |
| 75 | + |
| 76 | + $payload = [ |
| 77 | + 'mobileAuthEnabled' => true, |
| 78 | + 'instanceName' => (string) ($this->config->sitename ?: 'Leantime'), |
| 79 | + 'version' => $this->appSettings->appVersion, |
| 80 | + 'minAppVersion' => null, |
| 81 | + 'authMethods' => $authMethods, |
| 82 | + 'ssoProviders' => [], |
| 83 | + ]; |
| 84 | + |
| 85 | + if ($oidcEnabled) { |
| 86 | + // Generic-OIDC login initiation URL (core). The app opens this in the |
| 87 | + // system auth browser; the mobile branch is triggered by its own query |
| 88 | + // params (see Oidc\Controllers\Login). |
| 89 | + $payload['oidcLoginUrl'] = $this->request->getSchemeAndHttpHost().'/oidc/login'; |
| 90 | + } |
| 91 | + |
| 92 | + // AdvancedAuth (or other plugins) can append named SSO providers to the |
| 93 | + // PUBLIC-safe payload (labels + login URLs only) via this filter, without |
| 94 | + // core knowing about them. Filter handlers MUST preserve the public-safe |
| 95 | + // contract — never add secrets, plugin inventory, or versions here. |
| 96 | + $payload = self::dispatchFilter('publicStatus', $payload, ['request' => $this->request]); |
| 97 | + |
| 98 | + // Defense in depth: this endpoint is unauthenticated, so strip any |
| 99 | + // known-sensitive keys a misbehaving filter (or a future edit) might have |
| 100 | + // added. The recon-risk inventory must NEVER reach an unauthenticated |
| 101 | + // caller, even if a plugin gets the contract wrong. |
| 102 | + foreach (self::SENSITIVE_KEYS as $sensitive) { |
| 103 | + unset($payload[$sensitive]); |
| 104 | + } |
| 105 | + |
| 106 | + return new JsonResponse($payload); |
| 107 | + } |
| 108 | +} |
0 commit comments