阿里云主机折上折
  • 微信号
Current Site:Index > Safety precautions

Safety precautions

Author:Chuan Chen 阅读数:19955人阅读 分类: 开发工具

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:

  1. SSH Key Strength: Use at least 4096-bit RSA keys
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. Branch Protection Rules:

    • Enforce code reviews (minimum 1 approval)
    • Disable force pushes
    • Require passing CI checks
  2. 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:

  1. Use BFG tool to clean history:
java -jar bfg.jar --replace-text passwords.txt repo.git
  1. Key rotation process:
AWS_ACCESS_KEY=AKIA1234567890  # Must be revoked immediately
  1. 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:

  1. Use CI system key management
  2. Restrict build environment network egress
  3. 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:

  1. Beware of malicious commits disguised as code:
# Seemingly harmless whitespace changes
- const isValid = (input) => {
+ const isValid = (input) => { 
  # Actually adds zero-width characters
  1. Verify GPG signatures:
git verify-commit HEAD
  1. Use --verify-signatures option when merging code

Emergency Response Measures

Steps to take when a security incident is discovered:

  1. Immediately revoke related credentials
  2. Create a security branch for fixes:
git checkout -b security-hotfix
git push origin security-hotfix --force
  1. 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:

  1. Full-disk encryption
  2. Separate sensitive information in Git configuration:
[credential]
    helper = store --file ~/.secure/git-credentials
  1. 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

上一篇:性能优化技巧

下一篇:备份策略

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.