Safety precautions
Version Control Security
Git, as a distributed version control system, has security that directly impacts the integrity of code repositories. Configuring the .gitignore
file prevents sensitive information from being committed:
// Incorrect example: Committing a configuration file
/* config.json */
{
"database": {
"host": "prod-db.example.com",
"password": "s3cr3tP@ss"
}
}
Such files must be added to the ignore list:
# .gitignore
config.json
*.env
node_modules/
Access Permission Management
Repository access control is a fundamental security measure:
- SSH Key Strength: Use at least 4096-bit RSA keys
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
Branch Protection Rules:
- Enforce code reviews (minimum 1 approval)
- Disable force pushes
- Require passing CI checks
-
Principle of Least Privilege: Grant developers only necessary permissions
Commit Standards and Auditing
Each commit should include traceable information:
git commit -m "fix(auth): patch CVE-2023-1234 in JWT validation
- Add token expiration check
- Verify signature algorithm
- Related to security ticket #SEC-5678"
Audit log inspection command:
git log --pretty=format:'%h %an %ad %s' --date=iso --grep='security'
Sensitive Data Handling
Committed sensitive information must be thoroughly removed:
- Use BFG tool to clean history:
java -jar bfg.jar --replace-text passwords.txt repo.git
- Key rotation process:
AWS_ACCESS_KEY=AKIA1234567890 # Must be revoked immediately
- Use git-secrets for preventive detection:
git secrets --install
git secrets --register-aws
Dependency Security
Third-party dependencies may introduce vulnerabilities:
// Example risky configuration in package.json
"dependencies": {
"legacy-package": "^1.0.0", // Known to have RCE vulnerability
"unmaintained-lib": "git+ssh://git@example.com/repo.git" // Uncontrolled source code
}
Security practices:
- Regularly run
npm audit
- Lock dependency versions
- Verify submodule sources
Continuous Integration Security
CI/CD pipelines require special protection:
# Insecure .gitlab-ci.yml example
deploy:
script:
- echo $SSH_PRIVATE_KEY > key.pem # Plaintext key storage
- scp -i key.pem ./dist user@server:/path
Improved solution:
- Use CI system key management
- Restrict build environment network egress
- Implement build artifact verification
Repository Maintenance Security
Regularly perform repository health checks:
# Detect dangling objects
git fsck --full
# Clean up large historical files
git filter-branch --tree-filter 'rm -f large_video.mp4' HEAD
Always create backups before operations:
git bundle create backup.bundle --all
Collaboration Process Security
Code reviews require attention to:
- Beware of malicious commits disguised as code:
# Seemingly harmless whitespace changes
- const isValid = (input) => {
+ const isValid = (input) => {
# Actually adds zero-width characters
- Verify GPG signatures:
git verify-commit HEAD
- Use
--verify-signatures
option when merging code
Emergency Response Measures
Steps to take when a security incident is discovered:
- Immediately revoke related credentials
- Create a security branch for fixes:
git checkout -b security-hotfix
git push origin security-hotfix --force
- Document the incident timeline:
| Timestamp | Operator | Action Description |
|-------------------|----------|---------------------------|
| 2023-08-20T14:30 | admin | Reset all developer API keys |
Automated Monitoring
Implement real-time security monitoring:
# Example hook script to detect sensitive information
import re
def pre_commit():
banned = [r'passw(or)?d', r'secret', r'api_?key']
changes = os.popen('git diff --cached').read()
for pattern in banned:
if re.search(pattern, changes, re.I):
print(f"⚠️ Detected sensitive term {pattern}")
sys.exit(1)
Physical Device Security
Key protections for development terminal devices:
- Full-disk encryption
- Separate sensitive information in Git configuration:
[credential]
helper = store --file ~/.secure/git-credentials
- Disable global configuration:
git config --global --unset credential.helper
History Cleanup
Thoroughly clean specific file history:
git filter-repo --path confidential.docx --invert-paths
After cleanup, all collaborators must be notified:
All members must:
1. Delete local copies
2. Execute the following commands:
```bash
git fetch --all --prune
git rebase origin/main
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn