mcpField
S
secure-node-typescript

secure-node-typescript

github.com/joacodセキュリティ356 回インストール3 週間前に更新
リポジトリ

secure-node-typescript について

Node.js と TypeScript アプリケーション開発向けのセキュリティスキルです。リスク領域別の指針を OWASP Top 10:2025 に対応付け、TypeScript のコンパイラー設定を監査するスクリプトも備えています。

主な機能

  1. 01段階別のセキュリティ指針
  2. 02OWASP Top 10:2025 との対応
  3. 03安全なコーディング例
  4. 04TypeScript 設定の監査
  5. 05再利用可能な設定テンプレート

活用シーン

  1. 01Node.js サービスに安全な既定値を適用
  2. 02認証と HTTP 保護策を確認
  3. 03TypeScript のコンパイラー設定を監査

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/joacod/skills --skill secure-node-typescript

インストール後、Claude Code で/secure-node-typescriptを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

assets/
eslint-security.config.js
tsconfig.secure.json
references/
authentication.md
dependencies.md
error-logging.md
filesystem-paths.md
http-security.md
input-validation.md
operational.md
runtime-safety.md
security-index.md
typescript-safety.md
scripts/
audit-tsconfig.py

詳細

セキュアな Node.js TypeScript

概要

一般的なサーバーサイドの脅威を無効化する、安全性をデフォルトとした Node.js と TypeScript のアプリケーションを作成します。このスキルでは、ドメイン別に整理したセキュリティガイドラインと、最も重要な制御策のインラインパターンを提供します。

すべてのガイドラインは、コンプライアンスの追跡と監査を目的として、OWASP Top 10:2025 のカテゴリーに対応付けられています。OWASP の対応付けの全容については、references/security-index.md を参照してください。

セキュリティティア

コードのコンテキストに応じてガイドラインを適用してください。

ティア適用する場面主な重点領域
Alwaysすべての Node.js/TS コード厳格な TypeScript、入力検証、ハードコードされたシークレットの禁止、安全なエラー処理
API/HTTPWeb エンドポイント、ミドルウェアヘッダー(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、パスワード、RBACreferences/authentication.md
ヘッダー、CORS、レート制限、CSPreferences/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

コメント