| 
 | 1 | +<!DOCTYPE html>  | 
 | 2 | +<html lang="ja">  | 
 | 3 | +<head>  | 
 | 4 | +<meta http-equiv="X-UA-Compatible" content="IE=edge">  | 
 | 5 | +<meta charset="utf-8">  | 
 | 6 | +<meta name="viewport" content="width=device-width, initial-scale=1">  | 
 | 7 | +<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css">  | 
 | 8 | +<link rel="stylesheet" href="checker.css">  | 
 | 9 | +<script src="checker.js"></script>  | 
 | 10 | +<script src="format-strings.js"></script>  | 
 | 11 | +<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">  | 
 | 12 | + | 
 | 13 | +</head>  | 
 | 14 | +<body>  | 
 | 15 | +<!-- For GitHub Pages formatting: -->  | 
 | 16 | +<div class="container-lg px-3 my-5 markdown-body">  | 
 | 17 | +<h1>ラボ演習 Format Strings and Templates</h1>  | 
 | 18 | +<p>  | 
 | 19 | +これはセキュアなソフトウェア開発に関するラボ演習です。  | 
 | 20 | +ラボの詳細については、<a href="ja_introduction.html" target="_blank">概要</a>をご覧ください。  | 
 | 21 | + | 
 | 22 | +<p>  | 
 | 23 | +<h2>タスク</h2>  | 
 | 24 | +<p>  | 
 | 25 | +<b>Python の文字列フォーマッティングに内在する脆弱性の排除を実践しましょう。</b>  | 
 | 26 | + | 
 | 27 | +<p>  | 
 | 28 | +<h2>背景</h2>  | 
 | 29 | +<p>  | 
 | 30 | +この演習では、ユーザーが  | 
 | 31 | +<a href="https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method"><tt>  | 
 | 32 | +format string</tt></a>  | 
 | 33 | +を制御できないように、我々の文字列フォーマッティングを調整します。  | 
 | 34 | + | 
 | 35 | +<p>もしユーザーが Python の <tt>format string</tt>を制御できてしまうと、そのユーザーは本来アクセスすべきではない値にアクセスできてしまいます。特に、もしそういった変数の値がユーザーに出力として返された場合は、開発者が意図した範囲を超える情報漏洩を引き起こします。  | 
 | 36 | + | 
 | 37 | + | 
 | 38 | +<p>  | 
 | 39 | +<h2>タスクの詳細</h2>  | 
 | 40 | +<p>  | 
 | 41 | +文字列フォーマッティングがプログラム上の任意の値を出力しないように以下のコードを修正してください。  | 
 | 42 | + | 
 | 43 | +<p>  | 
 | 44 | +サーバサイドプログラムは Python で書かれており、 ここでは <tt>user_format</tt> として <tt>format string</tt> を指定することによってイベントの出力フォーマットをユーザが制御できるようになっています。開発者はおそらく、ユーザが <tt>'{event.level}'</tt> のようなフォーマット文字列を指定して、何が・どこに表示されるか制御することを期待したのでしょう。  | 
 | 45 | + | 
 | 46 | +<p>  | 
 | 47 | +しかし、多くのプログラミング言語において、信頼できないユーザに文字列フォーマットの制御を許すことは脆弱性につながります。フォーマット文字列は小さなプログラミング言語であり、信頼できないユーザからのコードを実行することは危険です。この Python のケースでは、攻撃者は <tt>'{event.__init__.__globals__[CONFIG][SECRET_KEY]}'</tt> といったフォーマット文字列を忍ばせて、パスワードやシークレットキーといった秘密情報を暴くことができるかもしれません。  | 
 | 48 | + | 
 | 49 | +<p>  | 
 | 50 | +このケースでは他の多くの場合と同じく、信頼できないユーザにフォーマット文字列を制御させる必要性は全くありません。可能な個所には全て、潜在的な攻撃者が制御できないように定数のフォーマットを用いるべきです。この演習ではユーザにフォーマット文字列を制御させる代わりに、<tt>'{event.level},{event.message}'</tt> といった固定値をフォーマットとして使用します。必要のないフォーマットのパラメータを全て削除することも忘れないでください。  | 
 | 51 | + | 
 | 52 | +<p>  | 
 | 53 | +必要に応じて、「ヒント」ボタンと「諦める」ボタンを使用してください。  | 
 | 54 | + | 
 | 55 | +<p>  | 
 | 56 | +<h2>演習<span id="grade"></span>)</h2>  | 
 | 57 | +<p>  | 
 | 58 | +<form id="lab">  | 
 | 59 | +<pre><code><textarea id="attempt0" rows="3" cols="60" spellcheck="false"  | 
 | 60 | +>def format_event(user_format, new_event):  | 
 | 61 | +   return user_format.format(event=new_event)</textarea  | 
 | 62 | +></code></pre>  | 
 | 63 | +<button type="button" class="hintButton">ヒント</button>  | 
 | 64 | +<button type="button" class="resetButton">リセット</button>  | 
 | 65 | +<button type="button" class="giveUpButton">諦める</button>  | 
 | 66 | +<br><br>  | 
 | 67 | +<p>  | 
 | 68 | +<i>このラボは<a href="https://access.redhat.com">Red Hat</a>の Jason Shepherd によって開発されました。</i>  | 
 | 69 | +Armin Ronacher による記事  | 
 | 70 | +<a href="https://lucumr.pocoo.org/2016/12/29/careful-with-str-format/">Be Careful with Python's New-Style String Format</a>  | 
 | 71 | +のサンプルコードを修正したバージョンに基づいており、David A. Wheeler が修正を加えています。  | 
 | 72 | +<br><br>  | 
 | 73 | +<p id="correctStamp" class="small">  | 
 | 74 | +<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly>  | 
 | 75 | +</textarea>  | 
 | 76 | +</form>  | 
 | 77 | +</div><!-- End GitHub pages formatting -->  | 
 | 78 | +</body>  | 
 | 79 | +</html>  | 
0 commit comments