@umacloud/knowledge 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/00-governance/governance-capabilities.md +557 -0
- package/00-governance/knowledge-map.md +39 -0
- package/00-governance/maintenance-policy.md +76 -0
- package/00-governance/review-checklist.md +81 -0
- package/README.md +13 -0
- package/ai/01-standards/agent-development-complete.md +691 -0
- package/ai/01-standards/llm-application-complete.md +488 -0
- package/ai/01-standards/mlops-complete.md +798 -0
- package/ai/01-standards/prompt-engineering-complete.md +646 -0
- package/ai/01-standards/rag-architecture-complete.md +649 -0
- package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
- package/ai/03-checklists/ai-project-checklist.md +215 -0
- package/ai/04-antipatterns/ai-antipatterns.md +661 -0
- package/ai/05-cases/case-rag-production.md +147 -0
- package/ai/06-glossary/ai-glossary.md +162 -0
- package/ai/agent-evaluation-benchmark.md +53 -0
- package/ai/ai-agent-memory-context-management.md +41 -0
- package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
- package/ai/ai-data-security-and-compliance-playbook.md +37 -0
- package/ai/ai-domain-index-and-checklist.md +40 -0
- package/ai/ai-governance-maturity-model.md +50 -0
- package/ai/ai-model-selection-and-routing-strategy.md +47 -0
- package/ai/ai-observability-and-oncall-runbook.md +52 -0
- package/ai/ai-rag-engineering-playbook.md +42 -0
- package/ai/ai-red-team-and-safety-evaluation.md +42 -0
- package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
- package/ai/llm-agent-engineering-deep-dive.md +57 -0
- package/ai/prompt-and-tool-guardrails.md +52 -0
- package/api/01-standards/enterprise-api-standards.md +198 -0
- package/api/01-standards/rest-api-design-guide.md +63 -0
- package/api/02-playbooks/api-pagination-playbook.md +93 -0
- package/api/02-playbooks/graphql-production-playbook.md +176 -0
- package/api/03-checklists/api-review-checklist.md +55 -0
- package/api/04-antipatterns/api-antipatterns.md +112 -0
- package/architecture/01-standards/api-gateway-patterns.md +496 -0
- package/architecture/01-standards/cloud-native-patterns.md +644 -0
- package/architecture/01-standards/distributed-systems-patterns.md +591 -0
- package/architecture/01-standards/event-driven-architecture.md +595 -0
- package/architecture/01-standards/microservices-patterns-complete.md +968 -0
- package/architecture/01-standards/microservices-patterns.md +495 -0
- package/architecture/01-standards/system-design-interview.md +664 -0
- package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
- package/architecture/02-playbooks/migration-playbook.md +780 -0
- package/architecture/02-playbooks/system-design-playbook.md +779 -0
- package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
- package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
- package/architecture/05-cases/case-netflix-microservices.md +413 -0
- package/architecture/06-glossary/architecture-glossary.md +164 -0
- package/architecture/adr-template-and-examples.md +38 -0
- package/architecture/api-gateway-deep-dive.md +1291 -0
- package/architecture/configuration-management.md +1162 -0
- package/architecture/distributed-transactions.md +1220 -0
- package/architecture/microservices-complete.md +735 -0
- package/architecture/resilience-and-disaster-patterns.md +37 -0
- package/architecture/service-governance.md +1198 -0
- package/architecture/system-architecture-deep-dive.md +37 -0
- package/backend/01-standards/analytics-and-growth.md +65 -0
- package/backend/01-standards/api-and-error-conventions.md +120 -0
- package/backend/01-standards/application-layering-and-packaging.md +160 -0
- package/backend/01-standards/auth-implementation.md +104 -0
- package/backend/01-standards/backend-framework-idioms.md +74 -0
- package/backend/01-standards/background-jobs-and-async.md +66 -0
- package/backend/01-standards/caching-strategies-complete.md +390 -0
- package/backend/01-standards/config-and-observability.md +77 -0
- package/backend/01-standards/data-modeling-and-persistence.md +94 -0
- package/backend/01-standards/django-complete.md +1765 -0
- package/backend/01-standards/email-and-notifications.md +64 -0
- package/backend/01-standards/fastapi-complete.md +925 -0
- package/backend/01-standards/file-upload-and-storage.md +66 -0
- package/backend/01-standards/graphql-api-complete.md +416 -0
- package/backend/01-standards/llm-application-standard.md +78 -0
- package/backend/01-standards/message-queue-patterns.md +379 -0
- package/backend/01-standards/microservices-and-distributed.md +78 -0
- package/backend/01-standards/nestjs-complete.md +2167 -0
- package/backend/01-standards/payment-integration.md +80 -0
- package/backend/01-standards/rate-limiting-complete.md +451 -0
- package/backend/01-standards/realtime-and-websocket.md +65 -0
- package/backend/01-standards/search-and-filtering.md +64 -0
- package/backend/01-standards/spring-boot-complete.md +445 -0
- package/backend/02-playbooks/api-design-playbook.md +718 -0
- package/backend/02-playbooks/email-send-playbook.md +130 -0
- package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
- package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
- package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
- package/backend/03-checklists/api-launch-checklist.md +189 -0
- package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
- package/blockchain/01-standards/blockchain-basics.md +557 -0
- package/blockchain/01-standards/smart-contract-development.md +1315 -0
- package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
- package/cicd/01-standards/github-actions-complete.md +473 -0
- package/cicd/01-standards/release-and-store-submission.md +75 -0
- package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
- package/cicd/02-playbooks/release-management-playbook.md +605 -0
- package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
- package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
- package/cicd/05-cases/case-deployment-automation.md +221 -0
- package/cicd/05-cases/case-gitops-transformation.md +212 -0
- package/cicd/06-glossary/cicd-glossary.md +114 -0
- package/cicd/cicd-blueprint-deep-dive.md +38 -0
- package/cicd/release-readiness-gate.md +37 -0
- package/cloud-native/01-standards/container-security.md +741 -0
- package/cloud-native/01-standards/kubernetes-complete.md +812 -0
- package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
- package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
- package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
- package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
- package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
- package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
- package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
- package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
- package/cloud-native/03-checklists/container-security-checklist.md +431 -0
- package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
- package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
- package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
- package/cloud-native/05-cases/case-k8s-migration.md +478 -0
- package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
- package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
- package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
- package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
- package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
- package/data/01-standards/elasticsearch-complete.md +2098 -0
- package/data/01-standards/postgresql-complete.md +1613 -0
- package/data/01-standards/redis-complete.md +1527 -0
- package/data/02-playbooks/database-optimization-playbook.md +403 -0
- package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
- package/data/03-checklists/database-launch-checklist.md +187 -0
- package/data/04-antipatterns/database-antipatterns.md +873 -0
- package/data/05-cases/case-database-migration.md +310 -0
- package/data/06-glossary/database-glossary.md +440 -0
- package/data/data-governance-and-modeling-deep-dive.md +39 -0
- package/data-engineering/01-standards/airflow-complete.md +523 -0
- package/data-engineering/01-standards/kafka-complete.md +1521 -0
- package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
- package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
- package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
- package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
- package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
- package/database/01-standards/database-schema-standards.md +147 -0
- package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
- package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
- package/database/02-playbooks/postgresql-production-playbook.md +146 -0
- package/database/02-playbooks/redis-caching-playbook.md +117 -0
- package/database/03-checklists/database-review-checklist.md +50 -0
- package/database/04-antipatterns/database-antipatterns.md +112 -0
- package/design/01-standards/ui-design-system-complete.md +423 -0
- package/design/02-playbooks/design-handoff-playbook.md +254 -0
- package/design/02-playbooks/design-review-playbook.md +388 -0
- package/design/03-checklists/design-review-checklist.md +246 -0
- package/design/04-antipatterns/design-antipatterns.md +378 -0
- package/design/05-cases/case-design-system-adoption.md +328 -0
- package/design/06-glossary/design-glossary.md +329 -0
- package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
- package/design/ux-system-deep-dive.md +38 -0
- package/design-systems/00-craft-rules.md +71 -0
- package/design-systems/aesthetic-families.md +43 -0
- package/design-systems/anti-ai-slop.md +162 -0
- package/design-systems/bold-geometric.md +120 -0
- package/design-systems/brutalist-bold.md +103 -0
- package/design-systems/editorial-clean.md +109 -0
- package/design-systems/glass-aurora.md +108 -0
- package/design-systems/modern-minimal.md +145 -0
- package/design-systems/premium-luxury.md +106 -0
- package/design-systems/product-type-design-map.md +48 -0
- package/design-systems/soft-warm.md +123 -0
- package/design-systems/tech-utility.md +113 -0
- package/desktop/01-standards/desktop-app-standard.md +72 -0
- package/desktop/01-standards/desktop-design.md +71 -0
- package/development/00-governance/document-template.md +41 -0
- package/development/01-standards/api-versioning-strategies.md +432 -0
- package/development/01-standards/authentication-patterns-complete.md +479 -0
- package/development/01-standards/css-architecture-complete.md +550 -0
- package/development/01-standards/database-migration-strategies.md +484 -0
- package/development/01-standards/elasticsearch-complete.md +347 -0
- package/development/01-standards/git-complete.md +371 -0
- package/development/01-standards/golang-complete.md +1565 -0
- package/development/01-standards/graphql-complete.md +298 -0
- package/development/01-standards/javascript-bundlers-complete.md +469 -0
- package/development/01-standards/javascript-typescript-complete.md +528 -0
- package/development/01-standards/jest-complete.md +275 -0
- package/development/01-standards/linux-complete.md +234 -0
- package/development/01-standards/logging-observability-complete.md +526 -0
- package/development/01-standards/microservices-communication.md +502 -0
- package/development/01-standards/mongodb-complete.md +406 -0
- package/development/01-standards/oauth2-complete.md +285 -0
- package/development/01-standards/performance-optimization-complete.md +289 -0
- package/development/01-standards/playwright-complete.md +247 -0
- package/development/01-standards/postgresql-complete.md +456 -0
- package/development/01-standards/pytest-complete.md +340 -0
- package/development/01-standards/python-async-programming.md +902 -0
- package/development/01-standards/python-complete.md +956 -0
- package/development/01-standards/python-decorators-complete.md +799 -0
- package/development/01-standards/python-design-patterns.md +2854 -0
- package/development/01-standards/python-packaging-distribution.md +420 -0
- package/development/01-standards/python-testing-strategies.md +607 -0
- package/development/01-standards/python-web-frameworks-comparison.md +471 -0
- package/development/01-standards/redis-complete.md +317 -0
- package/development/01-standards/rest-api-complete.md +316 -0
- package/development/01-standards/rust-complete.md +578 -0
- package/development/01-standards/typescript-advanced-types.md +1513 -0
- package/development/01-standards/web-security-complete.md +292 -0
- package/development/02-playbooks/api-design-playbook.md +810 -0
- package/development/02-playbooks/database-migration-playbook.md +580 -0
- package/development/02-playbooks/debugging-playbook.md +692 -0
- package/development/02-playbooks/feature-delivery-playbook.md +430 -0
- package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
- package/development/02-playbooks/performance-optimization-playbook.md +531 -0
- package/development/02-playbooks/performance-tuning-playbook.md +652 -0
- package/development/02-playbooks/refactor-playbook.md +403 -0
- package/development/02-playbooks/release-playbook.md +469 -0
- package/development/03-checklists/architecture-review-checklist.md +168 -0
- package/development/03-checklists/data-migration-checklist.md +157 -0
- package/development/03-checklists/oncall-handover-checklist.md +173 -0
- package/development/03-checklists/pr-checklist.md +158 -0
- package/development/03-checklists/production-readiness-checklist.md +190 -0
- package/development/03-checklists/release-readiness-checklist.md +154 -0
- package/development/03-checklists/security-review-checklist.md +182 -0
- package/development/04-antipatterns/api-antipatterns.md +657 -0
- package/development/04-antipatterns/architecture-antipatterns.md +686 -0
- package/development/04-antipatterns/backend-antipatterns.md +648 -0
- package/development/04-antipatterns/cicd-antipatterns.md +540 -0
- package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
- package/development/04-antipatterns/data-antipatterns.md +658 -0
- package/development/04-antipatterns/database-antipatterns.md +578 -0
- package/development/04-antipatterns/frontend-antipatterns.md +635 -0
- package/development/04-antipatterns/reliability-antipatterns.md +700 -0
- package/development/04-antipatterns/security-antipatterns.md +747 -0
- package/development/05-cases/case-api-version-migration.md +428 -0
- package/development/05-cases/case-authorization-hardening.md +383 -0
- package/development/05-cases/case-bluegreen-rollback.md +466 -0
- package/development/05-cases/case-cache-snowball-protection.md +485 -0
- package/development/05-cases/case-ci-cd-pipeline.md +544 -0
- package/development/05-cases/case-database-scaling.md +500 -0
- package/development/05-cases/case-db-hotspot-optimization.md +487 -0
- package/development/05-cases/case-incident-mttr-reduction.md +563 -0
- package/development/05-cases/case-microservice-migration.md +375 -0
- package/development/05-cases/case-performance-optimization.md +406 -0
- package/development/05-cases/case-security-incident-response.md +345 -0
- package/development/06-glossary/full-stack-glossary.md +166 -0
- package/development/09-maturity/quarterly-audit-template.md +35 -0
- package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
- package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
- package/development/12-scenarios/development-scenarios-guide.md +565 -0
- package/development/13-implementation-assets/implementation-toolkit.md +282 -0
- package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
- package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
- package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
- package/development/api-contract-and-versioning-guide.md +36 -0
- package/development/api-governance-complete.md +43 -0
- package/development/backend-engineering-complete.md +43 -0
- package/development/code-review-quality-complete.md +43 -0
- package/development/concurrency-reliability-complete.md +43 -0
- package/development/database-engineering-complete.md +43 -0
- package/development/engineering-effectiveness-complete.md +43 -0
- package/development/engineering-standards-deep-dive.md +38 -0
- package/development/frontend-engineering-complete.md +43 -0
- package/development/performance-capacity-complete.md +43 -0
- package/development/refactor-migration-complete.md +42 -0
- package/development/refactoring-and-techdebt-playbook.md +37 -0
- package/development/security-in-development-complete.md +43 -0
- package/devops/01-standards/cicd-pipeline-complete.md +262 -0
- package/devops/01-standards/docker-complete.md +1490 -0
- package/devops/01-standards/github-actions-complete.md +337 -0
- package/devops/01-standards/kubernetes-complete.md +638 -0
- package/devops/01-standards/terraform-complete.md +2117 -0
- package/devops/02-playbooks/docker-compose-playbook.md +233 -0
- package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
- package/devops/02-playbooks/docker-production-playbook.md +952 -0
- package/edge-iot/01-standards/edge-iot-complete.md +473 -0
- package/experts/architect/api-design.md +178 -0
- package/experts/architect/methodology.md +124 -0
- package/experts/architect/security.md +75 -0
- package/experts/backend-lead/methodology.md +216 -0
- package/experts/devops/methodology.md +160 -0
- package/experts/frontend-lead/methodology.md +178 -0
- package/experts/product-manager/industry/ecommerce.md +43 -0
- package/experts/product-manager/industry/saas.md +40 -0
- package/experts/product-manager/methodology.md +97 -0
- package/experts/qa-lead/methodology.md +123 -0
- package/experts/qa-lead/test-strategy.md +128 -0
- package/experts/uiux-designer/methodology.md +125 -0
- package/frontend/01-standards/accessibility-complete.md +532 -0
- package/frontend/01-standards/accessibility-standard.md +74 -0
- package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
- package/frontend/01-standards/design-tokens-complete.md +444 -0
- package/frontend/01-standards/forms-and-validation.md +77 -0
- package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
- package/frontend/01-standards/i18n-and-localization.md +65 -0
- package/frontend/01-standards/nextjs-complete.md +451 -0
- package/frontend/01-standards/react-complete.md +713 -0
- package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
- package/frontend/01-standards/react-hooks-complete.md +1171 -0
- package/frontend/01-standards/seo-and-web-vitals.md +77 -0
- package/frontend/01-standards/state-management-complete.md +444 -0
- package/frontend/01-standards/vue-complete.md +499 -0
- package/frontend/01-standards/vue3-complete.md +2002 -0
- package/frontend/01-standards/web-framework-best-practices.md +64 -0
- package/frontend/01-standards/web-performance-complete.md +495 -0
- package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
- package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
- package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
- package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
- package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
- package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
- package/frontend/03-checklists/component-quality-checklist.md +166 -0
- package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
- package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
- package/frontend/05-cases/case-performance-optimization.md +274 -0
- package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
- package/harmony/01-standards/harmonyos-design.md +65 -0
- package/high-quality-engineering-playbook.md +54 -0
- package/incident/01-standards/incident-response-complete.md +303 -0
- package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
- package/incident/02-playbooks/postmortem-playbook.md +398 -0
- package/incident/03-checklists/incident-readiness-checklist.md +181 -0
- package/incident/04-antipatterns/incident-antipatterns.md +490 -0
- package/incident/05-cases/case-cascade-failure.md +176 -0
- package/incident/06-glossary/incident-glossary.md +114 -0
- package/incident/postmortem-and-response-deep-dive.md +39 -0
- package/industries/ecommerce/ecommerce-complete.md +631 -0
- package/industries/education/education-complete.md +555 -0
- package/industries/fintech/fintech-complete.md +501 -0
- package/industries/gaming/gaming-complete.md +587 -0
- package/industries/healthcare/healthcare-complete.md +452 -0
- package/low-code/01-standards/low-code-complete.md +944 -0
- package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
- package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
- package/miniprogram/01-standards/miniprogram-design.md +61 -0
- package/miniprogram/01-standards/miniprogram-standard.md +81 -0
- package/mobile/01-standards/android-material-design.md +70 -0
- package/mobile/01-standards/flutter-complete.md +384 -0
- package/mobile/01-standards/ios-design-hig.md +78 -0
- package/mobile/01-standards/mobile-app-standard.md +85 -0
- package/mobile/01-standards/react-native-complete.md +352 -0
- package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
- package/mobile/02-playbooks/mobile-performance.md +473 -0
- package/mobile/03-checklists/mobile-release-checklist.md +234 -0
- package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
- package/mobile/05-cases/case-app-performance.md +500 -0
- package/mobile/05-cases/case-app-startup-optimization.md +218 -0
- package/mobile/06-glossary/mobile-glossary.md +484 -0
- package/observability/01-standards/observability-standards.md +103 -0
- package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
- package/observability/02-playbooks/structured-logging-playbook.md +73 -0
- package/observability/03-checklists/observability-checklist.md +54 -0
- package/observability/04-antipatterns/observability-antipatterns.md +106 -0
- package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
- package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
- package/operations/03-checklists/production-launch-checklist.md +365 -0
- package/operations/04-antipatterns/operations-antipatterns.md +664 -0
- package/operations/05-cases/case-sre-practices.md +581 -0
- package/operations/06-glossary/operations-glossary.md +120 -0
- package/operations/aiops-anomaly-detection.md +758 -0
- package/operations/capacity-planning.md +1061 -0
- package/operations/chaos-engineering.md +659 -0
- package/operations/incident-command-system.md +38 -0
- package/operations/observability-complete.md +442 -0
- package/operations/slo-sli-playbook.md +517 -0
- package/operations/sre-operations-deep-dive.md +39 -0
- package/package.json +8 -0
- package/performance/01-standards/performance-and-scalability.md +80 -0
- package/performance/01-standards/performance-standards.md +156 -0
- package/performance/02-playbooks/query-optimization-playbook.md +103 -0
- package/performance/03-checklists/performance-checklist.md +56 -0
- package/performance/04-antipatterns/performance-antipatterns.md +146 -0
- package/product/01-standards/product-management-complete.md +285 -0
- package/product/02-playbooks/feature-launch-playbook.md +207 -0
- package/product/02-playbooks/user-research-playbook.md +532 -0
- package/product/03-checklists/feature-launch-checklist.md +275 -0
- package/product/04-antipatterns/product-antipatterns.md +355 -0
- package/product/05-cases/case-mvp-to-scale.md +384 -0
- package/product/06-glossary/product-glossary.md +462 -0
- package/product/feature-prioritization-framework.md +40 -0
- package/product/kpi-and-metric-tree.md +37 -0
- package/product/product-discovery-and-prd-deep-dive.md +41 -0
- package/quantum/01-standards/quantum-complete.md +1186 -0
- package/security/01-standards/api-security-complete.md +511 -0
- package/security/01-standards/container-runtime-security.md +574 -0
- package/security/01-standards/data-protection-gdpr.md +543 -0
- package/security/01-standards/owasp-top10-complete.md +1890 -0
- package/security/01-standards/secure-coding-baseline.md +90 -0
- package/security/01-standards/supply-chain-security.md +441 -0
- package/security/01-standards/web-security-checklist.md +108 -0
- package/security/01-standards/zero-trust-architecture.md +521 -0
- package/security/02-playbooks/auth-sso-playbook.md +166 -0
- package/security/02-playbooks/incident-response-security-playbook.md +588 -0
- package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
- package/security/02-playbooks/payment-integration-playbook.md +119 -0
- package/security/02-playbooks/penetration-testing-playbook.md +517 -0
- package/security/03-checklists/security-audit-checklist.md +356 -0
- package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
- package/security/05-cases/case-log4shell-incident.md +537 -0
- package/security/05-cases/case-major-breaches.md +468 -0
- package/security/06-glossary/security-glossary.md +212 -0
- package/security/compliance-automation.md +993 -0
- package/security/container-security.md +680 -0
- package/security/devsecops-complete.md +426 -0
- package/security/sast-dast-sca.md +775 -0
- package/security/secrets-management.md +594 -0
- package/security/security-architecture-deep-dive.md +37 -0
- package/security/threat-modeling-stride-playbook.md +40 -0
- package/seed-templates/auth-system.md +59 -0
- package/seed-templates/blog-content.md +94 -0
- package/seed-templates/dashboard.md +89 -0
- package/seed-templates/docs-site.md +73 -0
- package/seed-templates/e-commerce.md +50 -0
- package/seed-templates/saas-landing.md +92 -0
- package/seed-templates/settings-page.md +51 -0
- package/testing/01-standards/test-strategy-and-layering.md +83 -0
- package/testing/01-standards/testing-strategy-complete.md +422 -0
- package/testing/01-standards/unit-testing-best-practices.md +118 -0
- package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
- package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
- package/testing/03-checklists/test-strategy-checklist.md +208 -0
- package/testing/04-antipatterns/testing-antipatterns.md +718 -0
- package/testing/05-cases/case-testing-transformation.md +300 -0
- package/testing/06-glossary/testing-glossary.md +110 -0
- package/testing/risk-based-test-matrix.md +36 -0
- package/testing/testing-strategy-deep-dive.md +37 -0
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: api-antipatterns
|
|
3
|
+
title: API 反模式指南
|
|
4
|
+
domain: development
|
|
5
|
+
category: 04-antipatterns
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [antipatterns, api, development, error, handling, limiting, naming, rate]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# API 反模式指南
|
|
12
|
+
|
|
13
|
+
> 适用范围:RESTful API / GraphQL / gRPC
|
|
14
|
+
> 约束级别:SHALL(必须在 API Review 阶段拦截)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. 不一致的命名(Inconsistent Naming)
|
|
19
|
+
|
|
20
|
+
### 描述
|
|
21
|
+
同一 API 中混用不同的命名风格(camelCase / snake_case / kebab-case),资源名称单复数不一致,动词名词混用。导致调用方无法预测 API 的 URL 和字段名,集成成本持续上升。
|
|
22
|
+
|
|
23
|
+
### 错误示例
|
|
24
|
+
```
|
|
25
|
+
# 路径命名混乱
|
|
26
|
+
GET /api/getUsers # 动词 + 驼峰
|
|
27
|
+
GET /api/order_list # 蛇形 + 单数
|
|
28
|
+
POST /api/Create-Payment # 大写 + kebab
|
|
29
|
+
GET /api/v1/product/123 # 单数
|
|
30
|
+
GET /api/v1/categories # 复数
|
|
31
|
+
|
|
32
|
+
# 响应字段命名不一致
|
|
33
|
+
{
|
|
34
|
+
"userId": 1, // camelCase
|
|
35
|
+
"user_name": "Alice", // snake_case
|
|
36
|
+
"Email": "alice@example.com", // PascalCase
|
|
37
|
+
"phone-number": "138xxxx" // kebab-case
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 正确示例
|
|
42
|
+
```
|
|
43
|
+
# 统一的 RESTful 命名规范
|
|
44
|
+
GET /api/v1/users # 复数名词
|
|
45
|
+
GET /api/v1/users/123 # 单个资源
|
|
46
|
+
POST /api/v1/users # 创建
|
|
47
|
+
PUT /api/v1/users/123 # 全量更新
|
|
48
|
+
PATCH /api/v1/users/123 # 部分更新
|
|
49
|
+
DELETE /api/v1/users/123 # 删除
|
|
50
|
+
GET /api/v1/users/123/orders # 嵌套资源
|
|
51
|
+
|
|
52
|
+
# 统一的响应字段(选择一种风格并贯穿整个 API)
|
|
53
|
+
{
|
|
54
|
+
"user_id": 1,
|
|
55
|
+
"user_name": "Alice",
|
|
56
|
+
"email": "alice@example.com",
|
|
57
|
+
"phone_number": "138xxxx"
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
# 使用 OpenAPI Schema 强制命名规范
|
|
63
|
+
from pydantic import BaseModel, ConfigDict
|
|
64
|
+
|
|
65
|
+
class UserResponse(BaseModel):
|
|
66
|
+
model_config = ConfigDict(
|
|
67
|
+
alias_generator=to_camel, # 或统一 snake_case
|
|
68
|
+
populate_by_name=True,
|
|
69
|
+
)
|
|
70
|
+
user_id: int
|
|
71
|
+
user_name: str
|
|
72
|
+
email: str
|
|
73
|
+
phone_number: str | None = None
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 检测方法
|
|
77
|
+
- OpenAPI / Swagger 文档审查:对所有 path 和 field 检查命名一致性。
|
|
78
|
+
- `spectral` lint 工具:自定义规则检查命名风格。
|
|
79
|
+
- API 测试中断言响应字段命名符合约定。
|
|
80
|
+
|
|
81
|
+
### 修复步骤
|
|
82
|
+
1. 制定团队 API 命名规范文档(路径用 kebab-case 或 snake_case,字段用 snake_case 或 camelCase)。
|
|
83
|
+
2. 使用 `spectral` 或自定义 lint 规则在 CI 中检查 OpenAPI spec。
|
|
84
|
+
3. 通过 Pydantic / Serializer 的 alias 机制统一输出格式。
|
|
85
|
+
4. 对已发布的 API,通过新版本逐步迁移(不可破坏性变更)。
|
|
86
|
+
|
|
87
|
+
### Agent Checklist
|
|
88
|
+
- [ ] API 路径统一使用复数名词、小写、无动词
|
|
89
|
+
- [ ] 响应字段统一使用同一种命名风格
|
|
90
|
+
- [ ] CI 包含 OpenAPI lint 检查
|
|
91
|
+
- [ ] 新旧接口命名风格一致
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## 2. 无版本控制(Missing API Versioning)
|
|
96
|
+
|
|
97
|
+
### 描述
|
|
98
|
+
API 没有版本号,任何变更直接影响所有调用方。当需要做不兼容变更时,要么破坏现有客户端,要么在代码中维护大量兼容逻辑。
|
|
99
|
+
|
|
100
|
+
### 错误示例
|
|
101
|
+
```python
|
|
102
|
+
# 无版本号 -- 任何变更影响所有客户端
|
|
103
|
+
@app.get("/users/{user_id}")
|
|
104
|
+
def get_user(user_id: int):
|
|
105
|
+
user = user_repo.get(user_id)
|
|
106
|
+
return {
|
|
107
|
+
"id": user.id,
|
|
108
|
+
"name": user.name, # 后来要拆分为 first_name + last_name
|
|
109
|
+
"email": user.email,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# "向下兼容"导致代码腐化
|
|
113
|
+
@app.get("/users/{user_id}")
|
|
114
|
+
def get_user(user_id: int, format: str = "v1"):
|
|
115
|
+
user = user_repo.get(user_id)
|
|
116
|
+
if format == "v1":
|
|
117
|
+
return {"id": user.id, "name": user.name}
|
|
118
|
+
elif format == "v2":
|
|
119
|
+
return {"id": user.id, "first_name": user.first_name, "last_name": user.last_name}
|
|
120
|
+
elif format == "v3":
|
|
121
|
+
# ... 越来越多的分支
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 正确示例
|
|
125
|
+
```python
|
|
126
|
+
# URL 路径版本控制
|
|
127
|
+
from fastapi import APIRouter
|
|
128
|
+
|
|
129
|
+
router_v1 = APIRouter(prefix="/api/v1")
|
|
130
|
+
router_v2 = APIRouter(prefix="/api/v2")
|
|
131
|
+
|
|
132
|
+
@router_v1.get("/users/{user_id}")
|
|
133
|
+
def get_user_v1(user_id: int) -> UserResponseV1:
|
|
134
|
+
user = user_repo.get(user_id)
|
|
135
|
+
return UserResponseV1(id=user.id, name=f"{user.first_name} {user.last_name}")
|
|
136
|
+
|
|
137
|
+
@router_v2.get("/users/{user_id}")
|
|
138
|
+
def get_user_v2(user_id: int) -> UserResponseV2:
|
|
139
|
+
user = user_repo.get(user_id)
|
|
140
|
+
return UserResponseV2(
|
|
141
|
+
id=user.id, first_name=user.first_name, last_name=user.last_name
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
app.include_router(router_v1)
|
|
145
|
+
app.include_router(router_v2)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
# Header 版本控制(适用于微服务间调用)
|
|
150
|
+
from fastapi import Header, HTTPException
|
|
151
|
+
|
|
152
|
+
@app.get("/users/{user_id}")
|
|
153
|
+
def get_user(user_id: int, x_api_version: str = Header("2024-01-01")):
|
|
154
|
+
user = user_repo.get(user_id)
|
|
155
|
+
if x_api_version < "2024-06-01":
|
|
156
|
+
return UserResponseV1.from_user(user)
|
|
157
|
+
return UserResponseV2.from_user(user)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 检测方法
|
|
161
|
+
- API 路径中不包含版本号(`/v1/`、`/v2/`)。
|
|
162
|
+
- 无 `Accept-Version` 或 `X-API-Version` header 支持。
|
|
163
|
+
- 同一个 endpoint handler 中包含版本分支逻辑。
|
|
164
|
+
- 客户端集成文档未说明版本策略。
|
|
165
|
+
|
|
166
|
+
### 修复步骤
|
|
167
|
+
1. 选择版本控制策略(URL 路径 vs Header vs Query Param)。
|
|
168
|
+
2. 为当前 API 标记为 v1,所有路径加上 `/api/v1/` 前缀。
|
|
169
|
+
3. 设立弃用策略:旧版本至少维护 N 个月后下线。
|
|
170
|
+
4. 在 API 文档和响应 Header 中标注版本信息和弃用时间。
|
|
171
|
+
|
|
172
|
+
### Agent Checklist
|
|
173
|
+
- [ ] 所有 API 路径包含版本号
|
|
174
|
+
- [ ] 不兼容变更通过新版本发布
|
|
175
|
+
- [ ] 旧版本有明确的弃用时间表
|
|
176
|
+
- [ ] API 文档标注当前最新版本
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## 3. 缺乏错误处理(Poor Error Handling)
|
|
181
|
+
|
|
182
|
+
### 描述
|
|
183
|
+
API 返回的错误信息不统一、不可机器解析、或泄露内部实现细节。调用方无法程序化处理错误,只能靠人工阅读错误消息。
|
|
184
|
+
|
|
185
|
+
### 错误示例
|
|
186
|
+
```python
|
|
187
|
+
@app.post("/orders")
|
|
188
|
+
def create_order(data: dict):
|
|
189
|
+
try:
|
|
190
|
+
order = order_service.create(data)
|
|
191
|
+
return order
|
|
192
|
+
except Exception as e:
|
|
193
|
+
# 问题 1: 所有错误返回 500
|
|
194
|
+
# 问题 2: 泄露堆栈信息
|
|
195
|
+
# 问题 3: 无统一格式
|
|
196
|
+
return {"error": str(e)}, 500
|
|
197
|
+
|
|
198
|
+
# 不同接口返回不同的错误格式
|
|
199
|
+
# 接口 A: {"error": "Not found"}
|
|
200
|
+
# 接口 B: {"message": "用户不存在", "code": -1}
|
|
201
|
+
# 接口 C: {"err_msg": "insufficient balance", "err_code": 40001}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 正确示例
|
|
205
|
+
```python
|
|
206
|
+
from enum import Enum
|
|
207
|
+
from pydantic import BaseModel
|
|
208
|
+
|
|
209
|
+
class ErrorCode(str, Enum):
|
|
210
|
+
VALIDATION_ERROR = "VALIDATION_ERROR"
|
|
211
|
+
NOT_FOUND = "NOT_FOUND"
|
|
212
|
+
CONFLICT = "CONFLICT"
|
|
213
|
+
INSUFFICIENT_BALANCE = "INSUFFICIENT_BALANCE"
|
|
214
|
+
RATE_LIMITED = "RATE_LIMITED"
|
|
215
|
+
INTERNAL_ERROR = "INTERNAL_ERROR"
|
|
216
|
+
|
|
217
|
+
class ErrorResponse(BaseModel):
|
|
218
|
+
error: ErrorCode
|
|
219
|
+
message: str
|
|
220
|
+
details: list[dict] | None = None
|
|
221
|
+
request_id: str
|
|
222
|
+
|
|
223
|
+
class AppException(Exception):
|
|
224
|
+
def __init__(self, code: ErrorCode, message: str, status: int = 400, details=None):
|
|
225
|
+
self.code = code
|
|
226
|
+
self.message = message
|
|
227
|
+
self.status = status
|
|
228
|
+
self.details = details
|
|
229
|
+
|
|
230
|
+
@app.exception_handler(AppException)
|
|
231
|
+
async def app_exception_handler(request: Request, exc: AppException):
|
|
232
|
+
return JSONResponse(
|
|
233
|
+
status_code=exc.status,
|
|
234
|
+
content=ErrorResponse(
|
|
235
|
+
error=exc.code,
|
|
236
|
+
message=exc.message,
|
|
237
|
+
details=exc.details,
|
|
238
|
+
request_id=request.state.request_id,
|
|
239
|
+
).model_dump(),
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
@app.exception_handler(Exception)
|
|
243
|
+
async def generic_exception_handler(request: Request, exc: Exception):
|
|
244
|
+
logger.exception("Unhandled exception", request_id=request.state.request_id)
|
|
245
|
+
return JSONResponse(
|
|
246
|
+
status_code=500,
|
|
247
|
+
content=ErrorResponse(
|
|
248
|
+
error=ErrorCode.INTERNAL_ERROR,
|
|
249
|
+
message="An internal error occurred. Please try again later.",
|
|
250
|
+
request_id=request.state.request_id,
|
|
251
|
+
).model_dump(),
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
# 业务代码使用统一异常
|
|
255
|
+
@app.post("/orders", response_model=OrderResponse, status_code=201)
|
|
256
|
+
def create_order(data: CreateOrderRequest):
|
|
257
|
+
user = user_repo.get(data.user_id)
|
|
258
|
+
if not user:
|
|
259
|
+
raise AppException(
|
|
260
|
+
code=ErrorCode.NOT_FOUND,
|
|
261
|
+
message=f"User {data.user_id} not found",
|
|
262
|
+
status=404,
|
|
263
|
+
)
|
|
264
|
+
if user.balance < data.total:
|
|
265
|
+
raise AppException(
|
|
266
|
+
code=ErrorCode.INSUFFICIENT_BALANCE,
|
|
267
|
+
message="Insufficient balance to place this order",
|
|
268
|
+
status=422,
|
|
269
|
+
details=[{"field": "total", "required": str(data.total), "available": str(user.balance)}],
|
|
270
|
+
)
|
|
271
|
+
return order_service.create(user, data)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 检测方法
|
|
275
|
+
- 不同接口的错误响应 JSON 结构不一致。
|
|
276
|
+
- 存在 `except Exception: return str(e)` 模式。
|
|
277
|
+
- 错误响应中包含堆栈信息、文件路径、SQL 语句。
|
|
278
|
+
- 所有错误都返回 HTTP 500。
|
|
279
|
+
|
|
280
|
+
### 修复步骤
|
|
281
|
+
1. 定义统一的 `ErrorResponse` 模型和 `ErrorCode` 枚举。
|
|
282
|
+
2. 创建自定义异常基类 `AppException`,包含错误码、消息、HTTP 状态码。
|
|
283
|
+
3. 注册全局异常处理器,拦截所有异常并转为统一格式。
|
|
284
|
+
4. 在生产环境中隐藏内部错误详情,只返回 request_id 供排查。
|
|
285
|
+
5. 更新 API 文档,列出每个接口可能返回的错误码。
|
|
286
|
+
|
|
287
|
+
### Agent Checklist
|
|
288
|
+
- [ ] 所有接口使用统一的错误响应格式
|
|
289
|
+
- [ ] 错误码使用枚举,可机器解析
|
|
290
|
+
- [ ] 生产环境不泄露堆栈信息
|
|
291
|
+
- [ ] 每个错误响应包含 request_id
|
|
292
|
+
- [ ] API 文档列出每个接口的错误码
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
## 4. 过大响应(Oversized Response)
|
|
297
|
+
|
|
298
|
+
### 描述
|
|
299
|
+
API 返回了远超客户端需要的数据量,包括不必要的嵌套对象、大文本字段、二进制数据。导致带宽浪费、客户端解析慢、移动端体验差。
|
|
300
|
+
|
|
301
|
+
### 错误示例
|
|
302
|
+
```python
|
|
303
|
+
@app.get("/users/{user_id}")
|
|
304
|
+
def get_user(user_id: int):
|
|
305
|
+
user = user_repo.get_with_all_relations(user_id)
|
|
306
|
+
return {
|
|
307
|
+
"id": user.id,
|
|
308
|
+
"name": user.name,
|
|
309
|
+
"email": user.email,
|
|
310
|
+
"avatar_base64": user.avatar_base64, # 500KB 的 base64 图片
|
|
311
|
+
"orders": [ # 所有历史订单
|
|
312
|
+
{
|
|
313
|
+
"id": o.id,
|
|
314
|
+
"items": [ # 每个订单的所有商品
|
|
315
|
+
{
|
|
316
|
+
"product": { # 嵌套完整商品信息
|
|
317
|
+
"id": p.id,
|
|
318
|
+
"description": p.description, # 长文本
|
|
319
|
+
"images": p.images, # 图片列表
|
|
320
|
+
}
|
|
321
|
+
} for p in o.items
|
|
322
|
+
]
|
|
323
|
+
} for o in user.orders # 可能有几千个订单
|
|
324
|
+
],
|
|
325
|
+
"audit_log": user.audit_log, # 审计日志,不应暴露给客户端
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 正确示例
|
|
330
|
+
```python
|
|
331
|
+
# 基础响应 -- 只包含核心字段
|
|
332
|
+
class UserSummary(BaseModel):
|
|
333
|
+
id: int
|
|
334
|
+
name: str
|
|
335
|
+
email: str
|
|
336
|
+
avatar_url: str # URL 而非 base64
|
|
337
|
+
|
|
338
|
+
# 详情响应 -- 按需扩展
|
|
339
|
+
class UserDetail(UserSummary):
|
|
340
|
+
phone: str | None
|
|
341
|
+
created_at: datetime
|
|
342
|
+
order_count: int # 聚合数字而非完整列表
|
|
343
|
+
|
|
344
|
+
@app.get("/users/{user_id}", response_model=UserSummary)
|
|
345
|
+
def get_user(user_id: int):
|
|
346
|
+
return user_repo.get_summary(user_id)
|
|
347
|
+
|
|
348
|
+
@app.get("/users/{user_id}/detail", response_model=UserDetail)
|
|
349
|
+
def get_user_detail(user_id: int):
|
|
350
|
+
return user_repo.get_detail(user_id)
|
|
351
|
+
|
|
352
|
+
# 订单通过独立的分页接口获取
|
|
353
|
+
@app.get("/users/{user_id}/orders", response_model=PaginatedResponse[OrderSummary])
|
|
354
|
+
def get_user_orders(
|
|
355
|
+
user_id: int,
|
|
356
|
+
page: int = Query(1, ge=1),
|
|
357
|
+
page_size: int = Query(20, ge=1, le=100),
|
|
358
|
+
):
|
|
359
|
+
return order_repo.get_by_user(user_id, page=page, page_size=page_size)
|
|
360
|
+
|
|
361
|
+
# GraphQL 方案 -- 客户端自选字段
|
|
362
|
+
type_defs = """
|
|
363
|
+
type User {
|
|
364
|
+
id: ID!
|
|
365
|
+
name: String!
|
|
366
|
+
email: String!
|
|
367
|
+
avatarUrl: String
|
|
368
|
+
orders(first: Int = 10, after: String): OrderConnection
|
|
369
|
+
}
|
|
370
|
+
"""
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### 检测方法
|
|
374
|
+
- 响应 JSON 大小 > 100KB。
|
|
375
|
+
- 响应中包含 base64 编码的二进制数据。
|
|
376
|
+
- 响应中嵌套层级 > 3 层。
|
|
377
|
+
- 响应中包含客户端不需要的内部字段(audit_log、internal_id 等)。
|
|
378
|
+
|
|
379
|
+
### 修复步骤
|
|
380
|
+
1. 分析前端实际使用的字段,删除未使用的字段。
|
|
381
|
+
2. 将大对象(图片、文件)改为 URL 引用。
|
|
382
|
+
3. 将嵌套列表拆分为独立的分页接口。
|
|
383
|
+
4. 使用不同粒度的响应模型(Summary / Detail / Full)。
|
|
384
|
+
5. 考虑 GraphQL 或 JSON:API sparse fieldsets 让客户端自选字段。
|
|
385
|
+
|
|
386
|
+
### Agent Checklist
|
|
387
|
+
- [ ] 单个响应 JSON < 100KB
|
|
388
|
+
- [ ] 无 base64 编码的二进制数据(使用 URL)
|
|
389
|
+
- [ ] 嵌套层级 <= 3
|
|
390
|
+
- [ ] 列表数据通过独立分页接口获取
|
|
391
|
+
- [ ] 响应模型不包含内部字段
|
|
392
|
+
|
|
393
|
+
---
|
|
394
|
+
|
|
395
|
+
## 5. 无限流 / 缺乏限流(Missing Rate Limiting)
|
|
396
|
+
|
|
397
|
+
### 描述
|
|
398
|
+
API 没有请求频率限制,任何客户端可以无限制地发起请求。导致 DDoS 攻击、恶意爬虫、单个客户端耗尽系统资源影响其他用户。
|
|
399
|
+
|
|
400
|
+
### 错误示例
|
|
401
|
+
```python
|
|
402
|
+
# 无任何限流措施
|
|
403
|
+
@app.post("/api/send-sms")
|
|
404
|
+
def send_sms(phone: str, message: str):
|
|
405
|
+
# 恶意调用者可以无限发送短信,消耗短信费用
|
|
406
|
+
sms_service.send(phone, message)
|
|
407
|
+
return {"status": "sent"}
|
|
408
|
+
|
|
409
|
+
@app.get("/api/search")
|
|
410
|
+
def search(query: str):
|
|
411
|
+
# 无限制搜索请求,可能打垮搜索引擎
|
|
412
|
+
return search_engine.query(query)
|
|
413
|
+
|
|
414
|
+
# 登录接口无限制 -- 可暴力破解
|
|
415
|
+
@app.post("/api/login")
|
|
416
|
+
def login(username: str, password: str):
|
|
417
|
+
user = authenticate(username, password)
|
|
418
|
+
if not user:
|
|
419
|
+
return {"error": "Invalid credentials"}, 401
|
|
420
|
+
return {"token": create_token(user)}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### 正确示例
|
|
424
|
+
```python
|
|
425
|
+
from slowapi import Limiter
|
|
426
|
+
from slowapi.util import get_remote_address
|
|
427
|
+
import redis
|
|
428
|
+
|
|
429
|
+
limiter = Limiter(key_func=get_remote_address)
|
|
430
|
+
|
|
431
|
+
# 全局默认限流
|
|
432
|
+
@app.middleware("http")
|
|
433
|
+
async def rate_limit_middleware(request: Request, call_next):
|
|
434
|
+
# 全局:每分钟 60 次
|
|
435
|
+
pass
|
|
436
|
+
|
|
437
|
+
# 精细化限流
|
|
438
|
+
@app.post("/api/send-sms")
|
|
439
|
+
@limiter.limit("5/minute") # 短信接口:每分钟 5 次
|
|
440
|
+
def send_sms(request: Request, data: SmsRequest):
|
|
441
|
+
sms_service.send(data.phone, data.message)
|
|
442
|
+
return {"status": "sent"}
|
|
443
|
+
|
|
444
|
+
@app.post("/api/login")
|
|
445
|
+
@limiter.limit("10/minute") # 登录接口:每分钟 10 次
|
|
446
|
+
def login(request: Request, credentials: LoginRequest):
|
|
447
|
+
user = authenticate(credentials.username, credentials.password)
|
|
448
|
+
if not user:
|
|
449
|
+
# 记录失败次数,连续失败超过阈值锁定账号
|
|
450
|
+
failed_count = login_tracker.increment(credentials.username)
|
|
451
|
+
if failed_count >= MAX_LOGIN_ATTEMPTS:
|
|
452
|
+
login_tracker.lock(credentials.username, duration=LOCKOUT_MINUTES)
|
|
453
|
+
raise AppException(ErrorCode.UNAUTHORIZED, "Invalid credentials", status=401)
|
|
454
|
+
login_tracker.reset(credentials.username)
|
|
455
|
+
return {"token": create_token(user)}
|
|
456
|
+
|
|
457
|
+
# API Key 维度的限流
|
|
458
|
+
class ApiKeyRateLimiter:
|
|
459
|
+
def __init__(self, redis_client: redis.Redis):
|
|
460
|
+
self._redis = redis_client
|
|
461
|
+
|
|
462
|
+
def check(self, api_key: str, limit: int, window_seconds: int) -> bool:
|
|
463
|
+
key = f"rate_limit:{api_key}"
|
|
464
|
+
current = self._redis.incr(key)
|
|
465
|
+
if current == 1:
|
|
466
|
+
self._redis.expire(key, window_seconds)
|
|
467
|
+
return current <= limit
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
```
|
|
471
|
+
# 响应 Header 包含限流信息
|
|
472
|
+
HTTP/1.1 200 OK
|
|
473
|
+
X-RateLimit-Limit: 60
|
|
474
|
+
X-RateLimit-Remaining: 45
|
|
475
|
+
X-RateLimit-Reset: 1704067200
|
|
476
|
+
|
|
477
|
+
# 超限时返回 429
|
|
478
|
+
HTTP/1.1 429 Too Many Requests
|
|
479
|
+
Retry-After: 30
|
|
480
|
+
{
|
|
481
|
+
"error": "RATE_LIMITED",
|
|
482
|
+
"message": "Too many requests. Please retry after 30 seconds.",
|
|
483
|
+
"request_id": "req_abc123"
|
|
484
|
+
}
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### 检测方法
|
|
488
|
+
- API 代码中无限流 middleware 或装饰器。
|
|
489
|
+
- 响应 Header 中无 `X-RateLimit-*` 字段。
|
|
490
|
+
- 负载测试中无 429 响应。
|
|
491
|
+
- 短信、邮件、登录等敏感接口无独立的限流策略。
|
|
492
|
+
|
|
493
|
+
### 修复步骤
|
|
494
|
+
1. 引入限流中间件(`slowapi` / Nginx `limit_req` / API Gateway)。
|
|
495
|
+
2. 设置全局默认限流(如 60 次/分钟)。
|
|
496
|
+
3. 对敏感接口(登录、短信、支付)设置更严格的限流。
|
|
497
|
+
4. 在响应 Header 中返回限流信息。
|
|
498
|
+
5. 超限时返回 HTTP 429 + `Retry-After` header。
|
|
499
|
+
6. 使用 Redis 实现分布式限流(多实例部署场景)。
|
|
500
|
+
|
|
501
|
+
### Agent Checklist
|
|
502
|
+
- [ ] 所有公开 API 有全局限流
|
|
503
|
+
- [ ] 敏感接口(登录/短信/支付)有独立限流
|
|
504
|
+
- [ ] 响应包含 `X-RateLimit-*` Header
|
|
505
|
+
- [ ] 超限返回 HTTP 429 + `Retry-After`
|
|
506
|
+
- [ ] 登录接口有连续失败锁定机制
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
## 6. 无认证 / 认证不当(Missing or Broken Authentication)
|
|
511
|
+
|
|
512
|
+
### 描述
|
|
513
|
+
API 没有认证机制,或认证实现存在严重缺陷(Token 不过期、无刷新机制、敏感接口可匿名访问、JWT 密钥硬编码)。
|
|
514
|
+
|
|
515
|
+
### 错误示例
|
|
516
|
+
```python
|
|
517
|
+
# 无认证:任何人都能访问
|
|
518
|
+
@app.get("/api/users")
|
|
519
|
+
def list_users():
|
|
520
|
+
return user_repo.get_all()
|
|
521
|
+
|
|
522
|
+
@app.delete("/api/users/{user_id}")
|
|
523
|
+
def delete_user(user_id: int):
|
|
524
|
+
user_repo.delete(user_id)
|
|
525
|
+
return {"status": "deleted"}
|
|
526
|
+
|
|
527
|
+
# JWT 实现缺陷
|
|
528
|
+
SECRET_KEY = "super-secret-123" # 硬编码密钥
|
|
529
|
+
|
|
530
|
+
def create_token(user):
|
|
531
|
+
return jwt.encode(
|
|
532
|
+
{"user_id": user.id}, # 无过期时间
|
|
533
|
+
SECRET_KEY,
|
|
534
|
+
algorithm="HS256"
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
def verify_token(token):
|
|
538
|
+
try:
|
|
539
|
+
return jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
|
540
|
+
except:
|
|
541
|
+
return None # 吞掉所有异常
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### 正确示例
|
|
545
|
+
```python
|
|
546
|
+
import os
|
|
547
|
+
from datetime import datetime, timedelta, timezone
|
|
548
|
+
from fastapi import Depends, HTTPException, Security
|
|
549
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
550
|
+
|
|
551
|
+
# 密钥从环境变量读取
|
|
552
|
+
JWT_SECRET = os.environ["JWT_SECRET_KEY"]
|
|
553
|
+
JWT_ALGORITHM = "HS256"
|
|
554
|
+
ACCESS_TOKEN_EXPIRE_MINUTES = 15
|
|
555
|
+
REFRESH_TOKEN_EXPIRE_DAYS = 7
|
|
556
|
+
|
|
557
|
+
security = HTTPBearer()
|
|
558
|
+
|
|
559
|
+
def create_access_token(user_id: int, roles: list[str]) -> str:
|
|
560
|
+
now = datetime.now(timezone.utc)
|
|
561
|
+
payload = {
|
|
562
|
+
"sub": str(user_id),
|
|
563
|
+
"roles": roles,
|
|
564
|
+
"iat": now,
|
|
565
|
+
"exp": now + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
|
566
|
+
"jti": str(uuid4()), # 唯一 ID,支持 Token 吊销
|
|
567
|
+
}
|
|
568
|
+
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
|
569
|
+
|
|
570
|
+
def create_refresh_token(user_id: int) -> str:
|
|
571
|
+
now = datetime.now(timezone.utc)
|
|
572
|
+
payload = {
|
|
573
|
+
"sub": str(user_id),
|
|
574
|
+
"type": "refresh",
|
|
575
|
+
"exp": now + timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS),
|
|
576
|
+
"jti": str(uuid4()),
|
|
577
|
+
}
|
|
578
|
+
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
|
579
|
+
|
|
580
|
+
async def get_current_user(
|
|
581
|
+
credentials: HTTPAuthorizationCredentials = Security(security),
|
|
582
|
+
) -> User:
|
|
583
|
+
try:
|
|
584
|
+
payload = jwt.decode(
|
|
585
|
+
credentials.credentials, JWT_SECRET, algorithms=[JWT_ALGORITHM]
|
|
586
|
+
)
|
|
587
|
+
except jwt.ExpiredSignatureError:
|
|
588
|
+
raise HTTPException(status_code=401, detail="Token expired")
|
|
589
|
+
except jwt.InvalidTokenError:
|
|
590
|
+
raise HTTPException(status_code=401, detail="Invalid token")
|
|
591
|
+
|
|
592
|
+
# 检查 Token 是否被吊销
|
|
593
|
+
if token_blacklist.is_revoked(payload["jti"]):
|
|
594
|
+
raise HTTPException(status_code=401, detail="Token revoked")
|
|
595
|
+
|
|
596
|
+
user = user_repo.get(int(payload["sub"]))
|
|
597
|
+
if not user:
|
|
598
|
+
raise HTTPException(status_code=401, detail="User not found")
|
|
599
|
+
return user
|
|
600
|
+
|
|
601
|
+
def require_role(*roles: str):
|
|
602
|
+
async def role_checker(user: User = Depends(get_current_user)):
|
|
603
|
+
if not any(r in user.roles for r in roles):
|
|
604
|
+
raise HTTPException(status_code=403, detail="Insufficient permissions")
|
|
605
|
+
return user
|
|
606
|
+
return role_checker
|
|
607
|
+
|
|
608
|
+
# 接口使用认证和授权
|
|
609
|
+
@app.get("/api/users", dependencies=[Depends(require_role("admin"))])
|
|
610
|
+
def list_users(current_user: User = Depends(get_current_user)):
|
|
611
|
+
return user_repo.get_all()
|
|
612
|
+
|
|
613
|
+
@app.delete("/api/users/{user_id}", dependencies=[Depends(require_role("admin"))])
|
|
614
|
+
def delete_user(user_id: int, current_user: User = Depends(get_current_user)):
|
|
615
|
+
if user_id == current_user.id:
|
|
616
|
+
raise AppException(ErrorCode.CONFLICT, "Cannot delete yourself")
|
|
617
|
+
user_repo.soft_delete(user_id, operator=current_user.id)
|
|
618
|
+
return {"status": "deleted"}
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
### 检测方法
|
|
622
|
+
- 接口 handler 无认证依赖(`Depends(get_current_user)` 或中间件)。
|
|
623
|
+
- JWT 密钥硬编码在源码中。
|
|
624
|
+
- Token 无 `exp` 字段或过期时间 > 24 小时。
|
|
625
|
+
- 无 Token 吊销机制(黑名单)。
|
|
626
|
+
- 管理接口和普通接口使用相同的权限级别。
|
|
627
|
+
|
|
628
|
+
### 修复步骤
|
|
629
|
+
1. 建立全局认证中间件,默认所有接口需要认证(白名单制)。
|
|
630
|
+
2. JWT 密钥从环境变量读取,不同环境使用不同密钥。
|
|
631
|
+
3. Access Token 过期时间设为 15 分钟,Refresh Token 设为 7 天。
|
|
632
|
+
4. 实现 Token 吊销机制(Redis 黑名单或数据库记录)。
|
|
633
|
+
5. 使用 RBAC 对不同接口设置不同的角色要求。
|
|
634
|
+
6. 安全扫描工具(`bandit` / `semgrep`)检查硬编码密钥。
|
|
635
|
+
|
|
636
|
+
### Agent Checklist
|
|
637
|
+
- [ ] 所有非公开接口要求认证
|
|
638
|
+
- [ ] JWT 密钥从环境变量读取
|
|
639
|
+
- [ ] Access Token 过期时间 <= 30 分钟
|
|
640
|
+
- [ ] 有 Refresh Token 机制
|
|
641
|
+
- [ ] Token 支持吊销(黑名单)
|
|
642
|
+
- [ ] 管理接口有独立的角色校验
|
|
643
|
+
- [ ] 无硬编码的密钥或 Token
|
|
644
|
+
|
|
645
|
+
---
|
|
646
|
+
|
|
647
|
+
## 全局 Agent Checklist
|
|
648
|
+
|
|
649
|
+
| 检查项 | 阈值 | 工具 |
|
|
650
|
+
|--------|------|------|
|
|
651
|
+
| API 命名一致性 | 100% | `spectral` / OpenAPI lint |
|
|
652
|
+
| API 版本号 | 必须有 | URL / Header 检查 |
|
|
653
|
+
| 错误格式统一性 | 100% | API 测试 / Contract 测试 |
|
|
654
|
+
| 单响应大小 | < 100KB | APM / 网络监控 |
|
|
655
|
+
| 限流覆盖率 | 100% 公开接口 | 压力测试 / 429 检查 |
|
|
656
|
+
| 认证覆盖率 | 100% 非公开接口 | 安全扫描 / Pentest |
|
|
657
|
+
| 硬编码密钥 | 0 处 | `bandit` / `semgrep` |
|