hatch3r 1.0.0
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/LICENSE +21 -0
- package/README.md +437 -0
- package/agents/hatch3r-a11y-auditor.md +126 -0
- package/agents/hatch3r-architect.md +160 -0
- package/agents/hatch3r-ci-watcher.md +123 -0
- package/agents/hatch3r-context-rules.md +97 -0
- package/agents/hatch3r-dependency-auditor.md +164 -0
- package/agents/hatch3r-devops.md +138 -0
- package/agents/hatch3r-docs-writer.md +97 -0
- package/agents/hatch3r-implementer.md +162 -0
- package/agents/hatch3r-learnings-loader.md +108 -0
- package/agents/hatch3r-lint-fixer.md +104 -0
- package/agents/hatch3r-perf-profiler.md +123 -0
- package/agents/hatch3r-researcher.md +642 -0
- package/agents/hatch3r-reviewer.md +81 -0
- package/agents/hatch3r-security-auditor.md +119 -0
- package/agents/hatch3r-test-writer.md +134 -0
- package/commands/hatch3r-agent-customize.md +146 -0
- package/commands/hatch3r-api-spec.md +49 -0
- package/commands/hatch3r-benchmark.md +50 -0
- package/commands/hatch3r-board-fill.md +504 -0
- package/commands/hatch3r-board-init.md +315 -0
- package/commands/hatch3r-board-pickup.md +672 -0
- package/commands/hatch3r-board-refresh.md +198 -0
- package/commands/hatch3r-board-shared.md +369 -0
- package/commands/hatch3r-bug-plan.md +410 -0
- package/commands/hatch3r-codebase-map.md +1182 -0
- package/commands/hatch3r-command-customize.md +94 -0
- package/commands/hatch3r-context-health.md +112 -0
- package/commands/hatch3r-cost-tracking.md +139 -0
- package/commands/hatch3r-dep-audit.md +171 -0
- package/commands/hatch3r-feature-plan.md +379 -0
- package/commands/hatch3r-healthcheck.md +307 -0
- package/commands/hatch3r-hooks.md +282 -0
- package/commands/hatch3r-learn.md +217 -0
- package/commands/hatch3r-migration-plan.md +51 -0
- package/commands/hatch3r-onboard.md +56 -0
- package/commands/hatch3r-project-spec.md +1153 -0
- package/commands/hatch3r-recipe.md +179 -0
- package/commands/hatch3r-refactor-plan.md +426 -0
- package/commands/hatch3r-release.md +328 -0
- package/commands/hatch3r-roadmap.md +556 -0
- package/commands/hatch3r-rule-customize.md +114 -0
- package/commands/hatch3r-security-audit.md +370 -0
- package/commands/hatch3r-skill-customize.md +93 -0
- package/commands/hatch3r-workflow.md +377 -0
- package/dist/cli/hooks-ZOTFDEA3.js +59 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +3584 -0
- package/github-agents/hatch3r-docs-agent.md +46 -0
- package/github-agents/hatch3r-lint-agent.md +41 -0
- package/github-agents/hatch3r-security-agent.md +54 -0
- package/github-agents/hatch3r-test-agent.md +66 -0
- package/hooks/hatch3r-ci-failure.md +10 -0
- package/hooks/hatch3r-file-save.md +11 -0
- package/hooks/hatch3r-post-merge.md +10 -0
- package/hooks/hatch3r-pre-commit.md +11 -0
- package/hooks/hatch3r-pre-push.md +10 -0
- package/hooks/hatch3r-session-start.md +10 -0
- package/mcp/mcp.json +62 -0
- package/package.json +84 -0
- package/prompts/hatch3r-bug-triage.md +155 -0
- package/prompts/hatch3r-code-review.md +131 -0
- package/prompts/hatch3r-pr-description.md +173 -0
- package/rules/hatch3r-accessibility-standards.md +77 -0
- package/rules/hatch3r-accessibility-standards.mdc +75 -0
- package/rules/hatch3r-agent-orchestration.md +160 -0
- package/rules/hatch3r-api-design.md +176 -0
- package/rules/hatch3r-api-design.mdc +176 -0
- package/rules/hatch3r-browser-verification.md +73 -0
- package/rules/hatch3r-browser-verification.mdc +73 -0
- package/rules/hatch3r-ci-cd.md +70 -0
- package/rules/hatch3r-ci-cd.mdc +68 -0
- package/rules/hatch3r-code-standards.md +102 -0
- package/rules/hatch3r-code-standards.mdc +100 -0
- package/rules/hatch3r-component-conventions.md +102 -0
- package/rules/hatch3r-component-conventions.mdc +102 -0
- package/rules/hatch3r-data-classification.md +85 -0
- package/rules/hatch3r-data-classification.mdc +83 -0
- package/rules/hatch3r-dependency-management.md +17 -0
- package/rules/hatch3r-dependency-management.mdc +15 -0
- package/rules/hatch3r-error-handling.md +17 -0
- package/rules/hatch3r-error-handling.mdc +15 -0
- package/rules/hatch3r-feature-flags.md +112 -0
- package/rules/hatch3r-feature-flags.mdc +112 -0
- package/rules/hatch3r-git-conventions.md +47 -0
- package/rules/hatch3r-git-conventions.mdc +45 -0
- package/rules/hatch3r-i18n.md +90 -0
- package/rules/hatch3r-i18n.mdc +90 -0
- package/rules/hatch3r-learning-consult.md +29 -0
- package/rules/hatch3r-learning-consult.mdc +27 -0
- package/rules/hatch3r-migrations.md +17 -0
- package/rules/hatch3r-migrations.mdc +15 -0
- package/rules/hatch3r-observability.md +165 -0
- package/rules/hatch3r-observability.mdc +165 -0
- package/rules/hatch3r-performance-budgets.md +109 -0
- package/rules/hatch3r-performance-budgets.mdc +109 -0
- package/rules/hatch3r-secrets-management.md +76 -0
- package/rules/hatch3r-secrets-management.mdc +74 -0
- package/rules/hatch3r-security-patterns.md +211 -0
- package/rules/hatch3r-security-patterns.mdc +211 -0
- package/rules/hatch3r-testing.md +89 -0
- package/rules/hatch3r-testing.mdc +87 -0
- package/rules/hatch3r-theming.md +51 -0
- package/rules/hatch3r-theming.mdc +51 -0
- package/rules/hatch3r-tooling-hierarchy.md +92 -0
- package/rules/hatch3r-tooling-hierarchy.mdc +79 -0
- package/skills/hatch3r-a11y-audit/SKILL.md +131 -0
- package/skills/hatch3r-agent-customize/SKILL.md +75 -0
- package/skills/hatch3r-api-spec/SKILL.md +66 -0
- package/skills/hatch3r-architecture-review/SKILL.md +96 -0
- package/skills/hatch3r-bug-fix/SKILL.md +129 -0
- package/skills/hatch3r-ci-pipeline/SKILL.md +76 -0
- package/skills/hatch3r-command-customize/SKILL.md +67 -0
- package/skills/hatch3r-context-health/SKILL.md +76 -0
- package/skills/hatch3r-cost-tracking/SKILL.md +65 -0
- package/skills/hatch3r-dep-audit/SKILL.md +82 -0
- package/skills/hatch3r-feature/SKILL.md +129 -0
- package/skills/hatch3r-gh-agentic-workflows/SKILL.md +150 -0
- package/skills/hatch3r-incident-response/SKILL.md +86 -0
- package/skills/hatch3r-issue-workflow/SKILL.md +139 -0
- package/skills/hatch3r-logical-refactor/SKILL.md +73 -0
- package/skills/hatch3r-migration/SKILL.md +76 -0
- package/skills/hatch3r-perf-audit/SKILL.md +114 -0
- package/skills/hatch3r-pr-creation/SKILL.md +85 -0
- package/skills/hatch3r-qa-validation/SKILL.md +86 -0
- package/skills/hatch3r-recipe/SKILL.md +67 -0
- package/skills/hatch3r-refactor/SKILL.md +86 -0
- package/skills/hatch3r-release/SKILL.md +93 -0
- package/skills/hatch3r-rule-customize/SKILL.md +70 -0
- package/skills/hatch3r-skill-customize/SKILL.md +67 -0
- package/skills/hatch3r-visual-refactor/SKILL.md +89 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Secret management, rotation, and secure handling patterns for the project
|
|
3
|
+
alwaysApply: true
|
|
4
|
+
---
|
|
5
|
+
# Secrets Management
|
|
6
|
+
|
|
7
|
+
## Env Var Management
|
|
8
|
+
|
|
9
|
+
- Store configuration and secrets in environment variables. Never hard-code secrets (API keys, tokens, passwords, connection strings) in source code, comments, or commit messages.
|
|
10
|
+
- Maintain a `.env.example` file in the repository root listing every required environment variable with placeholder values and brief descriptions. Update it whenever a new env var is introduced.
|
|
11
|
+
- Actual `.env` files must be in `.gitignore`. Verify `.gitignore` includes `.env`, `.env.local`, `.env.*.local`, and any other secret-bearing files before every commit.
|
|
12
|
+
- Use a typed env loader (e.g., `@t3-oss/env-core`, `envalid`, `zod` schema) to validate and parse environment variables at application startup. Fail fast with a clear error message listing missing or invalid variables.
|
|
13
|
+
- Separate secrets by environment: `.env.development`, `.env.staging`, `.env.production`. Never share production secrets with development environments.
|
|
14
|
+
- Document the source of each secret (which service/provider generates it) in `.env.example` or an internal secrets inventory document.
|
|
15
|
+
|
|
16
|
+
## Secret Rotation Policies
|
|
17
|
+
|
|
18
|
+
| Secret Type | Rotation Frequency | Trigger for Immediate Rotation |
|
|
19
|
+
|-------------|-------------------|-------------------------------|
|
|
20
|
+
| API keys (third-party) | Every 90 days | Suspected compromise, employee departure |
|
|
21
|
+
| Database credentials | Every 90 days | Credential exposure, personnel change |
|
|
22
|
+
| JWT signing keys | Every 180 days | Algorithm upgrade, key compromise |
|
|
23
|
+
| Webhook secrets | Every 180 days | Partner-side breach, integration change |
|
|
24
|
+
| Service account tokens | Every 90 days | Scope change, compromise |
|
|
25
|
+
| Encryption keys | Every 365 days (or per compliance) | Algorithm vulnerability, compliance audit |
|
|
26
|
+
| OAuth client secrets | Every 180 days | Provider breach, app re-registration |
|
|
27
|
+
|
|
28
|
+
- Implement dual-key (overlap) rotation: deploy the new secret, update all consumers, verify, then revoke the old secret. Never revoke before all consumers have migrated.
|
|
29
|
+
- Automate rotation where the provider supports it (AWS Secrets Manager auto-rotation, GCP Secret Manager rotation schedules).
|
|
30
|
+
- Log every rotation event: who initiated, when, which secret (by name, never by value), success/failure.
|
|
31
|
+
|
|
32
|
+
## Cloud Secret Managers
|
|
33
|
+
|
|
34
|
+
- **AWS Secrets Manager:** Store secrets as JSON key-value pairs. Use IAM policies to scope access per service/role. Enable automatic rotation with Lambda rotation functions. Use `aws-sdk` `GetSecretValue` at runtime — never bake secrets into container images or deployment artifacts.
|
|
35
|
+
- **GCP Secret Manager:** Store each secret as a versioned resource. Grant `secretmanager.secretAccessor` role per service account with resource-level IAM. Access secrets via `@google-cloud/secret-manager` client library at startup. Use secret versions (not `latest` in production) for auditability.
|
|
36
|
+
- **Azure Key Vault:** Store secrets, keys, and certificates. Use Managed Identity for authentication — no credentials needed to access the vault. Apply access policies per application identity. Enable soft-delete and purge protection.
|
|
37
|
+
- **HashiCorp Vault:** Use dynamic secrets where possible (database credentials, cloud IAM). Implement AppRole or Kubernetes auth for machine identity. Set TTLs on all leases. Audit log every access.
|
|
38
|
+
- **General:** Abstract the secret provider behind an interface so the application code is not coupled to a specific cloud provider. Inject the provider at startup via configuration.
|
|
39
|
+
|
|
40
|
+
## CI/CD Secret Injection
|
|
41
|
+
|
|
42
|
+
- **GitHub Actions:** Store secrets in repository or organization settings. Reference via `${{ secrets.SECRET_NAME }}`. Never echo secrets in workflow logs — use masking (`::add-mask::`). Use environment-scoped secrets for production deployments with required reviewers.
|
|
43
|
+
- **GitLab CI:** Store in project or group CI/CD variables. Mark as "Protected" (only available on protected branches) and "Masked" (redacted from logs). Use file-type variables for multi-line secrets (certificates, keys).
|
|
44
|
+
- **General CI principles:** Secrets must not appear in build logs, artifacts, or cached layers. Pin CI action versions by SHA (not tag) to prevent supply chain attacks on secret-accessing workflows. Rotate CI secrets on the same schedule as application secrets.
|
|
45
|
+
- **Ephemeral secrets:** For CI jobs that need temporary cloud access, use OIDC federation (e.g., GitHub Actions `aws-actions/configure-aws-credentials` with OIDC) instead of long-lived credentials.
|
|
46
|
+
|
|
47
|
+
## Application-Level Secret Handling
|
|
48
|
+
|
|
49
|
+
- **Never log secrets.** Sanitize log output to redact any field matching known secret patterns (tokens, keys, passwords). Use structured logging with an explicit allowlist of loggable fields.
|
|
50
|
+
- **Never serialize secrets** into JSON responses, error messages, stack traces, or client-side state. Treat secrets as write-only values: they go into the system but never come back out in any observable output.
|
|
51
|
+
- **Memory safety:** Clear secret values from memory after use where the language/runtime permits (overwrite buffers, null references). Avoid storing secrets in global/static variables that persist for the application lifetime.
|
|
52
|
+
- **Transport:** Transmit secrets only over TLS 1.2+. Never send secrets in URL query parameters (they appear in access logs and browser history). Use request headers or POST body.
|
|
53
|
+
- **Principle of least privilege:** Each service/function should have access only to the secrets it needs. Avoid a single "master" secret store accessible to all services.
|
|
54
|
+
- **Secret-bearing config objects:** Wrap secrets in a `Secret<T>` type that overrides `toString()` and `toJSON()` to return `"[REDACTED]"`. This prevents accidental exposure via logging or serialization.
|
|
55
|
+
|
|
56
|
+
## Secret Scanning in CI
|
|
57
|
+
|
|
58
|
+
- **gitleaks:** Run `gitleaks detect` in CI on every push and PR. Configure `.gitleaks.toml` with project-specific rules and allowlists for false positives (test fixtures, documentation examples).
|
|
59
|
+
- **TruffleHog:** Use `trufflehog git` for historical scanning of the full repository. Run quarterly or on suspected compromise. Focus on high-entropy strings and known secret patterns.
|
|
60
|
+
- **GitHub Secret Scanning:** Enable GitHub's built-in secret scanning and push protection. Configure custom patterns for project-specific secret formats.
|
|
61
|
+
- **Pre-commit hooks:** Install a local pre-commit hook (e.g., `gitleaks protect --staged`) to catch secrets before they reach the remote. This is defense-in-depth — CI scanning is still required.
|
|
62
|
+
- **Remediation SLA:** Secrets detected in CI must be rotated immediately (within 1 hour for production secrets). Assume any secret that reached a commit is compromised, even if the commit was force-pushed away — git history is recoverable.
|
|
63
|
+
|
|
64
|
+
## Emergency Rotation Procedures
|
|
65
|
+
|
|
66
|
+
When a secret is confirmed or suspected compromised:
|
|
67
|
+
|
|
68
|
+
1. **Assess scope:** Identify which secret, which environments, and which services are affected. Check audit logs for unauthorized access.
|
|
69
|
+
2. **Generate new secret:** Create a replacement via the appropriate provider (cloud console, API, or CLI). Do not reuse or derive from the compromised value.
|
|
70
|
+
3. **Deploy new secret:** Update the secret in the secret manager and deploy affected services. Use blue-green or rolling deployment to avoid downtime.
|
|
71
|
+
4. **Revoke old secret:** After confirming all services are using the new secret, revoke/delete the old one. Verify revocation by testing that the old value is rejected.
|
|
72
|
+
5. **Audit impact:** Review access logs for the compromised secret's lifetime. Identify any unauthorized actions taken with the compromised credential.
|
|
73
|
+
6. **Incident report:** Document the timeline, root cause (how the secret was exposed), blast radius, remediation steps, and preventive measures. File as a security incident per the project's incident response process.
|
|
74
|
+
7. **Prevent recurrence:** Add scanning rules for the pattern that was missed, review access controls, and update rotation policies if the secret exceeded its rotation window.
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Security patterns including input validation, auth enforcement, and AI/agentic security for the project
|
|
3
|
+
alwaysApply: true
|
|
4
|
+
---
|
|
5
|
+
# Security Patterns
|
|
6
|
+
|
|
7
|
+
## Input Validation
|
|
8
|
+
|
|
9
|
+
- Validate at the boundary: API routes, form handlers, webhook receivers, CLI parsers. Never trust data that has crossed a trust boundary.
|
|
10
|
+
- Use type-safe runtime schemas (zod, valibot, joi) co-located with the handler. Compile-time types alone are insufficient.
|
|
11
|
+
- Allowlist over denylist for string inputs (permitted characters, values, formats). Denylists are always incomplete.
|
|
12
|
+
- Enforce length limits, numeric range checks, and format validation (email, URL, UUID, ISO dates) on every external field.
|
|
13
|
+
- Reject unexpected fields with strict/passthrough-off schemas. Unknown keys are an attack surface.
|
|
14
|
+
- File uploads: validate type by magic bytes (not extension), enforce size limits, generate server-side filenames, reject path traversal (`..`, absolute paths, null bytes).
|
|
15
|
+
|
|
16
|
+
## Output Encoding
|
|
17
|
+
|
|
18
|
+
- Apply context-aware encoding: HTML entities for markup, URL-encoding for query params, JavaScript escaping for inline scripts, CSS escaping for style contexts.
|
|
19
|
+
- Never construct HTML from user input without sanitization (DOMPurify or equivalent server-side library). Treat all user content as untrusted.
|
|
20
|
+
- Use parameterized queries / prepared statements for SQL, Firestore filters, and NoSQL queries. Zero tolerance for string concatenation with user input.
|
|
21
|
+
- Enable auto-escaping in template engines by default. Disable only per-expression with review.
|
|
22
|
+
- Sanitize data before logging. Log output is also an injection vector (log forging, ANSI escape injection).
|
|
23
|
+
|
|
24
|
+
## Authentication Enforcement
|
|
25
|
+
|
|
26
|
+
- Auth middleware on every route by default. Public routes require explicit opt-out with code review justification.
|
|
27
|
+
- Token validation: pin allowed algorithms (reject `none`), enforce expiry (`exp`), verify audience (`aud`) and issuer (`iss`) claims. Reject tokens failing any check.
|
|
28
|
+
- Session security: `HttpOnly`, `Secure`, `SameSite=Strict` (or `Lax` with justification) cookies. Rotate session ID on privilege change (login, role switch).
|
|
29
|
+
- Multi-factor authentication for sensitive operations: admin actions, payment, account deletion, API key generation.
|
|
30
|
+
- Rate-limit authentication endpoints (login, token refresh, password reset). Lock accounts or add progressive delays after repeated failures.
|
|
31
|
+
- Invalidate all sessions on password change. Provide "sign out everywhere" capability.
|
|
32
|
+
|
|
33
|
+
## Fail-Closed Defaults
|
|
34
|
+
|
|
35
|
+
- Default deny for authorization. Every permission must be explicitly granted; absence of a rule means deny.
|
|
36
|
+
- Error handlers must not leak internal state: no stack traces, query details, file paths, or dependency versions in responses. Return generic error codes.
|
|
37
|
+
- Fallback to the most restrictive behavior on config parse failure. Misconfiguration must never widen access.
|
|
38
|
+
- Circuit breakers for downstream service failures. Degrade gracefully rather than retrying indefinitely or passing errors upstream.
|
|
39
|
+
- Health checks and readiness probes must not expose sensitive configuration or internal topology.
|
|
40
|
+
- Disable debug endpoints, verbose logging, and source maps in production builds. Gate behind feature flags if needed in staging.
|
|
41
|
+
|
|
42
|
+
## CSRF Protection
|
|
43
|
+
|
|
44
|
+
- Apply synchronizer token pattern or double-submit cookie for all state-mutating requests (POST, PUT, PATCH, DELETE).
|
|
45
|
+
- Set `SameSite` cookie attribute as defense-in-depth. It supplements but does not replace CSRF tokens.
|
|
46
|
+
- For API-only endpoints (no browser cookies), require a custom header (`X-Requested-With` or equivalent) that browsers will not send cross-origin without CORS preflight.
|
|
47
|
+
- Validate `Origin` and `Referer` headers as an additional layer for critical endpoints.
|
|
48
|
+
|
|
49
|
+
## AI & Agentic Security (OWASP Agentic Top 10)
|
|
50
|
+
|
|
51
|
+
### ASI01 — Agent Goal Hijack
|
|
52
|
+
|
|
53
|
+
- Separate system prompts from user input with clear delimiters. Never allow user content to override system instructions.
|
|
54
|
+
- Implement input guardrails: scan user messages for injection patterns before LLM processing.
|
|
55
|
+
- Enforce instruction hierarchy: system > developer > user. Reject attempts to redefine agent purpose.
|
|
56
|
+
- Defend against indirect prompt injection: sanitize and tag content retrieved from external sources (RAG, web, files) before including in context.
|
|
57
|
+
|
|
58
|
+
### ASI02 — Tool Misuse & Exploitation
|
|
59
|
+
|
|
60
|
+
- Deny-by-default tool access. Each tool requires explicit grant per agent role.
|
|
61
|
+
- Enforce parameter schemas on every tool call. Reject calls with unexpected, missing, or out-of-range arguments.
|
|
62
|
+
- Rate-limit tool invocations per agent per time window. Alert on anomalous tool usage patterns.
|
|
63
|
+
- Sandbox tool execution: restrict file system access, network egress, and subprocess spawning.
|
|
64
|
+
|
|
65
|
+
### ASI03 — Identity & Privilege Abuse
|
|
66
|
+
|
|
67
|
+
- Assign unique agent IDs per invocation. Log all actions with agent identity for non-repudiation.
|
|
68
|
+
- Apply least privilege: agents receive scoped credentials, never full user or admin tokens.
|
|
69
|
+
- Prevent privilege escalation across agent boundaries. An agent must not request or inherit higher privileges than its caller.
|
|
70
|
+
- Audit delegation chains: every permission grant from user → agent → sub-agent must be traceable.
|
|
71
|
+
|
|
72
|
+
### ASI04 — Supply Chain Vulnerabilities
|
|
73
|
+
|
|
74
|
+
- Pin MCP server and plugin versions. Never auto-install unverified packages (`npx -y` on untrusted sources).
|
|
75
|
+
- Verify package integrity (checksums, signatures) before loading tools or plugins.
|
|
76
|
+
- Audit third-party prompt templates for injected instructions before use.
|
|
77
|
+
- Maintain an allowlist of approved MCP servers and tool sources.
|
|
78
|
+
|
|
79
|
+
### ASI05 — Unexpected Code Execution
|
|
80
|
+
|
|
81
|
+
- Never execute agent-generated code without sandboxing (isolated container, restricted runtime, no network).
|
|
82
|
+
- Require human review for generated code that touches file system, network, or credentials.
|
|
83
|
+
- Restrict generated code to a safe subset: no `eval`, `exec`, shell commands, or dynamic imports.
|
|
84
|
+
- Enforce file system access controls: agents can only read/write within designated workspace directories.
|
|
85
|
+
|
|
86
|
+
### ASI06 — Memory & Context Poisoning
|
|
87
|
+
|
|
88
|
+
- Validate stored context before reuse. Re-check integrity and relevance of cached agent state on retrieval.
|
|
89
|
+
- Set expiry / TTL for all cached agent memory. Stale context is a poisoning vector.
|
|
90
|
+
- Tag and isolate RAG-retrieved content from trusted system instructions. Never promote retrieved content to system-level authority.
|
|
91
|
+
- Detect tampering: hash or sign stored memory entries, verify on read.
|
|
92
|
+
|
|
93
|
+
### ASI07 — Insecure Inter-Agent Communication
|
|
94
|
+
|
|
95
|
+
- Authenticate agent-to-agent messages. Each agent must verify the identity of its communication partner.
|
|
96
|
+
- Scope delegation tokens: a sub-agent receives only the permissions needed for its specific task.
|
|
97
|
+
- Validate message integrity (signing or HMAC) to prevent tampering in multi-agent workflows.
|
|
98
|
+
- Enforce privilege boundaries: a delegated agent cannot escalate beyond the scope granted by its parent.
|
|
99
|
+
|
|
100
|
+
### ASI08 — Cascading Failures
|
|
101
|
+
|
|
102
|
+
- Implement circuit breakers between agent stages. A failure in one agent must not propagate unchecked.
|
|
103
|
+
- Enforce timeouts on every agent invocation and tool call. No unbounded waits.
|
|
104
|
+
- Contain blast radius: isolate agent workflows so a compromised agent cannot affect unrelated workflows.
|
|
105
|
+
- Log and alert on error chains. Three consecutive failures in an agent chain should trigger automatic halt.
|
|
106
|
+
|
|
107
|
+
### ASI09 — Human-Agent Trust Exploitation
|
|
108
|
+
|
|
109
|
+
- Mandatory human confirmation for destructive operations: file deletion, database writes, external API calls with side effects, financial transactions.
|
|
110
|
+
- Enforce cost limits: cap token usage, API call counts, and compute time per agent invocation.
|
|
111
|
+
- Present agent actions transparently: show the user what the agent did and why, not just the result.
|
|
112
|
+
- Resist social engineering: agents must not bypass confirmation flows based on urgency framing in user input.
|
|
113
|
+
|
|
114
|
+
### ASI10 — Rogue Agents
|
|
115
|
+
|
|
116
|
+
- Monitor agent outputs for policy violations, off-topic responses, and anomalous behavior patterns.
|
|
117
|
+
- Validate agent outputs against expected schemas and content policies before acting on them.
|
|
118
|
+
- Enforce scope: reject agent actions outside the declared task boundary.
|
|
119
|
+
- Implement kill switches: ability to immediately terminate a running agent and revoke its credentials.
|
|
120
|
+
- Run anomaly detection on tool call patterns, output length, and execution time to flag compromised agents.
|
|
121
|
+
|
|
122
|
+
## OWASP Top 10 2025 (Web Application Security)
|
|
123
|
+
|
|
124
|
+
### A01 — Broken Access Control
|
|
125
|
+
|
|
126
|
+
- Enforce access control server-side. Client-side checks are UX, not security.
|
|
127
|
+
- Deny by default: every resource requires explicit permission. Absence of a grant means deny.
|
|
128
|
+
- Implement resource-level ownership checks: verify the authenticated user owns (or has a role granting access to) the requested resource. Parameterized IDs in URLs are not authorization — always validate ownership.
|
|
129
|
+
- Disable directory listing. Restrict access to metadata files (`.git`, `.env`, backup files).
|
|
130
|
+
- Rate-limit API access to minimize automated IDOR scanning and credential stuffing.
|
|
131
|
+
- Log access control failures and alert on repeated violations from the same identity.
|
|
132
|
+
|
|
133
|
+
### A02 — Cryptographic Failures
|
|
134
|
+
|
|
135
|
+
- Classify data by sensitivity (PII, financial, health, credentials). Apply encryption requirements per classification.
|
|
136
|
+
- Encrypt data in transit (TLS 1.2+ mandatory, prefer 1.3) and at rest (AES-256 or equivalent).
|
|
137
|
+
- Never use deprecated algorithms: MD5, SHA-1, DES, RC4, ECB mode. Use SHA-256+ for hashing, AES-GCM for symmetric encryption, RSA-OAEP or ECDSA for asymmetric.
|
|
138
|
+
- Hash passwords with bcrypt, scrypt, or Argon2id with appropriate work factors. Never use raw SHA/MD5 for passwords.
|
|
139
|
+
- Generate cryptographic keys with secure random sources (`crypto.randomBytes`, not `Math.random`). Never hard-code keys or IVs.
|
|
140
|
+
- Disable caching for responses containing sensitive data (`Cache-Control: no-store`).
|
|
141
|
+
|
|
142
|
+
### A03 — Injection
|
|
143
|
+
|
|
144
|
+
- Use parameterized queries or prepared statements for all database operations. Zero tolerance for string concatenation with user input in queries.
|
|
145
|
+
- Apply context-aware output encoding: HTML entities, URL encoding, JavaScript escaping, CSS escaping, LDAP escaping — matched to the output context.
|
|
146
|
+
- Validate and sanitize all external input with allowlist validation. Limit input length, character sets, and format.
|
|
147
|
+
- Use `LIMIT` and pagination in queries to prevent mass data disclosure via injection.
|
|
148
|
+
- For OS command execution: avoid entirely if possible. If necessary, use parameterized APIs (not shell interpolation) with strict input validation.
|
|
149
|
+
|
|
150
|
+
### A04 — Insecure Design
|
|
151
|
+
|
|
152
|
+
- Use threat modeling during design phase (STRIDE, attack trees, or equivalent). Identify trust boundaries and abuse cases before writing code.
|
|
153
|
+
- Establish and enforce secure design patterns: separation of concerns, defense in depth, least privilege, fail-closed.
|
|
154
|
+
- Write abuse-case user stories alongside feature user stories: "As an attacker, I want to..."
|
|
155
|
+
- Design rate limiting, resource quotas, and cost controls into the architecture — not as afterthoughts.
|
|
156
|
+
- Establish secure development lifecycle (SDL) practices: security requirements, design review, code review, testing.
|
|
157
|
+
|
|
158
|
+
### A05 — Security Misconfiguration
|
|
159
|
+
|
|
160
|
+
- Harden all environments: remove default accounts, disable unused features/ports/services, remove sample applications.
|
|
161
|
+
- Use identical security configuration across development, staging, and production. Differences in security settings between environments mask vulnerabilities.
|
|
162
|
+
- Automate configuration verification: infrastructure-as-code with security baselines, configuration scanning in CI.
|
|
163
|
+
- Send security headers on every response (HSTS, CSP, X-Content-Type-Options, X-Frame-Options). Centralize in middleware.
|
|
164
|
+
- Review cloud permissions quarterly. Remove unused IAM roles, security groups, and service accounts.
|
|
165
|
+
- Disable detailed error messages in production. Use generic error responses with correlation IDs for debugging.
|
|
166
|
+
|
|
167
|
+
### A06 — Vulnerable and Outdated Components
|
|
168
|
+
|
|
169
|
+
- Maintain a software bill of materials (SBOM) for all direct and transitive dependencies.
|
|
170
|
+
- Run `npm audit` (or equivalent) in CI on every build. Block merges with critical or high vulnerabilities.
|
|
171
|
+
- Subscribe to security advisories for all critical dependencies (GitHub Dependabot, Snyk, or equivalent).
|
|
172
|
+
- Remove unused dependencies. Unused code with known vulnerabilities is still a risk.
|
|
173
|
+
- Pin dependency versions in lockfiles. Review lockfile changes in PRs with the same scrutiny as code changes.
|
|
174
|
+
- Establish SLAs for vulnerability remediation: critical within 24 hours, high within 1 week, moderate within 1 sprint.
|
|
175
|
+
|
|
176
|
+
### A07 — Identification and Authentication Failures
|
|
177
|
+
|
|
178
|
+
- Implement multi-factor authentication for privileged accounts and sensitive operations.
|
|
179
|
+
- Enforce password complexity requirements: minimum 8 characters, check against breached password databases (Have I Been Pwned API).
|
|
180
|
+
- Protect against credential stuffing: rate-limit login attempts, implement progressive delays, use CAPTCHA after repeated failures.
|
|
181
|
+
- Session management: generate new session ID on login, invalidate on logout, set absolute and idle timeouts.
|
|
182
|
+
- Never expose session IDs in URLs. Use secure, HttpOnly, SameSite cookies.
|
|
183
|
+
- Implement account lockout with notification after repeated failed attempts.
|
|
184
|
+
|
|
185
|
+
### A08 — Software and Data Integrity Failures
|
|
186
|
+
|
|
187
|
+
- Verify integrity of all software updates, dependencies, and CI/CD pipeline artifacts using digital signatures or checksums.
|
|
188
|
+
- Use lockfiles and verify their integrity. `npm ci` (not `npm install`) in CI to ensure deterministic builds.
|
|
189
|
+
- CI/CD pipelines: require code review for all changes, enforce branch protection, sign commits where feasible.
|
|
190
|
+
- Never deserialize untrusted data without validation. Use schemas (zod, JSON Schema) to validate structure before processing.
|
|
191
|
+
- Protect CI/CD secrets and permissions: restrict who can modify pipeline configuration, require approval for deployment steps.
|
|
192
|
+
- Pin GitHub Actions and CI plugins by commit SHA, not mutable tags.
|
|
193
|
+
|
|
194
|
+
### A09 — Security Logging and Monitoring Failures
|
|
195
|
+
|
|
196
|
+
- Log all authentication events (success, failure, lockout), access control failures, input validation failures, and security-relevant business events.
|
|
197
|
+
- Use structured logging with correlation IDs. Include: timestamp, severity, event type, user identity (if available), source IP, resource accessed, outcome.
|
|
198
|
+
- Never log sensitive data: passwords, tokens, PII, credit card numbers, session IDs. Redact before logging.
|
|
199
|
+
- Centralize logs and enable real-time alerting for security events. Alert on: brute-force patterns, privilege escalation, anomalous access patterns.
|
|
200
|
+
- Retain security logs for the compliance-required period (minimum 90 days, typically 1 year).
|
|
201
|
+
- Test that logging works: include security event logging in integration tests. Verify alerts fire during incident response drills.
|
|
202
|
+
|
|
203
|
+
### A10 — Mishandling of Exceptional Conditions
|
|
204
|
+
|
|
205
|
+
- Catch and handle every possible error at the point of occurrence. Uncaught exceptions are a vulnerability — they can crash services, leak state, or bypass security checks.
|
|
206
|
+
- Fail closed, not open. When an error occurs in an authorization check, deny access. When a transaction fails mid-way, roll back completely. Never leave the system in a partially-completed state.
|
|
207
|
+
- Implement global exception handlers as a safety net. All unhandled exceptions must be logged, reported, and result in a safe error response (no stack traces, no internal details).
|
|
208
|
+
- Handle missing and malformed parameters explicitly. Do not rely on language defaults (undefined, null, zero) for security-sensitive logic.
|
|
209
|
+
- Check return values and error codes from all system calls, library functions, and external API responses. Ignored return values are a common source of silent failures.
|
|
210
|
+
- Add observability for error patterns: monitor error rates by category, alert on sudden spikes, and investigate recurring error types as potential attack probes.
|
|
211
|
+
- Roll back incomplete transactions atomically. Partial writes, partial state changes, and orphaned resources are integrity violations.
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Security patterns including input validation, auth enforcement, and AI/agentic security for the project
|
|
3
|
+
alwaysApply: true
|
|
4
|
+
---
|
|
5
|
+
# Security Patterns
|
|
6
|
+
|
|
7
|
+
## Input Validation
|
|
8
|
+
|
|
9
|
+
- Validate at the boundary: API routes, form handlers, webhook receivers, CLI parsers. Never trust data that has crossed a trust boundary.
|
|
10
|
+
- Use type-safe runtime schemas (zod, valibot, joi) co-located with the handler. Compile-time types alone are insufficient.
|
|
11
|
+
- Allowlist over denylist for string inputs (permitted characters, values, formats). Denylists are always incomplete.
|
|
12
|
+
- Enforce length limits, numeric range checks, and format validation (email, URL, UUID, ISO dates) on every external field.
|
|
13
|
+
- Reject unexpected fields with strict/passthrough-off schemas. Unknown keys are an attack surface.
|
|
14
|
+
- File uploads: validate type by magic bytes (not extension), enforce size limits, generate server-side filenames, reject path traversal (`..`, absolute paths, null bytes).
|
|
15
|
+
|
|
16
|
+
## Output Encoding
|
|
17
|
+
|
|
18
|
+
- Apply context-aware encoding: HTML entities for markup, URL-encoding for query params, JavaScript escaping for inline scripts, CSS escaping for style contexts.
|
|
19
|
+
- Never construct HTML from user input without sanitization (DOMPurify or equivalent server-side library). Treat all user content as untrusted.
|
|
20
|
+
- Use parameterized queries / prepared statements for SQL, Firestore filters, and NoSQL queries. Zero tolerance for string concatenation with user input.
|
|
21
|
+
- Enable auto-escaping in template engines by default. Disable only per-expression with review.
|
|
22
|
+
- Sanitize data before logging. Log output is also an injection vector (log forging, ANSI escape injection).
|
|
23
|
+
|
|
24
|
+
## Authentication Enforcement
|
|
25
|
+
|
|
26
|
+
- Auth middleware on every route by default. Public routes require explicit opt-out with code review justification.
|
|
27
|
+
- Token validation: pin allowed algorithms (reject `none`), enforce expiry (`exp`), verify audience (`aud`) and issuer (`iss`) claims. Reject tokens failing any check.
|
|
28
|
+
- Session security: `HttpOnly`, `Secure`, `SameSite=Strict` (or `Lax` with justification) cookies. Rotate session ID on privilege change (login, role switch).
|
|
29
|
+
- Multi-factor authentication for sensitive operations: admin actions, payment, account deletion, API key generation.
|
|
30
|
+
- Rate-limit authentication endpoints (login, token refresh, password reset). Lock accounts or add progressive delays after repeated failures.
|
|
31
|
+
- Invalidate all sessions on password change. Provide "sign out everywhere" capability.
|
|
32
|
+
|
|
33
|
+
## Fail-Closed Defaults
|
|
34
|
+
|
|
35
|
+
- Default deny for authorization. Every permission must be explicitly granted; absence of a rule means deny.
|
|
36
|
+
- Error handlers must not leak internal state: no stack traces, query details, file paths, or dependency versions in responses. Return generic error codes.
|
|
37
|
+
- Fallback to the most restrictive behavior on config parse failure. Misconfiguration must never widen access.
|
|
38
|
+
- Circuit breakers for downstream service failures. Degrade gracefully rather than retrying indefinitely or passing errors upstream.
|
|
39
|
+
- Health checks and readiness probes must not expose sensitive configuration or internal topology.
|
|
40
|
+
- Disable debug endpoints, verbose logging, and source maps in production builds. Gate behind feature flags if needed in staging.
|
|
41
|
+
|
|
42
|
+
## CSRF Protection
|
|
43
|
+
|
|
44
|
+
- Apply synchronizer token pattern or double-submit cookie for all state-mutating requests (POST, PUT, PATCH, DELETE).
|
|
45
|
+
- Set `SameSite` cookie attribute as defense-in-depth. It supplements but does not replace CSRF tokens.
|
|
46
|
+
- For API-only endpoints (no browser cookies), require a custom header (`X-Requested-With` or equivalent) that browsers will not send cross-origin without CORS preflight.
|
|
47
|
+
- Validate `Origin` and `Referer` headers as an additional layer for critical endpoints.
|
|
48
|
+
|
|
49
|
+
## AI & Agentic Security (OWASP Agentic Top 10)
|
|
50
|
+
|
|
51
|
+
### ASI01 — Agent Goal Hijack
|
|
52
|
+
|
|
53
|
+
- Separate system prompts from user input with clear delimiters. Never allow user content to override system instructions.
|
|
54
|
+
- Implement input guardrails: scan user messages for injection patterns before LLM processing.
|
|
55
|
+
- Enforce instruction hierarchy: system > developer > user. Reject attempts to redefine agent purpose.
|
|
56
|
+
- Defend against indirect prompt injection: sanitize and tag content retrieved from external sources (RAG, web, files) before including in context.
|
|
57
|
+
|
|
58
|
+
### ASI02 — Tool Misuse & Exploitation
|
|
59
|
+
|
|
60
|
+
- Deny-by-default tool access. Each tool requires explicit grant per agent role.
|
|
61
|
+
- Enforce parameter schemas on every tool call. Reject calls with unexpected, missing, or out-of-range arguments.
|
|
62
|
+
- Rate-limit tool invocations per agent per time window. Alert on anomalous tool usage patterns.
|
|
63
|
+
- Sandbox tool execution: restrict file system access, network egress, and subprocess spawning.
|
|
64
|
+
|
|
65
|
+
### ASI03 — Identity & Privilege Abuse
|
|
66
|
+
|
|
67
|
+
- Assign unique agent IDs per invocation. Log all actions with agent identity for non-repudiation.
|
|
68
|
+
- Apply least privilege: agents receive scoped credentials, never full user or admin tokens.
|
|
69
|
+
- Prevent privilege escalation across agent boundaries. An agent must not request or inherit higher privileges than its caller.
|
|
70
|
+
- Audit delegation chains: every permission grant from user → agent → sub-agent must be traceable.
|
|
71
|
+
|
|
72
|
+
### ASI04 — Supply Chain Vulnerabilities
|
|
73
|
+
|
|
74
|
+
- Pin MCP server and plugin versions. Never auto-install unverified packages (`npx -y` on untrusted sources).
|
|
75
|
+
- Verify package integrity (checksums, signatures) before loading tools or plugins.
|
|
76
|
+
- Audit third-party prompt templates for injected instructions before use.
|
|
77
|
+
- Maintain an allowlist of approved MCP servers and tool sources.
|
|
78
|
+
|
|
79
|
+
### ASI05 — Unexpected Code Execution
|
|
80
|
+
|
|
81
|
+
- Never execute agent-generated code without sandboxing (isolated container, restricted runtime, no network).
|
|
82
|
+
- Require human review for generated code that touches file system, network, or credentials.
|
|
83
|
+
- Restrict generated code to a safe subset: no `eval`, `exec`, shell commands, or dynamic imports.
|
|
84
|
+
- Enforce file system access controls: agents can only read/write within designated workspace directories.
|
|
85
|
+
|
|
86
|
+
### ASI06 — Memory & Context Poisoning
|
|
87
|
+
|
|
88
|
+
- Validate stored context before reuse. Re-check integrity and relevance of cached agent state on retrieval.
|
|
89
|
+
- Set expiry / TTL for all cached agent memory. Stale context is a poisoning vector.
|
|
90
|
+
- Tag and isolate RAG-retrieved content from trusted system instructions. Never promote retrieved content to system-level authority.
|
|
91
|
+
- Detect tampering: hash or sign stored memory entries, verify on read.
|
|
92
|
+
|
|
93
|
+
### ASI07 — Insecure Inter-Agent Communication
|
|
94
|
+
|
|
95
|
+
- Authenticate agent-to-agent messages. Each agent must verify the identity of its communication partner.
|
|
96
|
+
- Scope delegation tokens: a sub-agent receives only the permissions needed for its specific task.
|
|
97
|
+
- Validate message integrity (signing or HMAC) to prevent tampering in multi-agent workflows.
|
|
98
|
+
- Enforce privilege boundaries: a delegated agent cannot escalate beyond the scope granted by its parent.
|
|
99
|
+
|
|
100
|
+
### ASI08 — Cascading Failures
|
|
101
|
+
|
|
102
|
+
- Implement circuit breakers between agent stages. A failure in one agent must not propagate unchecked.
|
|
103
|
+
- Enforce timeouts on every agent invocation and tool call. No unbounded waits.
|
|
104
|
+
- Contain blast radius: isolate agent workflows so a compromised agent cannot affect unrelated workflows.
|
|
105
|
+
- Log and alert on error chains. Three consecutive failures in an agent chain should trigger automatic halt.
|
|
106
|
+
|
|
107
|
+
### ASI09 — Human-Agent Trust Exploitation
|
|
108
|
+
|
|
109
|
+
- Mandatory human confirmation for destructive operations: file deletion, database writes, external API calls with side effects, financial transactions.
|
|
110
|
+
- Enforce cost limits: cap token usage, API call counts, and compute time per agent invocation.
|
|
111
|
+
- Present agent actions transparently: show the user what the agent did and why, not just the result.
|
|
112
|
+
- Resist social engineering: agents must not bypass confirmation flows based on urgency framing in user input.
|
|
113
|
+
|
|
114
|
+
### ASI10 — Rogue Agents
|
|
115
|
+
|
|
116
|
+
- Monitor agent outputs for policy violations, off-topic responses, and anomalous behavior patterns.
|
|
117
|
+
- Validate agent outputs against expected schemas and content policies before acting on them.
|
|
118
|
+
- Enforce scope: reject agent actions outside the declared task boundary.
|
|
119
|
+
- Implement kill switches: ability to immediately terminate a running agent and revoke its credentials.
|
|
120
|
+
- Run anomaly detection on tool call patterns, output length, and execution time to flag compromised agents.
|
|
121
|
+
|
|
122
|
+
## OWASP Top 10 2025 (Web Application Security)
|
|
123
|
+
|
|
124
|
+
### A01 — Broken Access Control
|
|
125
|
+
|
|
126
|
+
- Enforce access control server-side. Client-side checks are UX, not security.
|
|
127
|
+
- Deny by default: every resource requires explicit permission. Absence of a grant means deny.
|
|
128
|
+
- Implement resource-level ownership checks: verify the authenticated user owns (or has a role granting access to) the requested resource. Parameterized IDs in URLs are not authorization — always validate ownership.
|
|
129
|
+
- Disable directory listing. Restrict access to metadata files (`.git`, `.env`, backup files).
|
|
130
|
+
- Rate-limit API access to minimize automated IDOR scanning and credential stuffing.
|
|
131
|
+
- Log access control failures and alert on repeated violations from the same identity.
|
|
132
|
+
|
|
133
|
+
### A02 — Cryptographic Failures
|
|
134
|
+
|
|
135
|
+
- Classify data by sensitivity (PII, financial, health, credentials). Apply encryption requirements per classification.
|
|
136
|
+
- Encrypt data in transit (TLS 1.2+ mandatory, prefer 1.3) and at rest (AES-256 or equivalent).
|
|
137
|
+
- Never use deprecated algorithms: MD5, SHA-1, DES, RC4, ECB mode. Use SHA-256+ for hashing, AES-GCM for symmetric encryption, RSA-OAEP or ECDSA for asymmetric.
|
|
138
|
+
- Hash passwords with bcrypt, scrypt, or Argon2id with appropriate work factors. Never use raw SHA/MD5 for passwords.
|
|
139
|
+
- Generate cryptographic keys with secure random sources (`crypto.randomBytes`, not `Math.random`). Never hard-code keys or IVs.
|
|
140
|
+
- Disable caching for responses containing sensitive data (`Cache-Control: no-store`).
|
|
141
|
+
|
|
142
|
+
### A03 — Injection
|
|
143
|
+
|
|
144
|
+
- Use parameterized queries or prepared statements for all database operations. Zero tolerance for string concatenation with user input in queries.
|
|
145
|
+
- Apply context-aware output encoding: HTML entities, URL encoding, JavaScript escaping, CSS escaping, LDAP escaping — matched to the output context.
|
|
146
|
+
- Validate and sanitize all external input with allowlist validation. Limit input length, character sets, and format.
|
|
147
|
+
- Use `LIMIT` and pagination in queries to prevent mass data disclosure via injection.
|
|
148
|
+
- For OS command execution: avoid entirely if possible. If necessary, use parameterized APIs (not shell interpolation) with strict input validation.
|
|
149
|
+
|
|
150
|
+
### A04 — Insecure Design
|
|
151
|
+
|
|
152
|
+
- Use threat modeling during design phase (STRIDE, attack trees, or equivalent). Identify trust boundaries and abuse cases before writing code.
|
|
153
|
+
- Establish and enforce secure design patterns: separation of concerns, defense in depth, least privilege, fail-closed.
|
|
154
|
+
- Write abuse-case user stories alongside feature user stories: "As an attacker, I want to..."
|
|
155
|
+
- Design rate limiting, resource quotas, and cost controls into the architecture — not as afterthoughts.
|
|
156
|
+
- Establish secure development lifecycle (SDL) practices: security requirements, design review, code review, testing.
|
|
157
|
+
|
|
158
|
+
### A05 — Security Misconfiguration
|
|
159
|
+
|
|
160
|
+
- Harden all environments: remove default accounts, disable unused features/ports/services, remove sample applications.
|
|
161
|
+
- Use identical security configuration across development, staging, and production. Differences in security settings between environments mask vulnerabilities.
|
|
162
|
+
- Automate configuration verification: infrastructure-as-code with security baselines, configuration scanning in CI.
|
|
163
|
+
- Send security headers on every response (HSTS, CSP, X-Content-Type-Options, X-Frame-Options). Centralize in middleware.
|
|
164
|
+
- Review cloud permissions quarterly. Remove unused IAM roles, security groups, and service accounts.
|
|
165
|
+
- Disable detailed error messages in production. Use generic error responses with correlation IDs for debugging.
|
|
166
|
+
|
|
167
|
+
### A06 — Vulnerable and Outdated Components
|
|
168
|
+
|
|
169
|
+
- Maintain a software bill of materials (SBOM) for all direct and transitive dependencies.
|
|
170
|
+
- Run `npm audit` (or equivalent) in CI on every build. Block merges with critical or high vulnerabilities.
|
|
171
|
+
- Subscribe to security advisories for all critical dependencies (GitHub Dependabot, Snyk, or equivalent).
|
|
172
|
+
- Remove unused dependencies. Unused code with known vulnerabilities is still a risk.
|
|
173
|
+
- Pin dependency versions in lockfiles. Review lockfile changes in PRs with the same scrutiny as code changes.
|
|
174
|
+
- Establish SLAs for vulnerability remediation: critical within 24 hours, high within 1 week, moderate within 1 sprint.
|
|
175
|
+
|
|
176
|
+
### A07 — Identification and Authentication Failures
|
|
177
|
+
|
|
178
|
+
- Implement multi-factor authentication for privileged accounts and sensitive operations.
|
|
179
|
+
- Enforce password complexity requirements: minimum 8 characters, check against breached password databases (Have I Been Pwned API).
|
|
180
|
+
- Protect against credential stuffing: rate-limit login attempts, implement progressive delays, use CAPTCHA after repeated failures.
|
|
181
|
+
- Session management: generate new session ID on login, invalidate on logout, set absolute and idle timeouts.
|
|
182
|
+
- Never expose session IDs in URLs. Use secure, HttpOnly, SameSite cookies.
|
|
183
|
+
- Implement account lockout with notification after repeated failed attempts.
|
|
184
|
+
|
|
185
|
+
### A08 — Software and Data Integrity Failures
|
|
186
|
+
|
|
187
|
+
- Verify integrity of all software updates, dependencies, and CI/CD pipeline artifacts using digital signatures or checksums.
|
|
188
|
+
- Use lockfiles and verify their integrity. `npm ci` (not `npm install`) in CI to ensure deterministic builds.
|
|
189
|
+
- CI/CD pipelines: require code review for all changes, enforce branch protection, sign commits where feasible.
|
|
190
|
+
- Never deserialize untrusted data without validation. Use schemas (zod, JSON Schema) to validate structure before processing.
|
|
191
|
+
- Protect CI/CD secrets and permissions: restrict who can modify pipeline configuration, require approval for deployment steps.
|
|
192
|
+
- Pin GitHub Actions and CI plugins by commit SHA, not mutable tags.
|
|
193
|
+
|
|
194
|
+
### A09 — Security Logging and Monitoring Failures
|
|
195
|
+
|
|
196
|
+
- Log all authentication events (success, failure, lockout), access control failures, input validation failures, and security-relevant business events.
|
|
197
|
+
- Use structured logging with correlation IDs. Include: timestamp, severity, event type, user identity (if available), source IP, resource accessed, outcome.
|
|
198
|
+
- Never log sensitive data: passwords, tokens, PII, credit card numbers, session IDs. Redact before logging.
|
|
199
|
+
- Centralize logs and enable real-time alerting for security events. Alert on: brute-force patterns, privilege escalation, anomalous access patterns.
|
|
200
|
+
- Retain security logs for the compliance-required period (minimum 90 days, typically 1 year).
|
|
201
|
+
- Test that logging works: include security event logging in integration tests. Verify alerts fire during incident response drills.
|
|
202
|
+
|
|
203
|
+
### A10 — Mishandling of Exceptional Conditions
|
|
204
|
+
|
|
205
|
+
- Catch and handle every possible error at the point of occurrence. Uncaught exceptions are a vulnerability — they can crash services, leak state, or bypass security checks.
|
|
206
|
+
- Fail closed, not open. When an error occurs in an authorization check, deny access. When a transaction fails mid-way, roll back completely. Never leave the system in a partially-completed state.
|
|
207
|
+
- Implement global exception handlers as a safety net. All unhandled exceptions must be logged, reported, and result in a safe error response (no stack traces, no internal details).
|
|
208
|
+
- Handle missing and malformed parameters explicitly. Do not rely on language defaults (undefined, null, zero) for security-sensitive logic.
|
|
209
|
+
- Check return values and error codes from all system calls, library functions, and external API responses. Ignored return values are a common source of silent failures.
|
|
210
|
+
- Add observability for error patterns: monitor error rates by category, alert on sudden spikes, and investigate recurring error types as potential attack probes.
|
|
211
|
+
- Roll back incomplete transactions atomically. Partial writes, partial state changes, and orphaned resources are integrity violations.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: hatch3r-testing
|
|
3
|
+
type: rule
|
|
4
|
+
description: Test standards and conventions for the project
|
|
5
|
+
scope: always
|
|
6
|
+
---
|
|
7
|
+
# Testing Standards
|
|
8
|
+
|
|
9
|
+
## Core Principles
|
|
10
|
+
|
|
11
|
+
- Unit tests: project test runner. Integration: test runner + emulators/mocks. E2E: browser automation (Playwright or equivalent).
|
|
12
|
+
- **Deterministic.** Mock time where needed. No wall clock dependency.
|
|
13
|
+
- **Isolated.** Each test sets up and tears down its own state.
|
|
14
|
+
- **Fast.** Unit tests < 50ms. Integration tests < 2s.
|
|
15
|
+
- **Named clearly.** Describe behavior: `"should award 15 XP for 25-min focus block"`.
|
|
16
|
+
- **Regression.** Every bug fix includes a test that fails before the fix and passes after.
|
|
17
|
+
- **No network.** Unit tests must not make network calls. Use mocks.
|
|
18
|
+
- No type escape hatches in tests. No `.skip` without a linked issue.
|
|
19
|
+
- Write tests to `tests/unit/`, `tests/integration/`, `tests/e2e/`, or equivalent.
|
|
20
|
+
- Use test fixtures from `tests/fixtures/` or equivalent.
|
|
21
|
+
- **Browser verification.** For UI changes, verify visually in the browser via browser automation MCP after automated tests pass. Capture screenshots as evidence.
|
|
22
|
+
|
|
23
|
+
## Coverage Thresholds
|
|
24
|
+
|
|
25
|
+
- **Statement coverage:** 80% minimum across the project. New code must not decrease overall coverage.
|
|
26
|
+
- **Branch coverage:** 70% minimum. Uncovered branches must be justified (e.g., defensive error handling unlikely to trigger).
|
|
27
|
+
- **Function coverage:** 80% minimum. Every exported function must have at least one test.
|
|
28
|
+
- **Per-PR gate:** CI blocks merge if the PR decreases coverage by more than 1% in any metric.
|
|
29
|
+
- **Critical modules** (auth, payments, data persistence, security rules): 90% statement, 85% branch minimum.
|
|
30
|
+
- Generate coverage reports in CI and publish as PR comments or artifacts for visibility.
|
|
31
|
+
- Exclude generated code, type declarations, and config files from coverage metrics.
|
|
32
|
+
|
|
33
|
+
## Mocking Strategy
|
|
34
|
+
|
|
35
|
+
- **Prefer fakes over mocks** for stateful dependencies (databases, caches). Fakes implement the real interface with in-memory state, making tests more realistic.
|
|
36
|
+
- **Use stubs** for simple value returns where behavior is irrelevant to the test (e.g., config lookups, feature flags).
|
|
37
|
+
- **Use mocks** (with call verification) only when the interaction itself is the behavior under test (e.g., verifying an event was emitted, an API was called with specific arguments).
|
|
38
|
+
- **Mock boundaries, not internals.** Mock at module/service boundaries (HTTP clients, database drivers, external SDKs). Never mock private functions or internal implementation details.
|
|
39
|
+
- **Reset mocks between tests.** Use `beforeEach` / `afterEach` to restore original implementations. Leaked mock state is a top source of flaky tests.
|
|
40
|
+
- **Type-safe mocks.** Mock implementations must satisfy the same TypeScript interface as the real dependency. Avoid `as any` in mock setup.
|
|
41
|
+
- **No mocking the unit under test.** If you need to mock part of the module you are testing, the module has too many responsibilities — refactor first.
|
|
42
|
+
|
|
43
|
+
## Property-Based Testing
|
|
44
|
+
|
|
45
|
+
- Use a property-based testing library (fast-check or equivalent) for functions with wide input domains.
|
|
46
|
+
- **Priority targets:** parsers, serializers, validators, encoders/decoders, mathematical functions, and any pure function with complex input types.
|
|
47
|
+
- Define invariants as properties: round-trip (encode then decode equals original), idempotency (applying twice equals applying once), monotonicity, commutativity.
|
|
48
|
+
- Use `fc.assert` with at least 100 runs per property. Increase to 1000 for critical paths.
|
|
49
|
+
- When a property test finds a failure, add the minimal counterexample as a dedicated regression unit test.
|
|
50
|
+
- Shrinking must be enabled — it reduces failing inputs to the smallest reproduction case.
|
|
51
|
+
- Property tests belong alongside unit tests in `tests/unit/`. Name them clearly: `"property: round-trip serialization for UserProfile"`.
|
|
52
|
+
|
|
53
|
+
## Mutation Testing
|
|
54
|
+
|
|
55
|
+
- Use Stryker (or equivalent mutation testing framework) on critical modules to measure test effectiveness beyond line coverage.
|
|
56
|
+
- **Mutation score target:** 70% minimum on critical modules (auth, data layer, business rules). 60% minimum project-wide.
|
|
57
|
+
- Run mutation testing in CI on a weekly schedule (not per-PR — too slow). Report results as a CI artifact.
|
|
58
|
+
- **Surviving mutants** indicate tests that pass regardless of code changes — these are false-coverage tests. Fix them by adding assertions that detect the mutation.
|
|
59
|
+
- Focus mutation testing effort on modules where a bug would cause data loss, security vulnerability, or financial impact.
|
|
60
|
+
- Exclude test files, generated code, and UI presentation logic from mutation analysis.
|
|
61
|
+
|
|
62
|
+
## Flaky Test Handling
|
|
63
|
+
|
|
64
|
+
- **Zero tolerance policy.** A flaky test erodes trust in the entire suite. Fix or quarantine within 48 hours of detection.
|
|
65
|
+
- **Quarantine process:** Move the flaky test to a `tests/quarantine/` directory or tag with `.skip("FLAKY: #issue-number")`. Create a tracking issue immediately.
|
|
66
|
+
- **Retry strategy in CI:** Allow a maximum of 1 automatic retry for the full test suite. Never retry individual tests silently — that masks flakiness.
|
|
67
|
+
- **Root cause investigation:** Common causes are shared mutable state, timing dependencies (real clocks, `setTimeout`), port conflicts, uncontrolled randomness, and external service calls.
|
|
68
|
+
- **Fix patterns:** Replace `setTimeout` with fake timers, replace shared state with per-test setup, replace port binding with dynamic ports, seed random generators deterministically.
|
|
69
|
+
- **Flaky test metrics:** Track flaky test rate over time. Target < 0.5% flaky rate (flaky runs / total runs). Alert when rate exceeds 1%.
|
|
70
|
+
- **Quarantine review:** Review quarantined tests weekly. Tests quarantined for more than 30 days must be either fixed or deleted with justification.
|
|
71
|
+
|
|
72
|
+
## Test Data Management
|
|
73
|
+
|
|
74
|
+
- **Factories over fixtures.** Use factory functions (builder pattern) to generate test data with sensible defaults and per-test overrides. Factories produce valid objects by default; tests override only the fields relevant to the scenario.
|
|
75
|
+
- **Builder pattern example:** `buildUser({ role: "admin" })` returns a full valid User with admin role and random but valid defaults for all other fields.
|
|
76
|
+
- **No shared mutable fixtures.** If multiple tests read the same fixture data, each test must get its own copy. Use `structuredClone()` or factory functions.
|
|
77
|
+
- **Realistic data.** Use faker or equivalent for generating realistic names, emails, dates. Avoid magic strings like `"test"`, `"foo"`, `"abc123"`.
|
|
78
|
+
- **Deterministic seeding.** When using random data generators, seed them per test file so failures are reproducible.
|
|
79
|
+
- **Fixture files** (JSON, YAML) are acceptable for large, complex, or externally-sourced test inputs (API response snapshots, configuration samples). Store in `tests/fixtures/`.
|
|
80
|
+
- **Database state:** Integration tests that require database state must set up and tear down within the test using helpers. Never depend on database state from a previous test.
|
|
81
|
+
|
|
82
|
+
## Snapshot Testing
|
|
83
|
+
|
|
84
|
+
- **Use sparingly.** Snapshots are appropriate for serialized output (JSON API responses, CLI output, rendered HTML structure) where the exact output matters and is stable.
|
|
85
|
+
- **Not appropriate for:** UI component visual appearance (use visual regression tests), objects with timestamps or random IDs (unstable), large objects (unreadable diffs).
|
|
86
|
+
- **Review discipline.** Snapshot updates (`--update-snapshots`) must be reviewed as carefully as code changes. Reviewers must verify the new snapshot is intentionally correct, not just "different."
|
|
87
|
+
- **Keep snapshots small.** Snapshot files > 100 lines suggest the test is asserting too broadly. Narrow the assertion to the relevant subset.
|
|
88
|
+
- **Inline snapshots** (where supported) are preferred over external `.snap` files for short outputs (< 20 lines) because they keep the assertion co-located with the test.
|
|
89
|
+
- **Name snapshot files** to match their test file: `auth.test.ts` → `auth.test.ts.snap`.
|