@astratra/security 1.0.0 → 1.0.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astratra/security",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Generic authentication and security middleware for Astratra workspaces.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -145,10 +145,12 @@ const withOptionalStore = (limiterOptions, store) => {
145
145
  return { ...limiterOptions, store };
146
146
  };
147
147
 
148
+ const defaultApiSkip = () => (process.env.NODE_ENV === 'production' ? undefined : skipLocalhost);
149
+
148
150
  const createApiLimiter = (options = {}) => rateLimit(withOptionalStore({
149
151
  windowMs: options.windowMs || 15 * 60 * 1000,
150
152
  max: options.max || 300,
151
- skip: options.skip || skipLocalhost,
153
+ skip: options.skip ?? defaultApiSkip(),
152
154
  standardHeaders: options.standardHeaders ?? true,
153
155
  legacyHeaders: options.legacyHeaders ?? false,
154
156
  validate: options.validate || { ip: false },
package/src/webauthn.js CHANGED
@@ -93,10 +93,17 @@ const normalizeChallenge = (pending) => {
93
93
  return {
94
94
  challenge: pending.challenge,
95
95
  origin: pending.origin || pending.metadata?.origin,
96
- rpID: pending.rpID || pending.metadata?.rpID
96
+ rpID: pending.rpID || pending.metadata?.rpID,
97
+ expiresAt: pending.expiresAt || pending.metadata?.expiresAt
97
98
  };
98
99
  };
99
100
 
101
+ const isExpiredChallenge = (pending) => {
102
+ if (!pending?.expiresAt) return false;
103
+ const expiresAt = new Date(pending.expiresAt).getTime();
104
+ return Number.isFinite(expiresAt) && expiresAt <= Date.now();
105
+ };
106
+
100
107
  const publicKeyToBase64Url = (publicKey) => {
101
108
  if (Buffer.isBuffer(publicKey)) return publicKey.toString('base64url');
102
109
  if (publicKey instanceof Uint8Array) return Buffer.from(publicKey).toString('base64url');
@@ -164,6 +171,9 @@ const createWebauthnService = (store, options = {}) => {
164
171
  if (!pending) {
165
172
  throw createError('No pending WebAuthn registration challenge.', 400);
166
173
  }
174
+ if (isExpiredChallenge(pending)) {
175
+ throw createError('Expired WebAuthn registration challenge.', 400);
176
+ }
167
177
 
168
178
  const verification = await verifyRegistrationResponse({
169
179
  response,
@@ -222,11 +232,17 @@ const createWebauthnService = (store, options = {}) => {
222
232
  if (!pending) {
223
233
  throw createError('No pending WebAuthn authentication challenge.', 400);
224
234
  }
235
+ if (isExpiredChallenge(pending)) {
236
+ throw createError('Expired WebAuthn authentication challenge.', 400);
237
+ }
225
238
 
226
239
  const stored = await store.getCredentialById(response.id);
227
240
  if (!stored) {
228
241
  throw createError('Unknown WebAuthn credential.', 401);
229
242
  }
243
+ if (String(stored.userId) !== String(userId)) {
244
+ throw createError('WebAuthn credential does not belong to this user.', 401);
245
+ }
230
246
 
231
247
  const credentialID = stored.credentialID || stored.id;
232
248
  const verification = await verifyAuthenticationResponse({