|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="ja"> |
| 3 | +<html> |
| 4 | +<head> |
| 5 | +<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 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="regex1.js"></script> |
| 11 | +<link rel="license" href="https://creativecommons.org/licenses/by/4.0/"> |
| 12 | + |
| 13 | +<!-- See create_labs.md for how to create your own lab! --> |
| 14 | + |
| 15 | +</head> |
| 16 | +<body> |
| 17 | +<!-- For GitHub Pages formatting: --> |
| 18 | +<div class="container-lg px-3 my-5 markdown-body"> |
| 19 | +<h1>ラボ演習 regex1</h1> |
| 20 | +<p> |
| 21 | +これはセキュアなソフトウェア開発に関するラボ演習です。 |
| 22 | +ラボの詳細については、<a href="introduction.html" target="_blank">概要</a>をご覧ください。 |
| 23 | + |
| 24 | +<p> |
| 25 | +<h2>ゴール</h2> |
| 26 | +<p> |
| 27 | +<b>入力検証のためのシンプルな正規表現の書き方を学びます。</b> |
| 28 | + |
| 29 | +<p> |
| 30 | +<h2>背景</h2> |
| 31 | +<p> |
| 32 | +正規表現(regular expressions: regexes)はテキストのパターンを表現するために広く利用されている表記方法です。 |
| 33 | +正規表現は入力の検証に使うことができます。正しく使用することで、多くの攻撃への対策となります。 |
| 34 | + |
| 35 | +<p> |
| 36 | +正規表現の表記方法は言語により若干異なりますが、大枠では共通しています。以下が正規表現を表記するための基本的なルールです。 |
| 37 | + |
| 38 | +<ol> |
| 39 | +<li>最も単純なルールは、文字や数字はそれ自身にマッチするということです。つまり、"<tt>d</tt>" という正規表現は "<tt>d</tt>" という文字にマッチします。多くの実装 |
| 40 | +ではデフォルトで大文字・小文字を区別してマッチし、それは通常望まれる動作です。 |
| 41 | +<li>もうひとつのルールは、いくつかの文字のうちどれかを指定するために大カッコで囲むというものです。もし大カッコが単なる英数字を囲っていた場合は、それらは囲まれた英数字のどれかにマッチします。つまり <tt>[brt]</tt> は単一の文字である "<tt>b</tt>", "<tt>r</tt>", "<tt>t</tt>" のどれかにマッチします。 |
| 42 | +大カッコの中ではダッシュ("-")で文字の範囲を示すことができます。つまり <tt>[A-D]</tt> はその範囲の一文字、すなわち一文字の A、一文字の B、一文字の C、一文字の D のどれかにマッチします。 |
| 43 | +大カッコの中には複数の範囲を書くことができます。 |
| 44 | +例えば <tt>[A-Za-z]</tt> は、大文字のアルファベット一文字または小文字のアルファ |
| 45 | +ベット一文字にマッチします。 |
| 46 | +(このラボでは EBCDIC のような使われなくなって久しい文字システムを使用していないと仮定しています) |
| 47 | +<li>パターンに続けて "<tt>*</tt>" が書かれた場合、それは"<i>0回以上</i>"を意味します。 |
| 48 | +ほとんど全ての正規表現の実装では(ただし POSIX BRE を除く)、パターンに続けて "<tt>+</tt>" が書かれた場合は"<i>1回以上</i>"を意味します。 |
| 49 | +つまり <tt>[A-D]*</tt> は、A, B, C, D のどれかが0文字以上続く場合にマッチします |
| 50 | +。 |
| 51 | +<li>選択肢、つまり指定したどれかにマッチするためには、"<tt>|</tt>" を使うことができます。 |
| 52 | +"<tt>|</tt>" は優先度が低いので、入力の検証に用いる場合は正しい選択肢を小カッコで囲む必要があります。つまり、例えば "yes" または "no" にマッチさせる方法は "<tt>(yes|no)</tt>" です。 |
| 53 | +</ol> |
| 54 | + |
| 55 | +<p> |
| 56 | +<h2>タスクの詳細</h2> |
| 57 | +<p> |
| 58 | +正規表現を使って入力の<i>検証</i>を行いたいとします。 |
| 59 | +それはつまり、入力は正規表現のパターンに<i>完全に</i>一致するということです。 |
| 60 | +正規表現では、シンボルを前置したり後置したりして、(マルチラインモードではなく)デフォルトモードでそれを行うことができます。 |
| 61 | +残念ながら、異なるプラットフォームでは入力に完全一致させるために異なる正規表現のシンボルを使用します。 |
| 62 | +以下の表は様々なプラットフォーム(で使用されるデフォルトの正規表現システム)において、どんな前置や後置が必要かを簡単に示したものです。 |
| 63 | + |
| 64 | +<p> |
| 65 | +<table> |
| 66 | +<tr> |
| 67 | +<th>プラットフォーム |
| 68 | +<th>前置 |
| 69 | +<th>後置 |
| 70 | +<tr> |
| 71 | +<td>POSIX BRE, POSIX ERE, and ECMAScript (JavaScript) |
| 72 | +<td>“^” |
| 73 | +<td>“$” |
| 74 | +<tr> |
| 75 | +<td>Java, .NET, PHP, Perl, and PCRE |
| 76 | +<td>“^” or “\A” |
| 77 | +<td>“\z” |
| 78 | +<tr> |
| 79 | +<td>Golang, Rust crate regex, and RE2 |
| 80 | +<td>“^” or “\A” |
| 81 | +<td>“$” or “\z” |
| 82 | +<tr> |
| 83 | +<td>Python |
| 84 | +<td>“^” or “\A” |
| 85 | +<td>“\Z” (not “\z”) |
| 86 | +<tr> |
| 87 | +<td>Ruby |
| 88 | +<td>“\A” |
| 89 | +<td>“\z” |
| 90 | +</table> |
| 91 | + |
| 92 | +<p> |
| 93 | +例えば ECMAScript (JavaScript) で入力を "<tt>ab</tt>" または "<tt>de</tt>" としたい場合に使用する正規表現は "<tt>^(ab|de)$</tt>" になります。 |
| 94 | +同じことを Python で行う場合は "<tt>^(ab|de)\Z</tt>" または "<tt>\A(ab|de)\Z</tt>" となります。(正規表現のパターンが少し違うことに注意してください) |
| 95 | + |
| 96 | +<p> |
| 97 | +より詳しい情報は OpenSSF ガイド <a href="https://best.openssf.org/Correctly-Using-Regular-Expressions">Correctly Using Regular Expressions for Secure Input Validation</a> にあります。 |
| 98 | + |
| 99 | +<p> |
| 100 | +<h2>演習 (<span id="grade"></span>)</h2> |
| 101 | + |
| 102 | +<b> |
| 103 | +以下の要求を満たす正規表現(regex)のパターンを作成してください。 |
| 104 | +</b> |
| 105 | + |
| 106 | +<p> |
| 107 | +必要に応じて「ヒント」や「諦める」のボタンを押してください。 |
| 108 | + |
| 109 | +<h3>Part 1</h3> |
| 110 | +<p> |
| 111 | +ECMAScript (JavaScript) で使うことを想定して、文字 "Y" または "N" のみにマッチする正規表現を作成してください。 |
| 112 | +<!-- |
| 113 | +You can use this an example for new labs. |
| 114 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 115 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 116 | +--> |
| 117 | +<form id="lab1"><pre><code |
| 118 | +><input id="attempt0" type="text" size="60" spellcheck="false" value=""> |
| 119 | +</code></pre> |
| 120 | +<button type="button" class="hintButton">ヒント</button> |
| 121 | +<button type="button" class="resetButton">リセット</button> |
| 122 | +<button type="button" class="giveUpButton">諦める</button> |
| 123 | +</form> |
| 124 | + |
| 125 | +<h3>Part 2</h3> |
| 126 | +<p> |
| 127 | +ECMAScript (JavaScript) で使うことを想定して、1文字以上の大文字のアルファベット(A から Z)にのみにマッチする正規表現を作成してください。 |
| 128 | +<!-- |
| 129 | +You can use this an example for new labs. |
| 130 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 131 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 132 | +--> |
| 133 | +<form id="lab2"> |
| 134 | +<pre><code |
| 135 | +><input id="attempt1" type="text" size="60" spellcheck="false" value=""> |
| 136 | +</code></pre> |
| 137 | +<button type="button" class="hintButton">ヒント</button> |
| 138 | +<button type="button" class="resetButton">リセット</button> |
| 139 | +<button type="button" class="giveUpButton">諦める</button> |
| 140 | +</form> |
| 141 | + |
| 142 | +<h3>Part 3</h3> |
| 143 | +<p> |
| 144 | +ECMAScript (JavaScript) で使うことを想定して、単語 "true" または "false" のみにマッチする正規表現を作成してください。 |
| 145 | +<!-- |
| 146 | +You can use this an example for new labs. |
| 147 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 148 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 149 | +--> |
| 150 | +<form id="lab3"> |
| 151 | +<pre><code |
| 152 | +><input id="attempt2" type="text" size="60" spellcheck="false" value=""> |
| 153 | +</code></pre> |
| 154 | +<button type="button" class="hintButton">ヒント</button> |
| 155 | +<button type="button" class="resetButton">リセット</button> |
| 156 | +<button type="button" class="giveUpButton">諦める</button> |
| 157 | +</form> |
| 158 | + |
| 159 | +<h3>Part 4</h3> |
| 160 | +<p> |
| 161 | +1文字以上の大文字のアルファベット(A から Z)にのみにマッチする正規表現を作成してください。 |
| 162 | +ただし今回は、(JavaScript ではなく)Python で使うことを想定してください。 |
| 163 | +<!-- |
| 164 | +You can use this an example for new labs. |
| 165 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 166 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 167 | +--> |
| 168 | +<form id="lab4"> |
| 169 | +<pre><code |
| 170 | +><input id="attempt3" type="text" size="60" spellcheck="false" value=""> |
| 171 | +</code></pre> |
| 172 | +<button type="button" class="hintButton">ヒント</button> |
| 173 | +<button type="button" class="resetButton">リセット</button> |
| 174 | +<button type="button" class="giveUpButton">諦める</button> |
| 175 | +</form> |
| 176 | + |
| 177 | +<h3>Part 5</h3> |
| 178 | +<p> |
| 179 | +1文字の大文字のアルファベット(A から Z)に続けてダッシュ ("-")、さらに続けて1文字以上の数字にのみにマッチする正規表現を作成してください。 |
| 180 | +今回は、(JavaScript ではなく)Ruby で使うことを想定してください。 |
| 181 | +<!-- |
| 182 | +You can use this an example for new labs. |
| 183 | +For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use |
| 184 | +<textarea id="attempt" rows="2" cols="65">...</textarea> |
| 185 | +--> |
| 186 | +<form id="lab5"> |
| 187 | +<pre><code |
| 188 | +><input id="attempt4" type="text" size="60" spellcheck="false" value=""> |
| 189 | +</code></pre> |
| 190 | +<button type="button" class="hintButton">ヒント</button> |
| 191 | +<button type="button" class="resetButton">リセット</button> |
| 192 | +<button type="button" class="giveUpButton">諦める</button> |
| 193 | +</form> |
| 194 | + |
| 195 | +<br><br> |
| 196 | +<p> |
| 197 | +<i>このラボは、<a href="https://www.linuxfoundation.org/">Linux Foundation</a>のDavid A. Wheelerによって開発されました。</i> |
| 198 | +<br><br> |
| 199 | +<p id="correctStamp" class="small"> |
| 200 | +<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly> |
| 201 | +</textarea> |
| 202 | + |
| 203 | +</div><!-- End GitHub pages formatting --> |
| 204 | +</body> |
| 205 | +</html> |
0 commit comments