Claude Code की 7 सुरक्षा विफलताएं: कारण, रिकवरी और बचाव
Claude Code में .env leak, production DB नुकसान, CI cost और prompt injection से बचने की गाइड।
Claude Code सिर्फ कोड सुझाने वाला chat window नहीं है। यह files edit कर सकता है, tests चला सकता है, Git status पढ़ सकता है और deployment commands भी propose कर सकता है। यही ताकत उपयोगी है, लेकिन अगर developer बिना पढ़े approvals देता जाए, तो खतरनाक action हाथ से type करने से भी जल्दी execute हो सकता है।
यह guide Claude Code की आम security गलतियों को practical guardrails में बदलती है। Beginner-friendly तरीके से कहें तो permission वह सीमा है कि agent क्या कर सकता है, hook वह checkpost है जो tool चलने से पहले या बाद में चलता है, और sandbox अलग working room है जहां गलती का असर कम रहता है। नीचे settings.json, Node.js secret scanner और GitHub Actions workflow दिए गए हैं।
flowchart TD
Request["User request"] --> Plan["Claude Code plan"]
Plan --> Permission["Permission rules"]
Permission --> Hook["PreToolUse hook"]
Hook --> Execute["Tool execution"]
Execute --> Audit["Log, review, recovery"]
Hook -->|block risky command| Stop["Stop before damage"]
Failure map
| Failure case | कब होता है | नुकसान | पहले लगाने वाला guardrail |
|---|---|---|---|
.env commit करना | ”CI के लिए यह file भी add कर दो” | API key leak और extra billing | .gitignore plus staged-file scan |
| Production data delete | Migration गलत URL पर चलती है | Data loss और restore work | DB command से पहले environment check |
git push --force | Conflict जल्दी solve करना | Team commits overwrite | ask rules और branch protection |
| CI में AI review loop | हर PR broad automation चलाता है | Actions और API cost बढ़ना | max-turns, timeout, least privilege |
| Untrusted logs paste करना | Logs में hidden instructions हैं | Prompt injection | Risky web और shell fetch deny |
| MCP access ज्यादा broad | External server पर जल्दी trust | Local data over-read | सिर्फ trusted MCP allow |
| Approval fatigue | User prompts बिना पढ़े approve करता है | Dangerous commands pass | Hooks जो default block करें |
इसका मतलब यह नहीं कि Claude Code default रूप से unsafe है। Official Security docs read-oriented permissions, approval prompts, sandboxing और user responsibility समझाते हैं। Real risk तब आता है जब team busy होती है और इंसान checks skip करने लगते हैं। इसलिए setup ऐसा होना चाहिए जिसमें safe path आसान और risky path स्पष्ट रूप से रुका हुआ हो।
Use case 1: personal project में API key leak रोकना
सबसे आम beginner mistake है .env को safe vault मान लेना। यह vault नहीं, local text file है। यह तभी सुरक्षित है जब Git, logs, screenshots और prompts में न जाए। Stripe, SendGrid, Anthropic या GitHub token public repository में दिखे, तो उसे compromised मानना चाहिए।
Repository में real value नहीं, सिर्फ configuration shape commit करें।
# .gitignore
.env
.env.*
!.env.example
secrets/
*.pem
*.key
*service-account*.json
credentials.json
# .env.example
ANTHROPIC_API_KEY=replace_me
DATABASE_URL=postgres://app_user:password@localhost:5432/app_dev
STRIPE_SECRET_KEY=sk_test_replace_me
अब Claude Code permissions tight करें। deny block करता है, ask हर बार confirmation मांगता है, और allow safe repeat commands को auto-approve करता है। Official Settings और Configure permissions pages बताते हैं कि ये rules कैसे evaluate होते हैं।
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(npm run lint)",
"Bash(npm run test *)",
"Bash(git status)",
"Bash(git diff *)"
],
"ask": [
"Bash(git push *)",
"Bash(npm run deploy *)",
"Write(./migrations/**)"
],
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Bash(rm -rf *)",
"Bash(curl *)",
"Bash(wget *)",
"WebFetch"
]
}
}
यह configuration Claude Code को बंद नहीं करती। यह low-risk commands को smooth बनाती है और destructive actions को visible बनाती है। अगर project को web access चाहिए, तो शुरुआत में narrow rule रखें: allowed domain, purpose और human review तय करें, फिर जरूरत के हिसाब से ढील दें।
Use case 2: team PR review को भरोसेमंद रखना
Teams में dangerous failure अक्सर process का होता है। लोग मानने लगते हैं कि “AI ने review कर दिया” इसलिए permission files, workflow changes और credential handling अलग से देखने की जरूरत नहीं। Security issue अक्सर यहीं से निकल जाता है।
यह scanner external dependency के बिना चलता है। Default में staged files scan करता है; CI में --all से पूरा repository scan कर सकता है।
// scripts/claude-security-check.mjs
import { execFileSync } from "node:child_process";
import fs from "node:fs";
const args = process.argv.slice(2);
const scanAll = args.includes("--all");
const explicitFiles = args.filter((arg) => arg !== "--all");
function runGit(args) {
return execFileSync("git", args, { encoding: "utf8", maxBuffer: 10 * 1024 * 1024 });
}
function filesToScan() {
if (explicitFiles.length > 0) return explicitFiles;
if (scanAll) return runGit(["ls-files"]).split(/\r?\n/).filter(Boolean);
return runGit(["diff", "--cached", "--name-only"]).split(/\r?\n/).filter(Boolean);
}
function readTrackedOrWorkingTree(file) {
if (scanAll || explicitFiles.length > 0) return fs.readFileSync(file, "utf8");
return runGit(["show", `:${file}`]);
}
const forbiddenPath = [
/^\.env$/,
/^\.env\./,
/(^|\/)secrets\//,
/(^|\/).*service-account.*\.json$/i,
/(^|\/)credentials\.json$/i,
/\.(pem|key)$/i
];
const secretPattern =
/(sk-ant-[A-Za-z0-9_-]{20,}|sk_live_[A-Za-z0-9_-]{20,}|AKIA[0-9A-Z]{16}|-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----)/;
let failed = false;
for (const file of filesToScan()) {
if (forbiddenPath.some((pattern) => pattern.test(file))) {
console.error(`[blocked] forbidden secret path: ${file}`);
failed = true;
continue;
}
try {
const text = readTrackedOrWorkingTree(file);
if (secretPattern.test(text)) {
console.error(`[blocked] secret-like value found in: ${file}`);
failed = true;
}
} catch {
// Ignore deleted or binary files.
}
}
if (failed) process.exit(1);
console.log("security check passed");
{
"scripts": {
"security:staged": "node scripts/claude-security-check.mjs",
"security:all": "node scripts/claude-security-check.mjs --all"
}
}
Common pitfall है script बनाकर रुक जाना। npm run security:staged को commit routine, PR template या CI में रखें। Claude Code से “security check याद रखना” कहना उतना मजबूत नहीं जितना automatic failing command।
Use case 3: production और CI budget बचाना
Production incidents अक्सर environment confusion से होते हैं। Problem सिर्फ DROP TABLE नहीं है; असली problem production DATABASE_URL पर command चलाना है जबकि developer उसे dev DB समझ रहा था। Claude Code से old data हटवाने से पहले database label, cloud project, branch और backup time साफ text में दिखाएं।
GitHub Actions में Claude Code इस्तेमाल करते समय official GitHub Actions docs API keys को Secrets में रखने और permissions सीमित रखने की सलाह देते हैं। नीचे वाला workflow सिर्फ PR diff review करता है और repository write access नहीं देता।
name: Claude Code guarded review
"on":
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
claude-security-review:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Run repository secret scan
run: node scripts/claude-security-check.mjs --all
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: "${{ secrets.ANTHROPIC_API_KEY }}"
prompt: >
Review only the pull request diff for secret handling, auth checks,
destructive commands, and permission changes. Do not modify files.
claude_args: |
--max-turns 3
--disallowedTools "Bash(git push *)" "Bash(npm run deploy *)" "Bash(rm -rf *)"
permissions: contents: read जरूरी है क्योंकि review-only job को write access नहीं चाहिए। timeout-minutes और --max-turns भी security controls हैं: uncontrolled automation cost और availability incident बन सकती है।
अगर incident हो चुका है तो recovery order
अगर API key leak हो गई है, तो Git history cleanup से शुरुआत न करें। पहले provider console में key revoke या rotate करें। Clean repository बेकार है अगर value पहले ही copy हो चुकी है।
- Exposed key revoke या rotate करें।
- Exposure window के usage logs और billing देखें।
- GitHub Secrets, deployment variables और CI values बदलें।
- Git history से value हटाएं और जरूरत हो तो cache cleanup request करें।
- उसी remediation PR में
.gitignore, permission settings और scanner जोड़ें।
अगर production data damage हुआ है, तो fix चलाने से पहले writes pause करें या app को read-only करें। Backup timestamp, restore target और acceptable data loss पहले तय करें। Recovery commands एक-एक करके human confirmation और logs के साथ चलाएं।
Official docs, internal links और अगला कदम
Official Security, Configure permissions, Settings, Hooks और GitHub Actions pages को source of truth मानें। Hooks खास तौर पर उपयोगी हैं क्योंकि वे tool execution से पहले custom checks चला सकते हैं।
Site पर आगे पढ़ने के लिए /hi/blog/claude-code-security-best-practices/, /hi/blog/claude-code-permissions-guide/ और /hi/blog/claude-code-secrets-management/ देखें।
Ready templates चाहिए तो /products/ के setup guides और checklists देखें। Team rollout, CI policy और review workflow बनाना हो तो /training/ में consultation और training options उपयोगी हैं।
Masa के local test repository में इस setup ने accidental .env stage, fake sk_live_ string और overly permissive GitHub Actions review job को publish से पहले block किया। Regex scanner हर secret format नहीं पकड़ सकता, इसलिए durable answer layered है: provider-side rotation, least privilege, automatic checks और Claude Code actions approve करने से पहले human review।
मुफ़्त PDF: Claude Code cheatsheet
Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.
हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.
लेखक के बारे में
Masa
Claude Code workflow और team adoption पर काम करने वाला engineer.
संबंधित लेख
Claude Code permission safety ladder: access धीरे-धीरे बढ़ाएं
read-only से limited edits, proof commands और deploy checks तक permission बढ़ाने की सुरक्षित ladder.
Claude Code Small PR Proof Pack: छोटे PR को review-ready बनाना
Claude Code PR के लिए diff, checks, public URL, CTA path और rollback वाला practical proof pack.
Claude Code Review Gate Before Commit: diff, test, public URL और CTA जांच
Claude Code से commit से पहले review gate बनाएं: diff, build, public URL, Gumroad, consultation, tests और unrelated files।