app.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import { randomBytes } from 'node:crypto'
  2. import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
  3. import { createServer as createHttpServer, type Server as HttpServer } from 'node:http'
  4. import { createServer as createHttpsServer } from 'node:https'
  5. import { join } from 'node:path'
  6. import { bodyParser } from '@koa/bodyparser'
  7. import Koa from 'koa'
  8. import { RealtimeAPI } from './realtimeApi.js'
  9. import type { ServerConfig } from './config.js'
  10. import { loadServerConfig } from './config.js'
  11. import { errorMessage, HttpError } from './errors.js'
  12. import { NodePairingStore } from './pairing.js'
  13. import { LocalAuthStore, UpstreamServiceSession } from './localAuth.js'
  14. import { AccountLoginPairingStore } from './accountPairing.js'
  15. import { UpstreamProfileIdentityService } from './profileIdentities.js'
  16. import { createApiRouter } from './routes.js'
  17. import {
  18. applySecurityHeaders,
  19. CsrfProtection,
  20. isAllowedHostHeader,
  21. isExactOrigin,
  22. } from './security.js'
  23. import { UpstreamHttpError, UpstreamClient } from './upstream.js'
  24. import { UploadStore } from './uploads.js'
  25. import { SystemUpdateManager } from './updateManager.js'
  26. import { PushCoordinator } from './pushCoordinator.js'
  27. import {
  28. APNsConfigurationManager,
  29. type APNsConfigurationSnapshot,
  30. } from './apnsConfiguration.js'
  31. import {
  32. FCMConfigurationManager,
  33. type FCMConfigurationSnapshot,
  34. } from './fcmConfiguration.js'
  35. import { PushCoordinatorEventAdapter } from './pushEventAdapter.js'
  36. import {
  37. AllowedHostsConfigurationManager,
  38. type AllowedHostsConfigurationSnapshot,
  39. } from './allowedHostsConfiguration.js'
  40. import {
  41. ChatPushJobManager,
  42. GroupPushEventWatcher,
  43. HermesChatNotificationResolver,
  44. HermesGroupEventSource,
  45. } from './pushEvents.js'
  46. export interface ApplicationOptions {
  47. config?: ServerConfig
  48. fetchImpl?: typeof fetch
  49. csrfSecret?: Buffer
  50. pairings?: NodePairingStore
  51. uploads?: UploadStore
  52. updates?: SystemUpdateManager
  53. auth?: LocalAuthStore
  54. upstreamSession?: UpstreamServiceSession
  55. accountPairings?: AccountLoginPairingStore
  56. profileIdentities?: UpstreamProfileIdentityService
  57. push?: PushCoordinator
  58. apnsConfiguration?: APNsConfigurationManager
  59. fcmConfiguration?: FCMConfigurationManager
  60. allowedHostsConfiguration?: AllowedHostsConfigurationManager
  61. }
  62. export interface ApplicationRuntime {
  63. realtime: RealtimeAPI
  64. app: Koa
  65. config: ServerConfig
  66. csrf: CsrfProtection
  67. pairings: NodePairingStore
  68. upstream: UpstreamClient
  69. uploads: UploadStore
  70. updates: SystemUpdateManager
  71. auth: LocalAuthStore
  72. upstreamSession: UpstreamServiceSession
  73. accountPairings: AccountLoginPairingStore
  74. profileIdentities: UpstreamProfileIdentityService
  75. push: PushCoordinator
  76. apnsConfiguration: APNsConfigurationManager
  77. fcmConfiguration: FCMConfigurationManager
  78. allowedHostsConfiguration: AllowedHostsConfigurationManager
  79. pushEventCoordinator: PushCoordinatorEventAdapter
  80. chatPushJobs: ChatPushJobManager
  81. groupPushEvents: GroupPushEventWatcher
  82. close(): void
  83. }
  84. function isMutation(method: string): boolean {
  85. return !['GET', 'HEAD', 'OPTIONS'].includes(method)
  86. }
  87. function persistentCsrfSecret(home: string, override?: Buffer): Buffer {
  88. if (override) return override
  89. const path = join(home, 'csrf-secret.bin')
  90. try {
  91. const secret = readFileSync(path)
  92. if (secret.byteLength === 32) return secret
  93. throw new Error('CSRF secret has an invalid length')
  94. } catch (error) {
  95. if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
  96. }
  97. mkdirSync(home, { recursive: true, mode: 0o700 })
  98. const generated = randomBytes(32)
  99. try {
  100. writeFileSync(path, generated, { mode: 0o600, flag: 'wx' })
  101. return generated
  102. } catch (error) {
  103. // Another 8800 process can finish first during launchd hand-off. Never
  104. // rotate its secret; use the one it safely wrote instead.
  105. if ((error as NodeJS.ErrnoException).code === 'EEXIST') return readFileSync(path)
  106. throw error
  107. }
  108. }
  109. export function createApplication(options: ApplicationOptions = {}): ApplicationRuntime {
  110. const config = options.config ?? loadServerConfig()
  111. const runtimeAllowedHosts = new Set(config.allowedHosts)
  112. config.allowedHosts = runtimeAllowedHosts
  113. const app = new Koa()
  114. app.proxy = false
  115. const csrf = new CsrfProtection(persistentCsrfSecret(config.home, options.csrfSecret), Boolean(config.tlsCert))
  116. const pairings = options.pairings ?? new NodePairingStore(config.home)
  117. const upstream = new UpstreamClient(config.upstream, options.fetchImpl, Boolean(config.tlsCert))
  118. const uploads = options.uploads ?? new UploadStore(config.home)
  119. const updates = options.updates ?? new SystemUpdateManager(config)
  120. const auth = options.auth ?? new LocalAuthStore(config.home, Boolean(config.tlsCert))
  121. if (config.superviseDashboard && !config.upstreamUsername && !config.upstreamPassword) {
  122. auth.ensureUpstreamCredentials()
  123. }
  124. const configuredUpstreamCredentials = config.upstreamUsername && config.upstreamPassword
  125. ? { username: config.upstreamUsername, password: config.upstreamPassword }
  126. : undefined
  127. const upstreamSession = options.upstreamSession ?? new UpstreamServiceSession(
  128. upstream,
  129. () => auth.upstreamCredentials(configuredUpstreamCredentials),
  130. )
  131. const accountPairings = options.accountPairings ?? new AccountLoginPairingStore()
  132. const profileIdentities = options.profileIdentities
  133. ?? new UpstreamProfileIdentityService(config, upstreamSession)
  134. const initialAPNsSettings: APNsConfigurationSnapshot = config.apnsSettings ?? {
  135. source: config.apns || config.apnsConfigurationError ? 'environment' : 'none',
  136. editable: !(config.apns || config.apnsConfigurationError),
  137. ...(config.apns ? {
  138. input: {
  139. ...config.apns,
  140. environments: [...(config.apns.environments ?? ['development', 'production'])],
  141. },
  142. config: config.apns,
  143. } : {}),
  144. warnings: [],
  145. ...(config.apnsConfigurationError ? { configurationError: config.apnsConfigurationError } : {}),
  146. }
  147. const apnsConfiguration = options.apnsConfiguration
  148. ?? new APNsConfigurationManager(config.home, initialAPNsSettings)
  149. const initialFCMSettings: FCMConfigurationSnapshot = config.fcmSettings ?? {
  150. source: config.fcm || config.fcmConfigurationError ? 'environment' : 'none',
  151. editable: !(config.fcm || config.fcmConfigurationError),
  152. ...(config.fcm ? { input: { ...config.fcm }, config: config.fcm } : {}),
  153. warnings: [],
  154. ...(config.fcmConfigurationError ? { configurationError: config.fcmConfigurationError } : {}),
  155. }
  156. const fcmConfiguration = options.fcmConfiguration
  157. ?? new FCMConfigurationManager(config.home, initialFCMSettings)
  158. const initialAllowedHostsSettings: AllowedHostsConfigurationSnapshot = config.allowedHostsSettings ?? {
  159. source: runtimeAllowedHosts.size ? 'environment' : 'none',
  160. hosts: [...runtimeAllowedHosts],
  161. editableHosts: [],
  162. environmentHosts: [...runtimeAllowedHosts],
  163. }
  164. const allowedHostsConfiguration = options.allowedHostsConfiguration
  165. ?? new AllowedHostsConfigurationManager(config.home, initialAllowedHostsSettings, runtimeAllowedHosts)
  166. const push = options.push ?? new PushCoordinator({
  167. home: config.home,
  168. apns: config.apns,
  169. apnsConfigurationError: config.apnsConfigurationError,
  170. fcm: config.fcm,
  171. fcmConfigurationError: config.fcmConfigurationError,
  172. isUserActive: userID => auth.isUserActive(userID),
  173. userAuthorizationVersion: userID => auth.pushAuthorizationVersion(userID),
  174. })
  175. const pushEventCoordinator = new PushCoordinatorEventAdapter(push, upstreamSession)
  176. const chatPushJobs = new ChatPushJobManager(config, upstreamSession, pushEventCoordinator, {
  177. resolver: new HermesChatNotificationResolver(
  178. upstreamSession,
  179. (localUserID, prompt) => push.promptDigest(localUserID, prompt),
  180. ),
  181. })
  182. const groupPushEvents = new GroupPushEventWatcher(
  183. new HermesGroupEventSource(config, upstreamSession),
  184. pushEventCoordinator,
  185. )
  186. const realtime = new RealtimeAPI(config, auth, csrf, pairings, upstream, upstreamSession, {
  187. coordinator: pushEventCoordinator,
  188. resolver: new HermesChatNotificationResolver(upstreamSession, (user, prompt) => push.promptDigest(user, prompt)),
  189. })
  190. chatPushJobs.setTransportFactory(realtime.recoveryTransport, job => realtime.ownsPushJob(job.id))
  191. try {
  192. uploads.cleanupUncommitted()
  193. } catch {
  194. // A locked cleanup should not prevent startup; upload operations will
  195. // still surface the underlying storage error when used.
  196. }
  197. app.use(async (ctx, next) => {
  198. try {
  199. await next()
  200. } catch (error) {
  201. if (error instanceof UpstreamHttpError) {
  202. ctx.status = error.response.status
  203. ctx.type = 'application/json; charset=utf-8'
  204. ctx.body = { error: `Hermes returned HTTP ${error.response.status}`, code: error.code }
  205. return
  206. }
  207. const status = error instanceof HttpError
  208. ? error.status
  209. : typeof (error as { status?: unknown })?.status === 'number'
  210. ? Number((error as { status: number }).status)
  211. : 500
  212. const expose = status < 500 || error instanceof HttpError
  213. ctx.status = status
  214. ctx.type = 'application/json; charset=utf-8'
  215. ctx.body = {
  216. error: expose ? errorMessage(error) : 'Internal server error',
  217. code: error instanceof HttpError ? error.code : 'internal_error',
  218. }
  219. if (status >= 500 && !config.production) ctx.app.emit('error', error, ctx)
  220. }
  221. })
  222. app.use(async (ctx, next) => {
  223. applySecurityHeaders(ctx, Boolean(config.tlsCert), config.allowedHosts)
  224. if (!isAllowedHostHeader(ctx.get('host'), config)) {
  225. throw new HttpError(421, 'Request Host is not allowed', 'invalid_host')
  226. }
  227. await next()
  228. })
  229. app.use(async (ctx, next) => {
  230. if (ctx.path.startsWith('/api/app/') && isMutation(ctx.method)) {
  231. const secure = ctx.secure || Boolean(config.tlsCert)
  232. if (!isExactOrigin(ctx.get('origin'), ctx.get('host'), secure, config.allowedHosts)) {
  233. throw new HttpError(403, 'Request Origin is not allowed', 'invalid_origin')
  234. }
  235. if (!csrf.verify(ctx.get('cookie'), ctx.get('x-csrf-token'))) {
  236. throw new HttpError(403, 'CSRF token is invalid or missing', 'invalid_csrf')
  237. }
  238. }
  239. await next()
  240. })
  241. app.use(realtime.middleware())
  242. app.use(bodyParser({
  243. encoding: 'utf-8',
  244. enableTypes: ['json'],
  245. parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
  246. jsonLimit: '2mb',
  247. onError: (error, ctx) => {
  248. throw new HttpError(ctx.status === 413 || (error as { status?: number }).status === 413 ? 413 : 400, 'Invalid JSON request body', 'invalid_json_body')
  249. },
  250. }))
  251. app.use(async (ctx, next) => {
  252. ctx.state.localAuth = auth
  253. ctx.state.upstreamSession = upstreamSession
  254. const anonymous = ctx.path === '/api/app/bootstrap'
  255. || ctx.path === '/api/app/login'
  256. || ctx.path === '/api/status'
  257. || ctx.path === '/api/auth/providers'
  258. || ctx.path === '/auth/password-login'
  259. || ctx.path.startsWith('/api/pair/v1/')
  260. || ctx.path.startsWith('/api/account-pair/v1/')
  261. || ctx.path === '/healthz'
  262. || ctx.path === '/readyz'
  263. if (ctx.path.startsWith('/api/app/') && !anonymous) {
  264. const allowPasswordChange = ctx.path === '/api/app/account/credentials'
  265. || ctx.path === '/api/app/logout'
  266. ctx.state.localUser = auth.require(ctx, allowPasswordChange)
  267. const adminOnly = ctx.path.startsWith('/api/app/admin/')
  268. || ctx.path.startsWith('/api/app/system/')
  269. || ctx.path.startsWith('/api/app/plugins/')
  270. || ctx.path.startsWith('/api/app/pairings')
  271. || ctx.path.startsWith('/api/app/paired-devices')
  272. || ctx.path.startsWith('/api/app/groups/nodes')
  273. if (adminOnly) ctx.state.localUser = auth.requireAdmin(ctx)
  274. }
  275. const gatewayCompatibility = (ctx.path.startsWith('/api/') && !ctx.path.startsWith('/api/app/')
  276. && !ctx.path.startsWith('/api/pair/'))
  277. || ctx.path === '/auth/logout'
  278. if (gatewayCompatibility && !anonymous) {
  279. const allowPasswordChange = ctx.path === '/api/auth/me' || ctx.path === '/api/profiles'
  280. || ctx.path === '/api/account/credentials'
  281. ctx.state.localUser = auth.require(ctx, allowPasswordChange)
  282. if (isMutation(ctx.method)) {
  283. const origin = ctx.get('origin')
  284. if (origin && !isExactOrigin(origin, ctx.get('host'), ctx.secure || Boolean(config.tlsCert), config.allowedHosts)) {
  285. throw new HttpError(403, 'Request Origin is not allowed', 'invalid_origin')
  286. }
  287. }
  288. }
  289. await next()
  290. })
  291. const router = createApiRouter({
  292. config,
  293. csrf,
  294. upstream,
  295. pairings,
  296. uploads,
  297. updates,
  298. auth,
  299. upstreamSession,
  300. accountPairings,
  301. profileIdentities,
  302. push,
  303. apnsConfiguration,
  304. fcmConfiguration,
  305. allowedHostsConfiguration,
  306. })
  307. app.use(router.routes())
  308. app.use(router.allowedMethods({ throw: false }))
  309. app.use(async (ctx, next) => {
  310. if (ctx.path === '/api' || ctx.path.startsWith('/api/') || ctx.path.startsWith('/ws/')) {
  311. ctx.status = 404
  312. ctx.type = 'application/json; charset=utf-8'
  313. ctx.body = { error: 'Route not found', code: 'not_found' }
  314. return
  315. }
  316. await next()
  317. })
  318. return {
  319. realtime,
  320. app,
  321. config,
  322. csrf,
  323. pairings,
  324. upstream,
  325. uploads,
  326. updates,
  327. auth,
  328. upstreamSession,
  329. accountPairings,
  330. profileIdentities,
  331. push,
  332. apnsConfiguration,
  333. fcmConfiguration,
  334. allowedHostsConfiguration,
  335. pushEventCoordinator,
  336. chatPushJobs,
  337. groupPushEvents,
  338. close: () => {
  339. realtime.close()
  340. chatPushJobs.stop()
  341. groupPushEvents.stop()
  342. push.close()
  343. uploads.close()
  344. },
  345. }
  346. }
  347. export interface NodeServerRuntime {
  348. server: HttpServer
  349. close(): Promise<void>
  350. }
  351. export function createNodeServer(runtime: ApplicationRuntime): NodeServerRuntime {
  352. const { config } = runtime
  353. const server: HttpServer = config.tlsCert && config.tlsKey
  354. ? createHttpsServer({
  355. cert: readFileSync(config.tlsCert),
  356. key: readFileSync(config.tlsKey),
  357. }, runtime.app.callback())
  358. : createHttpServer(runtime.app.callback())
  359. const synchronizePushObservers = (enabled = runtime.push.isAnyProviderEnabled()) => {
  360. if (enabled) {
  361. runtime.chatPushJobs.start()
  362. runtime.groupPushEvents.start()
  363. } else {
  364. runtime.chatPushJobs.stop()
  365. runtime.groupPushEvents.stop()
  366. }
  367. }
  368. synchronizePushObservers()
  369. const removePushConfigurationListener = runtime.push.onEnabledChange(synchronizePushObservers)
  370. const removeWebSockets = runtime.realtime.rejectLegacyUpgrades(server)
  371. return {
  372. server,
  373. close: async () => {
  374. removePushConfigurationListener()
  375. removeWebSockets()
  376. runtime.realtime.close()
  377. await new Promise<void>((resolve, reject) => {
  378. if (!server.listening) {
  379. resolve()
  380. return
  381. }
  382. server.close((error) => error ? reject(error) : resolve())
  383. })
  384. runtime.close()
  385. },
  386. }
  387. }