subaccountAccess.ts 1.2 KB

1234567891011121314151617181920212223
  1. /** Owner-scoped Bot endpoints. Keep native and administrative surfaces denied. */
  2. export function isWorkspaceAccountPath(method: string, path: string): boolean {
  3. const read = method === 'GET' || method === 'HEAD'
  4. if (read && /^\/api\/app\/workspace\/(snapshot|projects|memories|memory-export|memory-jobs|collaboration)$/.test(path)) return true
  5. if (read && /^\/api\/app\/workspace\/memories\/[^/]+\/revisions$/.test(path)) return true
  6. return method === 'POST' && /^\/api\/app\/workspace\/(projects|memories|memories\/forget|memory-jobs\/[^/]+\/retry|collaboration\/[^/]+\/stop)$/.test(path)
  7. }
  8. /** Bound memory as well as request frequency; untrusted forwarded IPs are not used. */
  9. export class RegistrationLimiter {
  10. private readonly attempts = new Map<string, { count: number; expires: number }>()
  11. take(address: string, now = Date.now()): number {
  12. for (const [key, value] of this.attempts) if (value.expires <= now) this.attempts.delete(key)
  13. let entry = this.attempts.get(address)
  14. if (!entry) {
  15. if (this.attempts.size >= 10_000) return 60
  16. entry = { count: 0, expires: now + 15 * 60_000 }
  17. this.attempts.set(address, entry)
  18. }
  19. if (++entry.count > 10) return Math.ceil((entry.expires - now) / 1000)
  20. return 0
  21. }
  22. }