Skip to content

実用パターン

基本パターン — AIに送る前後に挟む

最もシンプルな使い方。既存のAI呼び出しコードに2行追加するだけです。

javascript
async function chat(userInput) {
  // ─── 追加: 送信前にマスク ────────────────────────
  const { masked } = fw.mask(userInput);
  // ─────────────────────────────────────────────────

  // 既存のAI呼び出しコード(変更不要)
  const response = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: masked }],
  });
  const aiText = response.choices[0].message.content;

  // ─── 追加: 返答前に復元 ────────────────────────
  return fw.restoreAll(aiText);
  // ─────────────────────────────────────────────────
}

チャット履歴ごとマスクするパターン

javascript
// OpenAI / Claude のメッセージ形式をそのまま渡せる
const history = [
  { role: "user",      content: "田中さんのメールは tanaka@example.com です" },
  { role: "assistant", content: "承知しました。田中さんに連絡しますね。" },
  { role: "user",      content: "電話番号は 090-1234-5678 です" },
];

const { messages: maskedHistory } = fw.maskMessages(history);

// maskedHistory をそのまま AI に渡す
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: maskedHistory,
});

二重防御パターン(本番環境推奨)

インジェクション検知 → ブロック → マスク の完全防御パターンです。

javascript
async function secureChat(userInput) {
  // ── 第1防御: インジェクション攻撃をブロック ──────────────
  const threats = fw.detectInjection(userInput);
  if (threats.some(t => t.level === "critical")) {
    return { ok: false, error: "Security: Suspicious input detected" };
  }

  // ── 第2防御: PIIをマスクしてからAIに送る ───────────────
  const { masked, detections } = fw.mask(userInput);
  const aiResponse = await callYourAI(masked);

  return {
    ok:    true,
    answer: fw.restoreAll(aiResponse),
    stats:  { pii_protected: detections.length },
  };
}
ユーザー入力


detectInjection()  ──攻撃検知──▶  403 ブロック(AIには届かない)

  安全


fw.mask()          ──PII除去──▶  [SECURED:xxx] に置換


AI(Claude/GPT)   ──個人情報ゼロのテキスト──▶  回答生成


fw.restoreAll()    ──トークン復元──▶  元の値に戻す


ユーザーへ返却

対応ファイル形式

形式拡張子対応備考
PDF.pdfテキスト抽出可能なPDFのみ。スキャン(画像)PDFは非対応
Word.docx / .docMicrosoft Word文書
Excel.xlsx全シートのセル値をスキャン
PowerPoint.pptx全スライドのテキストをスキャン
HTML.htmlタグを除去したテキストをスキャン
Markdown.md議事録・READMEなどに対応
テキスト.txtプレーンテキスト
PDF(スキャン).pdf(画像)OCR非対応。テキスト抽出できない画像PDFはエラーを表示

ファイルアップロード時のスキャンパターン

javascript
app.post("/upload", upload.single("file"), async (req, res) => {
  const extractedText = await extractTextFromFile(req.file);

  const { masked, detections } = fw.mask(extractedText);

  res.json({
    original_length: extractedText.length,
    masked_text:     masked,
    pii_found:       detections.length,
    pii_types:       [...new Set(detections.map(d => d.type))],
  });
});

Privacy by Design.