Skip to content

Commit 9984826

Browse files
Add lab "regex0" (#459)
Some learners may skip this, but let's give a simple introduction for those who have never seen regexes. Signed-off-by: David A. Wheeler <[email protected]>
1 parent 4ff762d commit 9984826

File tree

3 files changed

+201
-2
lines changed

3 files changed

+201
-2
lines changed

docs/labs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Here are the labs available:
1515

1616
* [hello](hello.html) - simple "Hello, world!" demo
1717
* [input1](input1.html) - input validation (simple types)
18+
* [regex0](regex0.html) - regular expressions (regexes) - introduction
1819
* [regex1](regex1.html) - regular expressions (regexes)
1920
* [input2](input2.html) - input validation (more complex situations)
2021
* [csp1](csp1.html) - Content Security Policy (CSP)
@@ -58,7 +59,7 @@ The items marked "PLANNED-1" are those we intend to do first;
5859
* Input Validation: A Few Simple Data Types - [input1](input1.html)
5960
* Sidequest: Text, Unicode, and Locales
6061
* Validating Text
61-
* Introduction to Regular Expressions
62+
* Introduction to Regular Expressions - [regex0](regex0.html)
6263
* Using Regular Expressions for Text Input Validation - [regex1](regex1.html), [input2](input2.html)
6364
* Countering ReDoS Attacks on Regular Expressions - PLANNED-2
6465
* Input Validation: Beyond Numbers and Text

docs/labs/regex0.html

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css">
7+
<link rel="stylesheet" href="checker.css">
8+
<script src="js-yaml.min.js"></script>
9+
<script src="checker.js"></script>
10+
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">
11+
12+
<!-- See create_labs.md for how to create your own lab! -->
13+
14+
<!-- Sample expected answer -->
15+
<script id="expected0" type="plain/text">
16+
cat
17+
</script>
18+
19+
<!-- Full pattern of correct answer -->
20+
<script id="correct0" type="plain/text">
21+
cat
22+
</script>
23+
24+
<!-- Sample expected answer -->
25+
<script id="expected1" type="plain/text">
26+
ABBBB
27+
</script>
28+
29+
<!-- Full pattern of correct answer -->
30+
<script id="correct1" type="plain/text">
31+
A(\+|A\*)
32+
B(\+|B\*)
33+
</script>
34+
35+
<script id="info" type="application/yaml">
36+
---
37+
hints:
38+
- present: |-
39+
^$
40+
text: You need to enter a pattern.
41+
- present: |-
42+
^\^
43+
text: We are looking for 'cat' anywhere, not just the beginning,
44+
in this exercise.
45+
- absent: |-
46+
c
47+
text: If you are searching for "cat" you need to look for a "c"
48+
examples:
49+
- - "x"
50+
- absent: |-
51+
cat
52+
text: The pattern "cat" is needed to search for "cat".
53+
- absent: |-
54+
A
55+
index: 1
56+
text: You need to mention A.
57+
- absent: |-
58+
B
59+
index: 1
60+
text: You need to mention B.
61+
- absent: |-
62+
A(\+|A\*)
63+
index: 1
64+
text: Use "A+" to indicate "one or more A". You could also write "AA*".
65+
- absent: |-
66+
B(\+|B\*)
67+
index: 1
68+
text: Use "B+" to indicate "one or more B". You could also write "BB*".
69+
# successes:
70+
# - - " query ( 'id' ) . isInt ( {min: 1 , max: 9999 } ) ,"
71+
# - - " query ( `id` ) . isInt ( {min: 1 , max: 9_999 } ) , "
72+
# - - 'query ( "id" ) . isInt ( {min: 1 , max: 9_999 } ) ,'
73+
# failures:
74+
# - - " query,"
75+
# - - 'query(''id'').isint({min: 1, max: 9999})'
76+
# - - 'query(''id'').isInt({min: 1, max: 9999})'
77+
#
78+
# Remove all whitespace, so we can make our patterns easier to read
79+
#
80+
preprocessing:
81+
-
82+
- |-
83+
\s*
84+
- ""
85+
# -
86+
# - |-
87+
# (\\s\*)?\s+(\\s\*)?
88+
# - "\\s*"
89+
# debug: true
90+
</script>
91+
<!--
92+
93+
-->
94+
95+
</head>
96+
<body>
97+
<!-- For GitHub Pages formatting: -->
98+
<div class="container-lg px-3 my-5 markdown-body">
99+
<h1>Lab Exercise regex0</h1>
100+
<p>
101+
This is a lab exercise on developing secure software.
102+
For more information, see the <a href="introduction.html" target="_blank">introduction to
103+
the labs</a>.
104+
105+
<p>
106+
<h2>Goal</h2>
107+
<p>
108+
<b>Learn how to create simple regular expressions.</b>
109+
110+
<p>
111+
<h2>Background</h2>
112+
<p>
113+
Regular expressions (regexes) are a widely-used notation for
114+
expressing text patterns.
115+
We will later see how to use regexes to validate input.
116+
117+
<p>
118+
Different regex languages have slightly different notations,
119+
but they have much in common. Here are some basic rules for regex
120+
notations:
121+
122+
<ol>
123+
<li>The most trivial rule is that a letter or digit matches itself. That is, the regex “<tt>d</tt>” matches the letter “<tt>d</tt>”. Most implementations use case-sensitive matches by default, and that is usually what you want.
124+
<li>Another rule is that square brackets surround a rule that specifies any of a number of characters. If the square brackets surround just alphanumerics, then the pattern matches any of them. So <tt>[brt]</tt> matches a single “<tt>b</tt>”, “<tt>r</tt>”, or “<tt>t</tt>”.
125+
Inside the brackets you can include
126+
ranges of symbols separated by dash ("-"), so
127+
<tt>[A-D]</tt> will match one character, which can be one A, one B, one C,
128+
or one D.
129+
You can do this more than once.
130+
For example,
131+
the term <tt>[A-Za-z]</tt> will match one character, which can be
132+
an uppercase Latin letter or a lowercase Latin letter.
133+
(This text assumes you're not using a long-obsolete character system
134+
like EBCDIC.)
135+
<li>If you follow a pattern with “<tt>&#42;</tt>”, that means
136+
<i>0 or more times</i>”.
137+
In almost all regex implementations (except POSIX BRE),
138+
following a pattern with "<tt>+</tt>" means "<i>1 or more times</i>".
139+
So <tt>[A-D]*</tt> will match 0 or more letters as long as every letter
140+
is an A, B, C, or D.
141+
</ol>
142+
143+
<p>
144+
<h2>Task Information</h2>
145+
<p>
146+
We're going to create some simple regular expressions (regexes)
147+
to look for certain patterns.
148+
149+
<p>
150+
<h2>Interactive Lab (<span id="grade"></span>)</h2>
151+
152+
<b>Please create regular expression (regex) patterns
153+
that meet the criteria below.</b>
154+
155+
<h3>Part 1</h2>
156+
<p>
157+
Create a regular expression, for use in ECMAScript (JavaScript),
158+
that searches for the term "cat" anywhere in a string.
159+
160+
<!--
161+
You can use this an example for new labs.
162+
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
163+
<textarea id="attempt" rows="2" cols="65">...</textarea>
164+
-->
165+
<form id="lab1"><pre><code
166+
><input id="attempt0" type="text" size="60" spellcheck="false" value="">
167+
</code></pre>
168+
<button type="button" class="hintButton">Hint</button>
169+
<button type="button" class="resetButton">Reset</button>
170+
<button type="button" class="giveUpButton">Give up</button>
171+
</form>
172+
173+
<h3>Part 2</h2>
174+
<p>
175+
Create a regular expression, for use in ECMAScript (JavaScript),
176+
that searches for one or more "A" followed by one or more "B".
177+
<!--
178+
You can use this an example for new labs.
179+
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
180+
<textarea id="attempt" rows="2" cols="65">...</textarea>
181+
-->
182+
<form id="lab2">
183+
<pre><code
184+
><input id="attempt1" type="text" size="60" spellcheck="false" value="">
185+
</code></pre>
186+
<button type="button" class="hintButton">Hint</button>
187+
<button type="button" class="resetButton">Reset</button>
188+
<button type="button" class="giveUpButton">Give up</button>
189+
</form>
190+
191+
<br><br>
192+
<p id="correctStamp" class="small">
193+
<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly>
194+
</textarea>
195+
196+
</div><!-- End GitHub pages formatting -->
197+
</body>
198+
</html>

docs/labs/regex1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ <h1>Lab Exercise regex1</h1>
258258
<p>
259259
<h2>Goal</h2>
260260
<p>
261-
<b>Learn how to create simple regular expressions.</b>
261+
<b>Learn how to create simple regular expressions for input validation.</b>
262262

263263
<p>
264264
<h2>Background</h2>

0 commit comments

Comments
 (0)