secure-node-typescript について
Node.js と TypeScript アプリケーション開発向けのセキュリティスキルです。リスク領域別の指針を OWASP Top 10:2025 に対応付け、TypeScript のコンパイラー設定を監査するスクリプトも備えています。
主な機能
- 01段階別のセキュリティ指針
- 02OWASP Top 10:2025 との対応
- 03安全なコーディング例
- 04TypeScript 設定の監査
- 05再利用可能な設定テンプレート
活用シーン
- 01Node.js サービスに安全な既定値を適用
- 02認証と HTTP 保護策を確認
- 03TypeScript のコンパイラー設定を監査
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ joacod/ skills --skill secure-node-typescriptインストール後、Claude Code で/secure-node-typescriptを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
assets/eslint-security.config.jstsconfig.secure.jsonreferences/authentication.mddependencies.mderror-logging.mdfilesystem-paths.mdhttp-security.mdinput-validation.mdoperational.mdruntime-safety.mdsecurity-index.mdtypescript-safety.mdscripts/audit-tsconfig.py詳細
セキュアな Node.js TypeScript
概要
一般的なサーバーサイドの脅威を無効化する、安全性をデフォルトとした Node.js と TypeScript のアプリケーションを作成します。このスキルでは、ドメイン別に整理したセキュリティガイドラインと、最も重要な制御策のインラインパターンを提供します。
すべてのガイドラインは、コンプライアンスの追跡と監査を目的として、OWASP Top 10:2025 のカテゴリーに対応付けられています。OWASP の対応付けの全容については、references/security-index.md を参照してください。
セキュリティティア
コードのコンテキストに応じてガイドラインを適用してください。
| ティア | 適用する場面 | 主な重点領域 |
|---|---|---|
| Always | すべての Node.js/TS コード | 厳格な TypeScript、入力検証、ハードコードされたシークレットの禁止、安全なエラー処理 |
| API/HTTP | Web エンドポイント、ミドルウェア | ヘッダー(helmet)、レート制限、CORS、ボディサイズ制限、Content-Type の検証 |
| Auth | 認証機能 | パスワードのハッシュ化(argon2)、JWT の検証、安全な Cookie、RBAC |
| Data | 外部データの処理 | SQL インジェクション、XSS のサニタイズ、プロトタイプ汚染、スキーマ検証 |
| Runtime | 動的コード、プロセス | eval の禁止、安全な child_process、パストラバーサルの防止 |
すぐに使えるパターン
最も重要な10のセキュリティ制御策です。デフォルトで適用してください。
1. Strict TypeScript を有効にする
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUncheckedIndexedAccess": true
}
}
2. Zod ですべての入力を検証する
// DO: Schema validation at entry points
import { z } from 'zod'
const UserSchema = z.object({
email: z.string().email(),
age: z.number().int().min(0).max(150),
})
// In route handler
const result = UserSchema.safeParse(req.body)
if (!result.success) {
return res.status(400).json({ error: 'Invalid input' })
}
const user = result.data // Type-safe validated data
3. パラメーター化クエリを使用する
// DON'T: String concatenation (SQL injection)
const query = `SELECT * FROM users WHERE id = ${userId}`
// DO: Parameterized queries
const result = await db.query('SELECT * FROM users WHERE id = $1', [userId])
4. Argon2 でパスワードをハッシュ化する
import argon2 from 'argon2'
// Hash password
const hash = await argon2.hash(password, { type: argon2.argon2id })
// Verify password
const valid = await argon2.verify(hash, password)
5. Helmet でセキュリティヘッダーを設定する
import helmet from 'helmet'
import express from 'express'
const app = express()
app.use(helmet()) // Sets HSTS, CSP, X-Frame-Options, etc.
app.disable('x-powered-by')
6. リクエストボディのサイズを制限する
// DO: Enforce strict body limits
app.use(express.json({ limit: '1kb' })) // Adjust based on expected payload
// DON'T: Unlimited body parsing (DoS risk)
app.use(express.json())
7. ファイルパスをサニタイズする
import path from 'node:path'
// DO: Resolve and validate paths
const ALLOWED_DIR = '/app/uploads'
const safePath = path.resolve(ALLOWED_DIR, userInput)
if (!safePath.startsWith(ALLOWED_DIR)) {
throw new Error('Path traversal attempt blocked')
}
8. スタックトレースを決して公開しない
// DO: Generic error response to clients
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err) // Log full error internally
res.status(500).json({ error: 'Internal server error' }) // Generic to client
})
// DON'T: Expose error details
res.status(500).json({ error: err.message, stack: err.stack })
9. シークレットには環境変数を使用する
// DO: Load secrets from environment
import 'dotenv/config'
const dbPassword = process.env.DB_PASSWORD
if (!dbPassword) throw new Error('DB_PASSWORD required')
// DON'T: Hardcoded secrets
const dbPassword = 'secret123' // Never do this
10. node: プロトコルを使ってインポートする
// DO: Explicit Node.js built-in imports (prevents typosquatting)
import { createServer } from 'node:http'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
// DON'T: Implicit imports
import { createServer } from 'http' // Could resolve to malicious package
リファレンスの読み込みガイド
作業内容に応じてリファレンスファイルを読み込んでください。
| 作業 | 読み込むリファレンス |
|---|---|
| プロジェクトのセットアップ、tsconfig、型安全性 | references/typescript-safety.md |
| フォーム検証、ユーザー入力、API パラメーター | references/input-validation.md |
| ログイン、セッション、JWT、パスワード、RBAC | references/authentication.md |
| ヘッダー、CORS、レート制限、CSP | references/http-security.md |
| eval、child_process、プロトタイプ汚染 | references/runtime-safety.md |
| ファイルアップロード、パス処理、正規表現 | references/filesystem-paths.md |
| npm audit、ロックファイル、サプライチェーン | references/dependencies.md |
| エラー処理、ロギング、モニタリング | references/error-logging.md |
| リンター、CI/CD、脅威モデリング | references/operational.md |
| ガイドライン全体の検索 | references/security-index.md |
監査スクリプト
プロジェクトの tsconfig.json でセキュリティに関連する設定を検証します。
python3 scripts/audit-tsconfig.py /path/to/project
このスクリプトでは、以下を確認します。
strict: trueが有効であることnoImplicitAnyが有効であることstrictNullChecksが有効であることnoUncheckedIndexedAccessが有効であること- その他のセキュリティ関連のコンパイラーオプション
アセット
assets/ で利用できる設定テンプレート:
tsconfig.secure.json- Strict TypeScript 設定テンプレートeslint-security.config.js- ESLint セキュリティプラグインの設定
これらをコピーしてプロジェクトに合わせて調整し、出発点として使用してください。
Resources
- Security index:
references/security-index.md - TypeScript safety:
references/typescript-safety.md - Input validation:
references/input-validation.md - Authentication:
references/authentication.md - HTTP security:
references/http-security.md - Runtime safety:
references/runtime-safety.md - Filesystem paths:
references/filesystem-paths.md - Dependencies:
references/dependencies.md - Error logging:
references/error-logging.md - Operational:
references/operational.md
コメント
Markdown 対応