@theia/core 1.74.0-next.6 → 1.74.0-next.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/README.md +4 -4
  2. package/lib/browser/catalog.json +419 -41
  3. package/lib/browser/common-frontend-contribution.d.ts.map +1 -1
  4. package/lib/browser/common-frontend-contribution.js +1 -2
  5. package/lib/browser/common-frontend-contribution.js.map +1 -1
  6. package/lib/browser/keybinding.d.ts.map +1 -1
  7. package/lib/browser/keybinding.js +12 -7
  8. package/lib/browser/keybinding.js.map +1 -1
  9. package/lib/browser/preferences/preference-contribution.d.ts.map +1 -1
  10. package/lib/browser/preferences/preference-contribution.js +9 -1
  11. package/lib/browser/preferences/preference-contribution.js.map +1 -1
  12. package/lib/browser/preferences/preference-service.spec.js +63 -0
  13. package/lib/browser/preferences/preference-service.spec.js.map +1 -1
  14. package/lib/browser/shell/application-shell.d.ts.map +1 -1
  15. package/lib/browser/shell/application-shell.js +19 -26
  16. package/lib/browser/shell/application-shell.js.map +1 -1
  17. package/lib/browser/tree/tree-widget.d.ts +7 -33
  18. package/lib/browser/tree/tree-widget.d.ts.map +1 -1
  19. package/lib/browser/tree/tree-widget.js +7 -54
  20. package/lib/browser/tree/tree-widget.js.map +1 -1
  21. package/lib/browser/widgets/select-component.d.ts +2 -2
  22. package/lib/browser/widgets/select-component.d.ts.map +1 -1
  23. package/lib/browser/widgets/select-component.js +42 -12
  24. package/lib/browser/widgets/select-component.js.map +1 -1
  25. package/lib/common/preferences/index.d.ts +1 -0
  26. package/lib/common/preferences/index.d.ts.map +1 -1
  27. package/lib/common/preferences/index.js +1 -0
  28. package/lib/common/preferences/index.js.map +1 -1
  29. package/lib/common/preferences/preference-scope.d.ts +6 -1
  30. package/lib/common/preferences/preference-scope.d.ts.map +1 -1
  31. package/lib/common/preferences/preference-scope.js +5 -0
  32. package/lib/common/preferences/preference-scope.js.map +1 -1
  33. package/lib/common/preferences/preference-scope.spec.js +5 -3
  34. package/lib/common/preferences/preference-scope.spec.js.map +1 -1
  35. package/lib/common/preferences/preference-service.d.ts +19 -0
  36. package/lib/common/preferences/preference-service.d.ts.map +1 -1
  37. package/lib/common/preferences/preference-service.js +34 -5
  38. package/lib/common/preferences/preference-service.js.map +1 -1
  39. package/lib/common/preferences/session-preference-provider.d.ts +17 -0
  40. package/lib/common/preferences/session-preference-provider.d.ts.map +1 -0
  41. package/lib/common/preferences/session-preference-provider.js +77 -0
  42. package/lib/common/preferences/session-preference-provider.js.map +1 -0
  43. package/lib/common/preferences/session-preference-provider.spec.d.ts +2 -0
  44. package/lib/common/preferences/session-preference-provider.spec.d.ts.map +1 -0
  45. package/lib/common/preferences/session-preference-provider.spec.js +71 -0
  46. package/lib/common/preferences/session-preference-provider.spec.js.map +1 -0
  47. package/lib/electron-node/hosting/electron-backend-hosting-module.d.ts.map +1 -1
  48. package/lib/electron-node/hosting/electron-backend-hosting-module.js +7 -0
  49. package/lib/electron-node/hosting/electron-backend-hosting-module.js.map +1 -1
  50. package/lib/electron-node/hosting/electron-backend-hosting-module.spec.d.ts +2 -0
  51. package/lib/electron-node/hosting/electron-backend-hosting-module.spec.d.ts.map +1 -0
  52. package/lib/electron-node/hosting/electron-backend-hosting-module.spec.js +39 -0
  53. package/lib/electron-node/hosting/electron-backend-hosting-module.spec.js.map +1 -0
  54. package/lib/node/hosting/backend-hosting-module.d.ts.map +1 -1
  55. package/lib/node/hosting/backend-hosting-module.js +4 -1
  56. package/lib/node/hosting/backend-hosting-module.js.map +1 -1
  57. package/lib/node/hosting/browser-connection-token.d.ts +29 -1
  58. package/lib/node/hosting/browser-connection-token.d.ts.map +1 -1
  59. package/lib/node/hosting/browser-connection-token.js +25 -1
  60. package/lib/node/hosting/browser-connection-token.js.map +1 -1
  61. package/lib/node/hosting/browser-connection-token.spec.js +70 -0
  62. package/lib/node/hosting/browser-connection-token.spec.js.map +1 -1
  63. package/lib/node/index.d.ts +1 -0
  64. package/lib/node/index.d.ts.map +1 -1
  65. package/lib/node/index.js +3 -1
  66. package/lib/node/index.js.map +1 -1
  67. package/package.json +9 -9
  68. package/src/browser/common-frontend-contribution.ts +1 -2
  69. package/src/browser/keybinding.ts +12 -7
  70. package/src/browser/preferences/preference-contribution.ts +9 -1
  71. package/src/browser/preferences/preference-service.spec.ts +72 -0
  72. package/src/browser/shell/application-shell.ts +19 -25
  73. package/src/browser/style/select-component.css +16 -0
  74. package/src/browser/style/sidepanel.css +5 -0
  75. package/src/browser/style/status-bar.css +1 -1
  76. package/src/browser/tree/tree-widget.tsx +12 -78
  77. package/src/browser/widgets/select-component.tsx +41 -12
  78. package/src/common/i18n/nls.metadata.json +4149 -888
  79. package/src/common/preferences/index.ts +1 -0
  80. package/src/common/preferences/preference-scope.spec.ts +5 -3
  81. package/src/common/preferences/preference-scope.ts +6 -1
  82. package/src/common/preferences/preference-service.ts +39 -5
  83. package/src/common/preferences/session-preference-provider.spec.ts +78 -0
  84. package/src/common/preferences/session-preference-provider.ts +69 -0
  85. package/src/electron-node/hosting/electron-backend-hosting-module.spec.ts +42 -0
  86. package/src/electron-node/hosting/electron-backend-hosting-module.ts +8 -0
  87. package/src/node/hosting/backend-hosting-module.ts +5 -2
  88. package/src/node/hosting/browser-connection-token.spec.ts +92 -0
  89. package/src/node/hosting/browser-connection-token.ts +45 -1
  90. package/src/node/index.ts +1 -0
@@ -16,6 +16,7 @@
16
16
 
17
17
  export * from './defaults-preference-provider';
18
18
  export * from './preference-language-override-service';
19
+ export * from './session-preference-provider';
19
20
  export * from './preference-provider-impl';
20
21
  export * from './preference-provider';
21
22
  export * from './preference-schema-service';
@@ -20,11 +20,11 @@ import { PreferenceScope } from './preference-scope';
20
20
  describe('PreferenceScope', () => {
21
21
 
22
22
  it('getScopes() should return numbers from broadest to narrowest', () => {
23
- expect(PreferenceScope.getScopes()).deep.equal([0, 1, 2, 3]);
23
+ expect(PreferenceScope.getScopes()).deep.equal([0, 1, 2, 3, 4]);
24
24
  });
25
25
 
26
26
  it('getReversedScopes() should return numbers from narrowest to broadest', () => {
27
- expect(PreferenceScope.getReversedScopes()).deep.equal([3, 2, 1, 0]);
27
+ expect(PreferenceScope.getReversedScopes()).deep.equal([4, 3, 2, 1, 0]);
28
28
  });
29
29
 
30
30
  it('getScopeNames() should return the names of scopes broader than the current one', () => {
@@ -36,11 +36,13 @@ describe('PreferenceScope', () => {
36
36
  expect(PreferenceScope.is(PreferenceScope.User)).to.be.true;
37
37
  expect(PreferenceScope.is(PreferenceScope.Workspace)).to.be.true;
38
38
  expect(PreferenceScope.is(PreferenceScope.Folder)).to.be.true;
39
+ expect(PreferenceScope.is(PreferenceScope.Session)).to.be.true;
39
40
  expect(PreferenceScope.is(0)).to.be.true;
40
41
  expect(PreferenceScope.is(1)).to.be.true;
41
42
  expect(PreferenceScope.is(2)).to.be.true;
42
43
  expect(PreferenceScope.is(3)).to.be.true;
43
- expect(PreferenceScope.is(4)).to.be.false;
44
+ expect(PreferenceScope.is(4)).to.be.true;
45
+ expect(PreferenceScope.is(5)).to.be.false;
44
46
  expect(PreferenceScope.is(-1)).to.be.false;
45
47
  expect(PreferenceScope.is({})).to.be.false;
46
48
  expect(PreferenceScope.is('Default')).to.be.false;
@@ -23,7 +23,12 @@ export enum PreferenceScope {
23
23
  Default,
24
24
  User,
25
25
  Workspace,
26
- Folder
26
+ Folder,
27
+ /**
28
+ * Process-lifetime, in-memory scope used by `--session-preference` CLI overrides.
29
+ * Has the highest precedence and is never persisted to disk.
30
+ */
31
+ Session
27
32
  }
28
33
 
29
34
  export namespace PreferenceScope {
@@ -254,6 +254,10 @@ export interface PreferenceInspection<T = JSONValue> {
254
254
  * Value in folder scope.
255
255
  */
256
256
  workspaceFolderValue: T | undefined,
257
+ /**
258
+ * Value in session scope (in-memory, set via `--session-preference`).
259
+ */
260
+ sessionValue?: T | undefined,
257
261
  /**
258
262
  * The value that is active, i.e. the value set in the lowest scope available.
259
263
  */
@@ -453,11 +457,37 @@ export class PreferenceServiceImpl implements PreferenceService {
453
457
  }
454
458
  const provider = this.getProvider(resolvedScope);
455
459
  if (provider && await provider.setPreference(preferenceName, value, resourceUri)) {
460
+ await this.evictSessionOverride(preferenceName, resolvedScope);
456
461
  return;
457
462
  }
458
463
  throw new Error(`Unable to write to ${PreferenceScope[resolvedScope]} Settings.`);
459
464
  }
460
465
 
466
+ /**
467
+ * When a preference is written to a persistent scope, drop any in-memory session
468
+ * override (set via `--session-preference`) for the same key so that the explicit
469
+ * change takes effect for the remainder of the session. Without this, the session
470
+ * scope would keep shadowing the user's change because it has higher precedence.
471
+ *
472
+ * Note: this fires for any write to a persistent scope, including programmatic
473
+ * writes from extensions. Extensions that race with startup CLI overrides will
474
+ * therefore clear them. This is intentional: a programmatic write is "explicit"
475
+ * from the preference system's point of view, but consumers should be aware.
476
+ *
477
+ * Session preferences are global (no per-resource scoping), so `resourceUri` is
478
+ * not threaded through.
479
+ */
480
+ protected async evictSessionOverride(preferenceName: string, writtenScope: PreferenceScope): Promise<void> {
481
+ if (writtenScope === PreferenceScope.Session || writtenScope === PreferenceScope.Default) {
482
+ return;
483
+ }
484
+ const sessionProvider = this.getProvider(PreferenceScope.Session);
485
+ if (sessionProvider && sessionProvider.get(preferenceName) !== undefined) {
486
+ // Passing `undefined` clears the value in the session provider.
487
+ await sessionProvider.setPreference(preferenceName, undefined as unknown as JSONValue);
488
+ }
489
+ }
490
+
461
491
  getBoolean(preferenceName: string): boolean | undefined;
462
492
  getBoolean(preferenceName: string, defaultValue: boolean): boolean;
463
493
  getBoolean(preferenceName: string, defaultValue: boolean, resourceUri: string): boolean;
@@ -499,10 +529,11 @@ export class PreferenceServiceImpl implements PreferenceService {
499
529
  const globalValue = this.inspectInScope<T>(preferenceName, PreferenceScope.User, resourceUri, forceLanguageOverride);
500
530
  const workspaceValue = this.inspectInScope<T>(preferenceName, PreferenceScope.Workspace, resourceUri, forceLanguageOverride);
501
531
  const workspaceFolderValue = this.inspectInScope<T>(preferenceName, PreferenceScope.Folder, resourceUri, forceLanguageOverride);
532
+ const sessionValue = this.inspectInScope<T>(preferenceName, PreferenceScope.Session, resourceUri, forceLanguageOverride);
502
533
 
503
- const valueApplied = workspaceFolderValue ?? workspaceValue ?? globalValue ?? defaultValue;
534
+ const valueApplied = sessionValue ?? workspaceFolderValue ?? workspaceValue ?? globalValue ?? defaultValue;
504
535
 
505
- return { preferenceName, defaultValue, globalValue, workspaceValue, workspaceFolderValue, value: valueApplied };
536
+ return { preferenceName, defaultValue, globalValue, workspaceValue, workspaceFolderValue, sessionValue, value: valueApplied };
506
537
  }
507
538
 
508
539
  inspectInScope<T extends JSONValue>(preferenceName: string, scope: PreferenceScope, resourceUri?: string, forceLanguageOverride?: boolean): T | undefined {
@@ -526,6 +557,8 @@ export class PreferenceServiceImpl implements PreferenceService {
526
557
  return inspection.workspaceValue;
527
558
  case PreferenceScope.Folder:
528
559
  return inspection.workspaceFolderValue;
560
+ case PreferenceScope.Session:
561
+ return inspection.sessionValue;
529
562
  }
530
563
  unreachable(scope, 'Not all PreferenceScope enum variants handled.');
531
564
  }
@@ -551,9 +584,10 @@ export class PreferenceServiceImpl implements PreferenceService {
551
584
  }
552
585
 
553
586
  // Scopes in ascending order of scope breadth.
554
- const allScopes = [...this.schemaService.validScopes].reverse();
555
- // Get rid of Default scope. We can't set anything there.
556
- allScopes.pop();
587
+ const allScopes = [...this.schemaService.validScopes].reverse()
588
+ // Default is read-only, and Session is populated only by `--session-preference`,
589
+ // so neither is a valid target for a regular user edit.
590
+ .filter(scope => scope !== PreferenceScope.Default && scope !== PreferenceScope.Session);
557
591
 
558
592
  const isScopeDefined = (scope: PreferenceScope) => this.getScopedValueFromInspection(inspection, scope) !== undefined;
559
593
 
@@ -0,0 +1,78 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { expect } from 'chai';
18
+ import { SessionPreferenceProvider } from './session-preference-provider';
19
+ import { PreferenceScope } from './preference-scope';
20
+
21
+ describe('SessionPreferenceProvider', () => {
22
+
23
+ let provider: SessionPreferenceProvider;
24
+
25
+ beforeEach(() => {
26
+ provider = new SessionPreferenceProvider();
27
+ // PostConstruct emulation
28
+ (provider as unknown as { init(): void }).init();
29
+ });
30
+
31
+ it('claims only the Session scope', () => {
32
+ expect(provider.canHandleScope(PreferenceScope.Session)).to.be.true;
33
+ expect(provider.canHandleScope(PreferenceScope.User)).to.be.false;
34
+ expect(provider.canHandleScope(PreferenceScope.Workspace)).to.be.false;
35
+ expect(provider.canHandleScope(PreferenceScope.Folder)).to.be.false;
36
+ expect(provider.canHandleScope(PreferenceScope.Default)).to.be.false;
37
+ });
38
+
39
+ it('starts empty', () => {
40
+ expect(provider.getPreferences()).to.deep.equal({});
41
+ });
42
+
43
+ it('stores set values in memory and exposes them via get/resolve', async () => {
44
+ const ok = await provider.setPreference('editor.fontSize', 18);
45
+ expect(ok).to.be.true;
46
+ expect(provider.get('editor.fontSize')).to.equal(18);
47
+ expect(provider.resolve('editor.fontSize').value).to.equal(18);
48
+ expect(provider.getPreferences()).to.deep.equal({ 'editor.fontSize': 18 });
49
+ });
50
+
51
+ it('emits a change event on set', async () => {
52
+ let received: { name: string; newValue: unknown; oldValue: unknown; scope: PreferenceScope } | undefined;
53
+ provider.onDidPreferencesChanged(changes => {
54
+ const key = Object.keys(changes)[0];
55
+ received = {
56
+ name: changes[key].preferenceName,
57
+ newValue: changes[key].newValue,
58
+ oldValue: changes[key].oldValue,
59
+ scope: changes[key].scope
60
+ };
61
+ });
62
+ await provider.setPreference('foo', 'bar');
63
+ expect(received).to.deep.equal({ name: 'foo', newValue: 'bar', oldValue: undefined, scope: PreferenceScope.Session });
64
+ });
65
+
66
+ it('removes a value when set to undefined', async () => {
67
+ await provider.setPreference('foo', 'bar');
68
+ const removed = await provider.setPreference('foo', undefined as never);
69
+ expect(removed).to.be.true;
70
+ expect(provider.get('foo')).to.equal(undefined);
71
+ expect(provider.getPreferences()).to.deep.equal({});
72
+ });
73
+
74
+ it('returns false when removing a key that was never set', async () => {
75
+ const removed = await provider.setPreference('missing', undefined as never);
76
+ expect(removed).to.be.false;
77
+ });
78
+ });
@@ -0,0 +1,69 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { JSONObject, JSONValue } from '@lumino/coreutils';
18
+ import { injectable, postConstruct } from 'inversify';
19
+ import { PreferenceProvider, PreferenceProviderDataChange } from './preference-provider';
20
+ import { PreferenceProviderImpl } from './preference-provider-impl';
21
+ import { PreferenceScope } from './preference-scope';
22
+
23
+ /**
24
+ * In-memory preference provider used for process-lifetime overrides such as those
25
+ * supplied via the `--session-preference` CLI option. Values live in memory only
26
+ * and are dropped when the process exits.
27
+ */
28
+ @injectable()
29
+ export class SessionPreferenceProvider extends PreferenceProviderImpl implements PreferenceProvider {
30
+
31
+ protected readonly preferences = new Map<string, JSONValue>();
32
+
33
+ @postConstruct()
34
+ protected init(): void {
35
+ this._ready.resolve();
36
+ }
37
+
38
+ override canHandleScope(scope: PreferenceScope): boolean {
39
+ return scope === PreferenceScope.Session;
40
+ }
41
+
42
+ getPreferences(): JSONObject {
43
+ const result: JSONObject = {};
44
+ for (const [name, value] of this.preferences) {
45
+ result[name] = value;
46
+ }
47
+ return result;
48
+ }
49
+
50
+ async setPreference(key: string, value: JSONValue | undefined): Promise<boolean> {
51
+ const oldValue = this.preferences.get(key);
52
+ if (value === undefined) {
53
+ if (!this.preferences.has(key)) {
54
+ return false;
55
+ }
56
+ this.preferences.delete(key);
57
+ } else {
58
+ this.preferences.set(key, value);
59
+ }
60
+ const change: PreferenceProviderDataChange = {
61
+ preferenceName: key,
62
+ newValue: value,
63
+ oldValue,
64
+ scope: PreferenceScope.Session
65
+ };
66
+ await this.emitPreferencesChangedEvent([change]);
67
+ return true;
68
+ }
69
+ }
@@ -0,0 +1,42 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 EclipseSource GmbH and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { expect } from 'chai';
18
+ import { Container } from 'inversify';
19
+ import electronBackendHostingModule from './electron-backend-hosting-module';
20
+ import { HttpConnectionValidator } from '../../node/hosting/browser-connection-token';
21
+
22
+ describe('electron-backend-hosting-module', () => {
23
+
24
+ it('should bind a no-op HttpConnectionValidator that calls next() unconditionally', () => {
25
+ const container = new Container();
26
+ container.load(electronBackendHostingModule);
27
+ const validator = container.get<HttpConnectionValidator>(HttpConnectionValidator);
28
+
29
+ let nextCalled = false;
30
+ let sentStatus: number | undefined;
31
+ // No cookie provided: the no-op validator must still call next() and never send a status.
32
+ const req = { headers: {} as Record<string, string | undefined> };
33
+ const res = { sendStatus: (status: number) => { sentStatus = status; } };
34
+ const next = (): void => { nextCalled = true; };
35
+
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ validator.validateRequest(req as any, res as any, next as any);
38
+
39
+ expect(nextCalled).to.be.true;
40
+ expect(sentStatus).to.be.undefined;
41
+ });
42
+ });
@@ -16,9 +16,17 @@
16
16
 
17
17
  import { ContainerModule } from 'inversify';
18
18
  import { WsRequestValidatorContribution } from '../../node/ws-request-validators';
19
+ import { HttpConnectionValidator } from '../../node/hosting/browser-connection-token';
19
20
  import { ElectronWsOriginValidator } from './electron-ws-origin-validator';
20
21
 
21
22
  export default new ContainerModule(bind => {
22
23
  bind(ElectronWsOriginValidator).toSelf().inSingletonScope();
23
24
  bind(WsRequestValidatorContribution).toService(ElectronWsOriginValidator);
25
+
26
+ // Electron uses its own `ElectronSecurityToken`, so the connection-token cookie is not used.
27
+ // Bind a no-op validator so consumers can inject it uniformly.
28
+ const noopValidator: HttpConnectionValidator = {
29
+ validateRequest: (_req, _res, next) => next()
30
+ };
31
+ bind(HttpConnectionValidator).toConstantValue(noopValidator);
24
32
  });
@@ -19,7 +19,7 @@ import { BackendApplicationContribution } from '../backend-application';
19
19
  import { WsRequestValidatorContribution } from '../ws-request-validators';
20
20
  import { BackendApplicationHosts } from './backend-application-hosts';
21
21
  import {
22
- BrowserConnectionToken, BrowserConnectionTokenBackendContribution, createBrowserConnectionToken
22
+ BrowserConnectionToken, BrowserConnectionTokenBackendContribution, HttpConnectionValidator, createBrowserConnectionToken
23
23
  } from './browser-connection-token';
24
24
  import { WsOriginValidator } from './ws-origin-validator';
25
25
 
@@ -28,9 +28,12 @@ export default new ContainerModule(bind => {
28
28
  bind(WsOriginValidator).toSelf().inSingletonScope();
29
29
  bind(WsRequestValidatorContribution).toService(WsOriginValidator);
30
30
 
31
- // Cookie-based connection token protects both HTTP and WebSocket endpoints.
31
+ // Cookie-based connection token. It is validated on WebSocket upgrades and bootstrapped
32
+ // (set as a cookie) on every HTTP request; HTTP enforcement is opt-in per route via
33
+ // `HttpConnectionValidator`.
32
34
  bind(BrowserConnectionToken).toConstantValue(createBrowserConnectionToken());
33
35
  bind(BrowserConnectionTokenBackendContribution).toSelf().inSingletonScope();
34
36
  bind(BackendApplicationContribution).toService(BrowserConnectionTokenBackendContribution);
35
37
  bind(WsRequestValidatorContribution).toService(BrowserConnectionTokenBackendContribution);
38
+ bind(HttpConnectionValidator).toService(BrowserConnectionTokenBackendContribution);
36
39
  });
@@ -16,6 +16,7 @@
16
16
 
17
17
  import { expect } from 'chai';
18
18
  import * as http from 'http';
19
+ import { environment } from '../../common';
19
20
  import {
20
21
  BrowserConnectionToken,
21
22
  BrowserConnectionTokenBackendContribution,
@@ -164,4 +165,95 @@ describe('BrowserConnectionTokenBackendContribution', () => {
164
165
  expect(result.cookieCalls[0].value).to.equal(token.value);
165
166
  });
166
167
  });
168
+
169
+ describe('validateRequest (HTTP token enforcement)', () => {
170
+
171
+ interface ValidationResult {
172
+ nextCalled: boolean;
173
+ sentStatus?: number;
174
+ }
175
+
176
+ function callValidate(cookieValue?: string): ValidationResult {
177
+ const contribution = createContribution();
178
+ const result: ValidationResult = { nextCalled: false };
179
+
180
+ const req = { headers: {} as Record<string, string | undefined> };
181
+ if (cookieValue !== undefined) {
182
+ req.headers.cookie = `${BROWSER_TOKEN_COOKIE_NAME}=${cookieValue}`;
183
+ }
184
+
185
+ const res = {
186
+ sendStatus: (status: number) => { result.sentStatus = status; }
187
+ };
188
+
189
+ const next = (): void => { result.nextCalled = true; };
190
+
191
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
+ (contribution as any).validateRequest(req, res, next);
193
+ return result;
194
+ }
195
+
196
+ it('should allow request with valid token cookie', () => {
197
+ const result = callValidate(token.value);
198
+ expect(result.nextCalled).to.be.true;
199
+ expect(result.sentStatus).to.be.undefined;
200
+ });
201
+
202
+ it('should reject request with no cookie', () => {
203
+ const result = callValidate();
204
+ expect(result.nextCalled).to.be.false;
205
+ expect(result.sentStatus).to.equal(403);
206
+ });
207
+
208
+ it('should reject request with invalid token', () => {
209
+ const result = callValidate('wrong-token');
210
+ expect(result.nextCalled).to.be.false;
211
+ expect(result.sentStatus).to.equal(403);
212
+ });
213
+
214
+ it('should reject request with stale token from previous server', () => {
215
+ const staleToken = createBrowserConnectionToken().value;
216
+ const result = callValidate(staleToken);
217
+ expect(result.nextCalled).to.be.false;
218
+ expect(result.sentStatus).to.equal(403);
219
+ });
220
+
221
+ it('should reject request with empty token', () => {
222
+ const result = callValidate('');
223
+ expect(result.nextCalled).to.be.false;
224
+ expect(result.sentStatus).to.equal(403);
225
+ });
226
+ });
227
+
228
+ describe('Electron deployment (no-op)', () => {
229
+
230
+ let originalIsElectron: () => boolean;
231
+
232
+ beforeEach(() => {
233
+ originalIsElectron = environment.electron.is;
234
+ environment.electron.is = () => true;
235
+ });
236
+
237
+ afterEach(() => {
238
+ environment.electron.is = originalIsElectron;
239
+ });
240
+
241
+ it('validateRequest should call next() without a cookie', () => {
242
+ const contribution = createContribution();
243
+ let nextCalled = false;
244
+ let sentStatus: number | undefined;
245
+ const req = { headers: {} as Record<string, string | undefined> };
246
+ const res = { sendStatus: (status: number) => { sentStatus = status; } };
247
+ const next = (): void => { nextCalled = true; };
248
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
249
+ (contribution as any).validateRequest(req, res, next);
250
+ expect(nextCalled).to.be.true;
251
+ expect(sentStatus).to.be.undefined;
252
+ });
253
+
254
+ it('allowWsUpgrade should return true without a cookie', () => {
255
+ const contribution = createContribution();
256
+ expect(contribution.allowWsUpgrade(createRequest())).to.be.true;
257
+ });
258
+ });
167
259
  });
@@ -33,6 +33,26 @@ export interface BrowserConnectionToken {
33
33
  value: string;
34
34
  }
35
35
 
36
+ export const HttpConnectionValidator = Symbol('HttpConnectionValidator');
37
+
38
+ /**
39
+ * Express middleware provider that rejects HTTP requests lacking a valid connection token.
40
+ *
41
+ * The connection-token cookie is only *bootstrapped* globally (see
42
+ * {@link BrowserConnectionTokenBackendContribution}); enforcement is opt-in per route.
43
+ * Security-sensitive HTTP endpoints (e.g. the filesystem upload/download routes) should
44
+ * inject this and apply {@link validateRequest} as route middleware. Non-sensitive routes
45
+ * (the initial HTML page, static assets) must not use it, as they legitimately have no
46
+ * cookie yet on the very first page load.
47
+ */
48
+ export interface HttpConnectionValidator {
49
+ /**
50
+ * Express middleware that calls `next()` when the request carries a valid connection-token
51
+ * cookie (or when running in Electron) and responds with `403` otherwise.
52
+ */
53
+ validateRequest(req: express.Request, res: express.Response, next: express.NextFunction): void;
54
+ }
55
+
36
56
  /**
37
57
  * Validates WebSocket and HTTP requests using a cookie-based connection token.
38
58
  *
@@ -40,13 +60,18 @@ export interface BrowserConnectionToken {
40
60
  * as a `SameSite=Strict; HttpOnly` cookie on the first page load. Cross-origin pages
41
61
  * cannot obtain or send this cookie, so their requests are rejected.
42
62
  *
63
+ * The cookie is *bootstrapped* for every HTTP request (via {@link expressMiddleware}) so that
64
+ * browsers always receive it, but HTTP requests are only *rejected* on routes that opt in to
65
+ * enforcement via {@link validateRequest} (see {@link HttpConnectionValidator}). WebSocket
66
+ * upgrades are always validated (see {@link allowWsUpgrade}).
67
+ *
43
68
  * This complements the origin validator: non-browser callers that omit the Origin
44
69
  * header (e.g. Node.js scripts) still cannot reach the backend without the cookie.
45
70
  *
46
71
  * Skipped in Electron deployments (which use their own `ElectronSecurityToken`).
47
72
  */
48
73
  @injectable()
49
- export class BrowserConnectionTokenBackendContribution implements BackendApplicationContribution, WsRequestValidatorContribution {
74
+ export class BrowserConnectionTokenBackendContribution implements BackendApplicationContribution, WsRequestValidatorContribution, HttpConnectionValidator {
50
75
 
51
76
  @inject(BrowserConnectionToken)
52
77
  protected readonly browserConnectionToken: BrowserConnectionToken;
@@ -84,6 +109,25 @@ export class BrowserConnectionTokenBackendContribution implements BackendApplica
84
109
  return false;
85
110
  }
86
111
 
112
+ /**
113
+ * Reject the request with `403` unless it carries a valid connection-token cookie.
114
+ * Always allows the request in Electron deployments, consistent with {@link allowWsUpgrade}.
115
+ */
116
+ validateRequest(req: express.Request, res: express.Response, next: express.NextFunction): void {
117
+ if (environment.electron.is()) {
118
+ next();
119
+ return;
120
+ }
121
+ const token = this.getTokenFromCookie(req);
122
+ if (token && this.isTokenValid(token)) {
123
+ next();
124
+ return;
125
+ }
126
+ // No cookie or stale/invalid token: reject. Legitimate browsers always have the cookie
127
+ // because it is set on the initial page load, and same-origin requests send it automatically.
128
+ res.sendStatus(403);
129
+ }
130
+
87
131
  protected expressMiddleware(req: express.Request, res: express.Response, next: express.NextFunction): void {
88
132
  const existing = this.getTokenFromCookie(req);
89
133
  if (!existing || !this.isTokenValid(existing)) {
package/src/node/index.ts CHANGED
@@ -23,3 +23,4 @@ export * from './cli';
23
23
  export * from './setting-service';
24
24
  export { FileSystemLocking } from './filesystem-locking';
25
25
  export { BackendRequestAllowedContribution } from './request/backend-request-facade';
26
+ export { HttpConnectionValidator } from './hosting/browser-connection-token';