정적 분석(Static Analysis)은 소스 코드를 실행하지 않고 분석하여 버그, 보안 취약점, 코드 품질 문제를 발견하는 기법이다. 린팅(linting)과 SAST(Static Application Security Testing)를 포함한다.
정적 분석 종류
ESLint 설정 예제
json
// eslint.config.js (flat config)
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import security from 'eslint-plugin-security';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
{
plugins: { security },
rules: {
'no-eval': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'security/detect-sql-injection': 'error',
'complexity': ['warn', { max: 10 }],
}
}
);
Semgrep (SAST) 예제
yaml
# 커스텀 룰: 하드코딩된 패스워드 탐지
rules:
- id: hardcoded-password
patterns:
- pattern: |
const $VAR = "..."
...
password: $VAR
message: "하드코딩된 패스워드 발견: $VAR"
severity: ERROR
languages: [typescript]
bash
# 실행
semgrep --config=auto src/
semgrep --config=p/owasp-top-ten src/
SonarQube 메트릭
Quality Gate (예):
- Coverage ≥ 80%
- Duplicated Lines < 3%
- Maintainability Rating ≥ A
- Reliability Rating ≥ A
- Security Rating ≥ A
- Security Hotspots Reviewed = 100%
Cognitive Complexity: 코드 이해 어려움 측정
(순환 복잡도보다 직관적)
yaml
# GitHub Actions
- name: Lint
run: npx eslint . --max-warnings 0
- name: Type Check
run: npx tsc --noEmit
- name: SAST
run: semgrep --config=auto --error
- name: Dependency Audit
run: npm audit --audit-level=high
관련 문서