LoginSignup
63

More than 5 years have passed since last update.

posted at

updated at

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

必要になったので。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 の サニタイジング

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
What you can do with signing up
63