Skip to Content
Web アプリケーションセキュリティ
book

Web アプリケーションセキュリティ

by Andrew Hoffman
May 2025
Beginner to intermediate
330 pages
4h 34m
Japanese
O'Reilly Media, Inc.
Content preview from Web アプリケーションセキュリティ

第22章. XSS攻撃からの防御

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

パートIIでは、 ユーザ・デバイス上でJavaScriptコードを実行するブラウザの機能を利用したXSS攻撃について深く考察した。XSSの脆弱性は、スクリプト実行の脆弱性が潜在的な被害の幅を持つように、広範囲に広がっており、大きな被害をもたらす可能性がある。

幸いなことに、XSSはウェブに頻繁に現れるが、セキュアコーディングのベストプラクティスとXSS特有の緩和テクニックによって、完全に緩和したり防いだりすることは非常に簡単だ。この章では、あなたのコードベースをXSSから守るためのすべてを紹介する。

アンチXSSコーディングのベストプラクティス

XSS脆弱性に遭遇する確率を劇的に軽減するために、開発チームに実装できる主要な規則がひとつある:「文字列を除き、ユーザから与えられたデータをDOMに渡さないこと」だ。

多くのアプリケーションは、ユーザからDOMへのデータ転送を組み込む機能を持っているため、このような規則はすべてのアプリケーションに適用できるわけではない。この場合、この規則をより具体的にすることができる。"ユーザから提供されたデータがサニタイズされていない状態でDOMに渡されることを決して許してはならない"。

ユーザが提供するデータをDOMに入力できるようにすることは、最初の選択肢ではなく、予備的な、最後の選択肢であるべきだ。そのような関数は、誤ってXSS脆弱性につながるので、他のオプションが利用可能な場合は、それを最初に選択すべきである。

ユーザから提供されたデータをDOMに渡さなければならない場合、可能であれば文字列として渡すべきである。つまり、HTML/DOMが要求されず、ユーザが提供したデータがテキストとして表示されるためにDOMに渡される場合は、ユーザが提供したデータがDOMではなくテキストとして解釈されるようにしなければならない(図22-1参照)。

text-vs-dom
図22-1. ほとんどのXSSは(すべてではないが)、ユーザが提供したテキストが不適切にDOMに注入された結果として発生する。

クライアント、サーバの両方で、さまざまな方法でこれらのチェックを行うことができる。

まず、文字列の検出はJavaScriptではとても簡単だ:

const isString = function(x) {
  if (typeof x === 'string' || x instanceof String) {
    return true;
  }
  return false;
};

残念ながら、このチェックは数値のチェックでは失敗する。数値はDOMへのインジェクションでも安全なので、このエッジケースを扱うのは厄介だ。

文字列と数値を「文字列のような」オブジェクトに分類することができる。文字列のような」オブジェクトは、JSON.parse() の比較的知られていない副作用を使って評価することができる:

const isStringLike = function(x) {
  try {
     return JSON.stringify(JSON.parse(x)) === x;
  } catch (e) {
    console ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

詳解 システム・パフォーマンス 第2版

詳解 システム・パフォーマンス 第2版

Brendan Gregg, 西脇 靖紘, 長尾 高弘
Linuxカーネルプログラミング 第2版

Linuxカーネルプログラミング 第2版

Kaiwan N. Billimoria, 武内 覚, 大岩 尚宏

Publisher Resources

ISBN: 9798341651111