LoginSignup
64
65

More than 5 years have passed since last update.

JavaScriptで最低限のサニタイズをする

Last updated at Posted at 2014-08-21

必要になったので。SECのセキュアプログラミング講座を根拠に、「<」「>」「&」「'」「"」をサニタイズ。

sanitaize.encode(文字列) でサニタイジング、 sanitaize.decode(文字列) で元に戻す。

sanitaize = {
  encode : function (str) {
    return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
  },

  decode : function (str) {
    return str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, '\'').replace(/&amp;/g, '&');
  }
};

参考URL

IPA ISEC セキュア・プログラミング講座:Webアプリケーション編 第8章 マッシュアップ:クライアントサイドマッシュアップ: #3 クライアント側コードに起因するスクリプト注入対策

galife: JavaScript の サニタイジング

64
65
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
65