@adaptic/backend-legacy 0.0.1058 → 0.0.1059

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.
@@ -65,6 +65,7 @@ var __importStar = (this && this.__importStar) || (function () {
65
65
  })();
66
66
  Object.defineProperty(exports, "__esModule", { value: true });
67
67
  exports.MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = exports.MAX_SERVICE_TOKEN_LIFETIME_SEC = exports.MINIMUM_SERVICE_SECRET_LENGTH = exports.SERVICE_TOKEN_AUDIENCE = exports.SERVICE_TOKEN_ISSUER = void 0;
68
+ exports.assertServiceSecretsDistinctFromJwtSecret = assertServiceSecretsDistinctFromJwtSecret;
68
69
  exports.verifyServiceToken = verifyServiceToken;
69
70
  const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
70
71
  const jwtConfig_1 = require("../config/jwtConfig.cjs");
@@ -170,6 +171,31 @@ function resolveServiceSecrets() {
170
171
  }
171
172
  return slots;
172
173
  }
174
+ const SECRET_COLLISION_MESSAGE = '[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
175
+ 'These MUST be distinct secrets — sharing them collapses the ' +
176
+ 'separation between end-user and service identity. Refusing to ' +
177
+ 'verify service tokens until they differ.';
178
+ function collidesWithUserSecret(slots) {
179
+ return slots.some(({ secret }) => secret === jwtConfig_1.jwtSecret);
180
+ }
181
+ /**
182
+ * Boot-time guard: refuse to start when a provisioned service secret (either
183
+ * slot) equals `JWT_SECRET`.
184
+ *
185
+ * `verifyServiceToken` already refuses per request, but that surfaces only as
186
+ * a stream of 401s on service calls after a deploy. Failing at boot puts the
187
+ * misconfiguration on the deploy, where it is caught before traffic. An
188
+ * unprovisioned service path is not an error — it is simply disabled.
189
+ *
190
+ * @throws {Error} When a service secret collides with the user-session secret.
191
+ */
192
+ function assertServiceSecretsDistinctFromJwtSecret() {
193
+ const slots = resolveServiceSecrets();
194
+ if (slots !== null && collidesWithUserSecret(slots)) {
195
+ logger_1.logger.error(SECRET_COLLISION_MESSAGE);
196
+ throw new Error('BACKEND_SERVICE_JWT_SECRET (or _PREVIOUS) must differ from JWT_SECRET.');
197
+ }
198
+ }
173
199
  /**
174
200
  * Verify a token's signature and bound claims against one secret.
175
201
  *
@@ -246,11 +272,8 @@ function verifyServiceToken(token, nowMs = Date.now()) {
246
272
  // which bypasses every tenancy scope and role gate. Refuse rather than
247
273
  // silently operate with the separation collapsed. This holds for the
248
274
  // previous slot too: a rotation must not reopen the hole.
249
- if (slots.some(({ secret }) => secret === jwtConfig_1.jwtSecret)) {
250
- logger_1.logger.error('[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
251
- 'These MUST be distinct secrets — sharing them collapses the ' +
252
- 'separation between end-user and service identity. Refusing to ' +
253
- 'verify service tokens until they differ.');
275
+ if (collidesWithUserSecret(slots)) {
276
+ logger_1.logger.error(SECRET_COLLISION_MESSAGE);
254
277
  throw new token_verifier_1.AuthError('invalid_token', 'misconfigured');
255
278
  }
256
279
  let payload = null;
@@ -75,6 +75,18 @@ export declare const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
75
75
  * rotation.
76
76
  */
77
77
  export type ServiceKeySlot = 'primary' | 'previous';
78
+ /**
79
+ * Boot-time guard: refuse to start when a provisioned service secret (either
80
+ * slot) equals `JWT_SECRET`.
81
+ *
82
+ * `verifyServiceToken` already refuses per request, but that surfaces only as
83
+ * a stream of 401s on service calls after a deploy. Failing at boot puts the
84
+ * misconfiguration on the deploy, where it is caught before traffic. An
85
+ * unprovisioned service path is not an error — it is simply disabled.
86
+ *
87
+ * @throws {Error} When a service secret collides with the user-session secret.
88
+ */
89
+ export declare function assertServiceSecretsDistinctFromJwtSecret(): void;
78
90
  /**
79
91
  * Attempt to establish a service principal from a bearer token.
80
92
  *
@@ -16,8 +16,13 @@ const MINIMUM_SECRET_LENGTH = 32;
16
16
  const DEV_FALLBACK_SECRET = 'development_secret_key_for_local_testing_only_do_not_use_in_production';
17
17
  const isProduction = process.env.NODE_ENV === 'production';
18
18
  function resolveJwtSecret() {
19
+ var _a, _b;
19
20
  // Try JWT_SECRET first, then fall back to NEXTAUTH_SECRET for compatibility
20
- const secret = process.env.JWT_SECRET || process.env.NEXTAUTH_SECRET;
21
+ // Trimmed so a stray newline or space from a pasted dashboard value cannot
22
+ // make this differ from the same secret as `service-token.ts` reads it (that
23
+ // module trims), which would defeat its JWT_SECRET collision check. A
24
+ // whitespace-only JWT_SECRET counts as unset and falls through.
25
+ const secret = ((_a = process.env.JWT_SECRET) === null || _a === void 0 ? void 0 : _a.trim()) || ((_b = process.env.NEXTAUTH_SECRET) === null || _b === void 0 ? void 0 : _b.trim());
21
26
  if (!secret) {
22
27
  if (isProduction) {
23
28
  logger_1.logger.error('[SECURITY] FATAL: JWT_SECRET environment variable is not set. ' +
@@ -75,6 +75,18 @@ export declare const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
75
75
  * rotation.
76
76
  */
77
77
  export type ServiceKeySlot = 'primary' | 'previous';
78
+ /**
79
+ * Boot-time guard: refuse to start when a provisioned service secret (either
80
+ * slot) equals `JWT_SECRET`.
81
+ *
82
+ * `verifyServiceToken` already refuses per request, but that surfaces only as
83
+ * a stream of 401s on service calls after a deploy. Failing at boot puts the
84
+ * misconfiguration on the deploy, where it is caught before traffic. An
85
+ * unprovisioned service path is not an error — it is simply disabled.
86
+ *
87
+ * @throws {Error} When a service secret collides with the user-session secret.
88
+ */
89
+ export declare function assertServiceSecretsDistinctFromJwtSecret(): void;
78
90
  /**
79
91
  * Attempt to establish a service principal from a bearer token.
80
92
  *
@@ -132,6 +132,31 @@ function resolveServiceSecrets() {
132
132
  }
133
133
  return slots;
134
134
  }
135
+ const SECRET_COLLISION_MESSAGE = '[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
136
+ 'These MUST be distinct secrets — sharing them collapses the ' +
137
+ 'separation between end-user and service identity. Refusing to ' +
138
+ 'verify service tokens until they differ.';
139
+ function collidesWithUserSecret(slots) {
140
+ return slots.some(({ secret }) => secret === jwtSecret);
141
+ }
142
+ /**
143
+ * Boot-time guard: refuse to start when a provisioned service secret (either
144
+ * slot) equals `JWT_SECRET`.
145
+ *
146
+ * `verifyServiceToken` already refuses per request, but that surfaces only as
147
+ * a stream of 401s on service calls after a deploy. Failing at boot puts the
148
+ * misconfiguration on the deploy, where it is caught before traffic. An
149
+ * unprovisioned service path is not an error — it is simply disabled.
150
+ *
151
+ * @throws {Error} When a service secret collides with the user-session secret.
152
+ */
153
+ export function assertServiceSecretsDistinctFromJwtSecret() {
154
+ const slots = resolveServiceSecrets();
155
+ if (slots !== null && collidesWithUserSecret(slots)) {
156
+ logger.error(SECRET_COLLISION_MESSAGE);
157
+ throw new Error('BACKEND_SERVICE_JWT_SECRET (or _PREVIOUS) must differ from JWT_SECRET.');
158
+ }
159
+ }
135
160
  /**
136
161
  * Verify a token's signature and bound claims against one secret.
137
162
  *
@@ -208,11 +233,8 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
208
233
  // which bypasses every tenancy scope and role gate. Refuse rather than
209
234
  // silently operate with the separation collapsed. This holds for the
210
235
  // previous slot too: a rotation must not reopen the hole.
211
- if (slots.some(({ secret }) => secret === jwtSecret)) {
212
- logger.error('[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
213
- 'These MUST be distinct secrets — sharing them collapses the ' +
214
- 'separation between end-user and service identity. Refusing to ' +
215
- 'verify service tokens until they differ.');
236
+ if (collidesWithUserSecret(slots)) {
237
+ logger.error(SECRET_COLLISION_MESSAGE);
216
238
  throw new AuthError('invalid_token', 'misconfigured');
217
239
  }
218
240
  let payload = null;
@@ -14,7 +14,11 @@ const DEV_FALLBACK_SECRET = 'development_secret_key_for_local_testing_only_do_no
14
14
  const isProduction = process.env.NODE_ENV === 'production';
15
15
  function resolveJwtSecret() {
16
16
  // Try JWT_SECRET first, then fall back to NEXTAUTH_SECRET for compatibility
17
- const secret = process.env.JWT_SECRET || process.env.NEXTAUTH_SECRET;
17
+ // Trimmed so a stray newline or space from a pasted dashboard value cannot
18
+ // make this differ from the same secret as `service-token.ts` reads it (that
19
+ // module trims), which would defeat its JWT_SECRET collision check. A
20
+ // whitespace-only JWT_SECRET counts as unset and falls through.
21
+ const secret = process.env.JWT_SECRET?.trim() || process.env.NEXTAUTH_SECRET?.trim();
18
22
  if (!secret) {
19
23
  if (isProduction) {
20
24
  logger.error('[SECURITY] FATAL: JWT_SECRET environment variable is not set. ' +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaptic/backend-legacy",
3
- "version": "0.0.1058",
3
+ "version": "0.0.1059",
4
4
  "description": "Backend executable CRUD functions with dynamic variables construction, and type definitions for the Adaptic AI platform.",
5
5
  "type": "module",
6
6
  "types": "index.d.ts",