@bymax-one/nest-auth 1.4.19 → 1.4.20

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.
@@ -5626,11 +5626,25 @@ declare class RolesGuard implements CanActivate {
5626
5626
  * `req.user` before this guard runs.
5627
5627
  *
5628
5628
  * @remarks
5629
- * **Multi-tenant ownership limitation:** This guard does NOT verify that the
5630
- * target resource belongs to the same `tenantId` as the JWT. In multi-tenant
5631
- * deployments the controller or service MUST additionally enforce ownership
5632
- * against `req.user.tenantId` to prevent cross-tenant IDOR. This guard only
5633
- * enforces the *identity* boundary (self vs. other), not the *tenant* boundary.
5629
+ * **Tenant boundary:** when the route captures the tenant under `tenantId`, `tenant_id` or
5630
+ * `tenant`, every capture present must equal the token's `tenantId`; otherwise the request is
5631
+ * refused with `INSUFFICIENT_ROLE` before the self-or-admin decision is made. An id is only
5632
+ * unique within a tenant, and the `admin` role is held inside one, so neither identity nor role
5633
+ * reaches across the boundary. Captures that disagree with one another, are empty, or are arrays
5634
+ * name no tenant the token can equal and are refused; a wildcard capture arrives as an array of
5635
+ * the segments it matched, so it is refused even for an admin.
5636
+ *
5637
+ * **What the tenant check cannot see:** only those three capture names are read. A route that
5638
+ * names the tenant any other way, `{org}` for instance, is decided by identity and role alone, and
5639
+ * the handler must bind that capture to the token's tenant itself, as must a route with no tenant
5640
+ * capture. Where Express resolves a capture name used at two nesting levels, the inner value wins
5641
+ * (measured on Express 5, with or without `mergeParams`), and a capture on a parent router
5642
+ * mounted without `mergeParams` is not visible to the inner route, so the guard sees none.
5643
+ * Nest controllers compose the full path on one route and are not affected by the second.
5644
+ *
5645
+ * **The id:** the `userId` or `id` capture must be one non-empty path segment. An empty value or
5646
+ * one carrying a slash (a decoded `%2F`) is refused with `INSUFFICIENT_ROLE`, for an admin too and
5647
+ * for an owner whose own id contains a slash.
5634
5648
  *
5635
5649
  * **Admin role convention:** The literal `'admin'` role is checked against the
5636
5650
  * configured hierarchy. If the consuming application uses a different convention
@@ -9025,97 +9039,6 @@ declare class MfaService {
9025
9039
  * @throws {@link AuthException} `MFA_STATE_CONFLICT` when another transition holds the lock.
9026
9040
  */
9027
9041
  private transitionMfaRecord;
9028
- /**
9029
- * Whether `stored` decrypts, under the current or a retired key, to `secretBase32`.
9030
- *
9031
- * Compares plaintext, so a record that was only re-encrypted still matches and one holding a
9032
- * different secret does not. A ciphertext no key opens does not match either.
9033
- */
9034
- private holdsSecret;
9035
- /**
9036
- * Encrypts a TOTP secret for storage in the database using AES-256-GCM.
9037
- */
9038
- private encryptSecret;
9039
- /**
9040
- * Decrypts a stored TOTP secret, falling back to keys retired by a rotation.
9041
- *
9042
- * The ciphertext records no key identifier, so without the retired keys, changing
9043
- * `mfa.encryptionKey` makes every stored secret undecryptable — every enrolled user's
9044
- * authenticator stops matching at once, with no way back. AES-GCM authenticates, so a wrong
9045
- * key fails unambiguously rather than returning garbage; trying them in order is safe.
9046
- *
9047
- * Re-throws any decryption failure as an opaque `TOKEN_INVALID` to prevent
9048
- * error-type oracle attacks (callers cannot distinguish format vs. tamper errors).
9049
- */
9050
- private decryptSecret;
9051
- /**
9052
- * Decrypt under the current key, then under each retired one.
9053
- *
9054
- * @param encrypted - The stored ciphertext.
9055
- * @returns The plaintext and whether a retired key produced it — the signal that the record
9056
- * should be re-encrypted under the current key.
9057
- * @throws {@link AuthException} `TOKEN_INVALID` when no key decrypts it.
9058
- */
9059
- private decryptWithRotation;
9060
- /**
9061
- * Safely parses a Redis-stored {@link MfaSetupData} JSON payload.
9062
- *
9063
- * A corrupted or tampered Redis value would otherwise raise an unhandled
9064
- * `SyntaxError` from the service boundary (uncaught 500). We translate the
9065
- * failure into an opaque `MFA_SETUP_REQUIRED`, which mirrors the response
9066
- * the caller would receive if the key were absent — preventing an attacker
9067
- * with Redis write access from distinguishing "no setup pending" from
9068
- * "setup payload corrupted".
9069
- */
9070
- private parseSetupData;
9071
- /**
9072
- * Safely parses the AES-decrypted JSON array of plain-text recovery codes.
9073
- *
9074
- * A decrypted value that fails JSON parsing indicates either a downgrade of
9075
- * the stored payload (highly unlikely, since AES-GCM authenticates the
9076
- * ciphertext) or an internal bug. Both cases are surfaced opaquely as
9077
- * `MFA_SETUP_REQUIRED` so callers do not learn structural details of the
9078
- * encrypted payload.
9079
- */
9080
- private parsePlainRecoveryCodes;
9081
- /**
9082
- * Generates `count` recovery codes in `XXXX-XXXX-XXXX-XXXX-XXXX-XXXX` format
9083
- * (96 bits of entropy per code, hex-encoded).
9084
- *
9085
- * Returns both the plain-text (shown once to the user) and scrypt-hashed
9086
- * versions (stored in the database).
9087
- *
9088
- * @remarks
9089
- * Each code carries 96 bits of entropy (12 random bytes → 24 hex chars grouped
9090
- * as 6 × 4). This is well above the NIST SP 800-63B recommendation for
9091
- * offline-resistant credentials. The previous 12-digit decimal format offered
9092
- * ~40 bits, which relied entirely on the scrypt hash for offline resistance —
9093
- * any database leak would have put the raw codes within offline reach.
9094
- */
9095
- private hashRecoveryCodes;
9096
- /**
9097
- * Derives the stored digest for a recovery code: a keyed HMAC-SHA-256, hex encoded.
9098
- *
9099
- * A recovery code is 96 bits of CSPRNG output, not a human-chosen password, so a
9100
- * memory-hard KDF buys nothing against it — there is no dictionary to walk and brute
9101
- * forcing 2^96 is out of reach whatever the hash costs. What the KDF did buy was an
9102
- * attacker-reachable CPU amplifier: a challenge submitting a wrong recovery code scanned
9103
- * every stored digest, so one request cost as many scrypt derivations as the user had
9104
- * codes. The keyed MAC is the right primitive here — the secret key is what stops an
9105
- * offline attacker precomputing digests from a leaked table, and it costs microseconds.
9106
- */
9107
- private digestRecoveryCode;
9108
- /**
9109
- * Compares a submitted recovery code against every stored digest.
9110
- *
9111
- * One storage format only: a keyed MAC over the code. Every digest is compared in constant
9112
- * time and the scan always runs to completion — including the step that picks the winner out
9113
- * of the recorded results — so neither the position of a match nor the number of codes still
9114
- * unused is observable in the response time.
9115
- *
9116
- * @returns Index of the first matching digest, or `-1` if none match.
9117
- */
9118
- private verifyRecoveryCode;
9119
9042
  /**
9120
9043
  * Claims a matched recovery code for exactly one challenge.
9121
9044
  *
@@ -9143,41 +9066,6 @@ declare class MfaService {
9143
9066
  * @returns `true` when this challenge is the one that claimed it.
9144
9067
  */
9145
9068
  private claimRecoveryCode;
9146
- /**
9147
- * Returns a `SafeAuthUser` projection of a dashboard user, stripping credentials
9148
- * and MFA secret fields before passing to hooks or email providers.
9149
- */
9150
- private toSafeUser;
9151
- /**
9152
- * Returns a `SafeAuthPlatformUser` projection of a platform admin, stripping
9153
- * credentials and MFA secret fields.
9154
- */
9155
- private toSafePlatformUser;
9156
- /**
9157
- * Constructs a `SafeAuthUser`-compatible object from a `SafeAuthPlatformUser` so
9158
- * that it can be passed to hooks typed for `SafeAuthUser`.
9159
- *
9160
- * Platform admins have no `tenantId` or `emailVerified` fields. These are filled
9161
- * with sentinel values (`''` and `true` respectively) since platform admins are
9162
- * provisioned directly and do not participate in the tenant/email-verification flow.
9163
- */
9164
- private platformUserAsSafeUser;
9165
- /**
9166
- * The account a hook is handed after an MFA transition, projected from the state the transition
9167
- * wrote.
9168
- *
9169
- * The record in hand was read before the transition, so its `mfaEnabled` still describes the
9170
- * state the transition replaced; the hooks run after the write and must be told what it
9171
- * produced. Only the flag is overridden. The secret and recovery-code fields are stripped by
9172
- * the same projections every other hook payload uses, and the repository's own record is not
9173
- * modified.
9174
- *
9175
- * @param context - The identity plane the account belongs to.
9176
- * @param user - The record read before the transition.
9177
- * @param mfaEnabled - Whether the transition left the second factor enabled.
9178
- * @returns The projection for the plane, with `mfaEnabled` as written.
9179
- */
9180
- private committedSafeUser;
9181
9069
  /**
9182
9070
  * Initiates the MFA setup flow for a dashboard user or platform administrator.
9183
9071
  *
@@ -5626,11 +5626,25 @@ declare class RolesGuard implements CanActivate {
5626
5626
  * `req.user` before this guard runs.
5627
5627
  *
5628
5628
  * @remarks
5629
- * **Multi-tenant ownership limitation:** This guard does NOT verify that the
5630
- * target resource belongs to the same `tenantId` as the JWT. In multi-tenant
5631
- * deployments the controller or service MUST additionally enforce ownership
5632
- * against `req.user.tenantId` to prevent cross-tenant IDOR. This guard only
5633
- * enforces the *identity* boundary (self vs. other), not the *tenant* boundary.
5629
+ * **Tenant boundary:** when the route captures the tenant under `tenantId`, `tenant_id` or
5630
+ * `tenant`, every capture present must equal the token's `tenantId`; otherwise the request is
5631
+ * refused with `INSUFFICIENT_ROLE` before the self-or-admin decision is made. An id is only
5632
+ * unique within a tenant, and the `admin` role is held inside one, so neither identity nor role
5633
+ * reaches across the boundary. Captures that disagree with one another, are empty, or are arrays
5634
+ * name no tenant the token can equal and are refused; a wildcard capture arrives as an array of
5635
+ * the segments it matched, so it is refused even for an admin.
5636
+ *
5637
+ * **What the tenant check cannot see:** only those three capture names are read. A route that
5638
+ * names the tenant any other way, `{org}` for instance, is decided by identity and role alone, and
5639
+ * the handler must bind that capture to the token's tenant itself, as must a route with no tenant
5640
+ * capture. Where Express resolves a capture name used at two nesting levels, the inner value wins
5641
+ * (measured on Express 5, with or without `mergeParams`), and a capture on a parent router
5642
+ * mounted without `mergeParams` is not visible to the inner route, so the guard sees none.
5643
+ * Nest controllers compose the full path on one route and are not affected by the second.
5644
+ *
5645
+ * **The id:** the `userId` or `id` capture must be one non-empty path segment. An empty value or
5646
+ * one carrying a slash (a decoded `%2F`) is refused with `INSUFFICIENT_ROLE`, for an admin too and
5647
+ * for an owner whose own id contains a slash.
5634
5648
  *
5635
5649
  * **Admin role convention:** The literal `'admin'` role is checked against the
5636
5650
  * configured hierarchy. If the consuming application uses a different convention
@@ -9025,97 +9039,6 @@ declare class MfaService {
9025
9039
  * @throws {@link AuthException} `MFA_STATE_CONFLICT` when another transition holds the lock.
9026
9040
  */
9027
9041
  private transitionMfaRecord;
9028
- /**
9029
- * Whether `stored` decrypts, under the current or a retired key, to `secretBase32`.
9030
- *
9031
- * Compares plaintext, so a record that was only re-encrypted still matches and one holding a
9032
- * different secret does not. A ciphertext no key opens does not match either.
9033
- */
9034
- private holdsSecret;
9035
- /**
9036
- * Encrypts a TOTP secret for storage in the database using AES-256-GCM.
9037
- */
9038
- private encryptSecret;
9039
- /**
9040
- * Decrypts a stored TOTP secret, falling back to keys retired by a rotation.
9041
- *
9042
- * The ciphertext records no key identifier, so without the retired keys, changing
9043
- * `mfa.encryptionKey` makes every stored secret undecryptable — every enrolled user's
9044
- * authenticator stops matching at once, with no way back. AES-GCM authenticates, so a wrong
9045
- * key fails unambiguously rather than returning garbage; trying them in order is safe.
9046
- *
9047
- * Re-throws any decryption failure as an opaque `TOKEN_INVALID` to prevent
9048
- * error-type oracle attacks (callers cannot distinguish format vs. tamper errors).
9049
- */
9050
- private decryptSecret;
9051
- /**
9052
- * Decrypt under the current key, then under each retired one.
9053
- *
9054
- * @param encrypted - The stored ciphertext.
9055
- * @returns The plaintext and whether a retired key produced it — the signal that the record
9056
- * should be re-encrypted under the current key.
9057
- * @throws {@link AuthException} `TOKEN_INVALID` when no key decrypts it.
9058
- */
9059
- private decryptWithRotation;
9060
- /**
9061
- * Safely parses a Redis-stored {@link MfaSetupData} JSON payload.
9062
- *
9063
- * A corrupted or tampered Redis value would otherwise raise an unhandled
9064
- * `SyntaxError` from the service boundary (uncaught 500). We translate the
9065
- * failure into an opaque `MFA_SETUP_REQUIRED`, which mirrors the response
9066
- * the caller would receive if the key were absent — preventing an attacker
9067
- * with Redis write access from distinguishing "no setup pending" from
9068
- * "setup payload corrupted".
9069
- */
9070
- private parseSetupData;
9071
- /**
9072
- * Safely parses the AES-decrypted JSON array of plain-text recovery codes.
9073
- *
9074
- * A decrypted value that fails JSON parsing indicates either a downgrade of
9075
- * the stored payload (highly unlikely, since AES-GCM authenticates the
9076
- * ciphertext) or an internal bug. Both cases are surfaced opaquely as
9077
- * `MFA_SETUP_REQUIRED` so callers do not learn structural details of the
9078
- * encrypted payload.
9079
- */
9080
- private parsePlainRecoveryCodes;
9081
- /**
9082
- * Generates `count` recovery codes in `XXXX-XXXX-XXXX-XXXX-XXXX-XXXX` format
9083
- * (96 bits of entropy per code, hex-encoded).
9084
- *
9085
- * Returns both the plain-text (shown once to the user) and scrypt-hashed
9086
- * versions (stored in the database).
9087
- *
9088
- * @remarks
9089
- * Each code carries 96 bits of entropy (12 random bytes → 24 hex chars grouped
9090
- * as 6 × 4). This is well above the NIST SP 800-63B recommendation for
9091
- * offline-resistant credentials. The previous 12-digit decimal format offered
9092
- * ~40 bits, which relied entirely on the scrypt hash for offline resistance —
9093
- * any database leak would have put the raw codes within offline reach.
9094
- */
9095
- private hashRecoveryCodes;
9096
- /**
9097
- * Derives the stored digest for a recovery code: a keyed HMAC-SHA-256, hex encoded.
9098
- *
9099
- * A recovery code is 96 bits of CSPRNG output, not a human-chosen password, so a
9100
- * memory-hard KDF buys nothing against it — there is no dictionary to walk and brute
9101
- * forcing 2^96 is out of reach whatever the hash costs. What the KDF did buy was an
9102
- * attacker-reachable CPU amplifier: a challenge submitting a wrong recovery code scanned
9103
- * every stored digest, so one request cost as many scrypt derivations as the user had
9104
- * codes. The keyed MAC is the right primitive here — the secret key is what stops an
9105
- * offline attacker precomputing digests from a leaked table, and it costs microseconds.
9106
- */
9107
- private digestRecoveryCode;
9108
- /**
9109
- * Compares a submitted recovery code against every stored digest.
9110
- *
9111
- * One storage format only: a keyed MAC over the code. Every digest is compared in constant
9112
- * time and the scan always runs to completion — including the step that picks the winner out
9113
- * of the recorded results — so neither the position of a match nor the number of codes still
9114
- * unused is observable in the response time.
9115
- *
9116
- * @returns Index of the first matching digest, or `-1` if none match.
9117
- */
9118
- private verifyRecoveryCode;
9119
9042
  /**
9120
9043
  * Claims a matched recovery code for exactly one challenge.
9121
9044
  *
@@ -9143,41 +9066,6 @@ declare class MfaService {
9143
9066
  * @returns `true` when this challenge is the one that claimed it.
9144
9067
  */
9145
9068
  private claimRecoveryCode;
9146
- /**
9147
- * Returns a `SafeAuthUser` projection of a dashboard user, stripping credentials
9148
- * and MFA secret fields before passing to hooks or email providers.
9149
- */
9150
- private toSafeUser;
9151
- /**
9152
- * Returns a `SafeAuthPlatformUser` projection of a platform admin, stripping
9153
- * credentials and MFA secret fields.
9154
- */
9155
- private toSafePlatformUser;
9156
- /**
9157
- * Constructs a `SafeAuthUser`-compatible object from a `SafeAuthPlatformUser` so
9158
- * that it can be passed to hooks typed for `SafeAuthUser`.
9159
- *
9160
- * Platform admins have no `tenantId` or `emailVerified` fields. These are filled
9161
- * with sentinel values (`''` and `true` respectively) since platform admins are
9162
- * provisioned directly and do not participate in the tenant/email-verification flow.
9163
- */
9164
- private platformUserAsSafeUser;
9165
- /**
9166
- * The account a hook is handed after an MFA transition, projected from the state the transition
9167
- * wrote.
9168
- *
9169
- * The record in hand was read before the transition, so its `mfaEnabled` still describes the
9170
- * state the transition replaced; the hooks run after the write and must be told what it
9171
- * produced. Only the flag is overridden. The secret and recovery-code fields are stripped by
9172
- * the same projections every other hook payload uses, and the repository's own record is not
9173
- * modified.
9174
- *
9175
- * @param context - The identity plane the account belongs to.
9176
- * @param user - The record read before the transition.
9177
- * @param mfaEnabled - Whether the transition left the second factor enabled.
9178
- * @returns The projection for the plane, with `mfaEnabled` as written.
9179
- */
9180
- private committedSafeUser;
9181
9069
  /**
9182
9070
  * Initiates the MFA setup flow for a dashboard user or platform administrator.
9183
9071
  *