Skip to content

Commit c9e4a28

Browse files
Merge pull request #451 from ossf/refine_input1
Refine input1
2 parents 6ef53e5 + 3091b79 commit c9e4a28

File tree

6 files changed

+93
-67
lines changed

6 files changed

+93
-67
lines changed

docs/labs/csp1.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<h1>Lab Exercise csp1</h1>
202202
<p>
203203
This is a lab exercise on developing secure software.
204-
For more information, see the <a href="introduction.html">introduction to
204+
For more information, see the <a href="introduction.html" target="_blank">introduction to
205205
the labs</a>.
206206

207207
<p>
@@ -387,8 +387,8 @@ <h2>Task Information</h2>
387387
<h2>Interactive Lab (<span id="grade"></span>)</h2>
388388
<p>
389389
<form id="lab">
390-
<pre><code>
391-
const express = require("express");
390+
<pre><code
391+
>const express = require("express");
392392
<input id="attempt0" type="text" size="60" spellcheck="false" value="const">
393393

394394
const app = express();

docs/labs/hello.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
<h1>Lab Exercise hello</h1>
121121
<p>
122122
This is a lab exercise on developing secure software.
123-
For more information, see the <a href="introduction.html">introduction to
123+
For more information, see the <a href="introduction.html" target="_blank">introduction to
124124
the labs</a>.
125125

126126
<p>
@@ -162,8 +162,8 @@ <h2>Interactive Lab (<span id="grade"></span>)</h2>
162162
<textarea id="attempt" rows="2" cols="65">...</textarea>
163163
-->
164164
<form id="lab">
165-
<pre><code>
166-
<input id="attempt0" type="text" size="65" spellcheck="false"
165+
<pre><code
166+
><input id="attempt0" type="text" size="60" spellcheck="false"
167167
value='console.log("Goodbye.");'>
168168
</code></pre>
169169
<button type="button" class="hintButton">Hint</button>

docs/labs/input1-emulation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h1>Lab Exercise Input1</h1>
88
<p>
99
This is a lab exercise on developing secure software.
10-
For more information, see the <a href="introduction.html">introduction to
10+
For more information, see the <a href="introduction.html" target="_blank">introduction to
1111
the labs</a>.
1212

1313
<p>

docs/labs/input1.html

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@
109109
<h1>Lab Exercise input1</h1>
110110
<p>
111111
This is a lab exercise on developing secure software.
112-
For more information, see the <a href="introduction.html">introduction to
112+
For more information, see the <a href="introduction.html" target="_blank">introduction to
113113
the labs</a>.
114114

115115
<p>
116-
<h2>Task</h2>
116+
<h2>Goal</h2>
117117
<p>
118-
<b>Please change the code below so the query parameter
119-
<tt>id</tt> <i>must</i> be an integer between 1 and 9999 (including
120-
those numbers).</b>
118+
Practice validating input of a simple data type.
121119

122120
<p>
123121
<h2>Background</h2>
@@ -130,18 +128,14 @@ <h2>Background</h2>
130128

131129
<p>
132130
<!-- https://expressjs.com/en/guide/routing.html -->
133-
Express allows us to state that when the system receives
134-
an HTTP <tt>get</tt> request for a given route
135-
(e.g., <tt>/invoices</tt>), Express will run a list of functions ("handlers").
131+
Express allows us to state that when the system receives a specific request,
132+
it will run a list of functions ("handlers").
136133
The library <tt>express-validator</tt> provides a set of validation functions
137134
to make it easy to add validation checks.
138135

139136
<p>
140137
The code below sets up handlers for a <tt>get</tt> request on path
141138
<tt>/invoices</tt>.
142-
This code could be triggered, for example, by requesting
143-
<tt>http://localhost:3000/invoices?id=1</tt>
144-
(if it was running at <tt>localhost</tt> and responding to port 3000).
145139
If there are no validation errors, the code is supposed to show the invoice id.
146140
If there is a validation error, it responds with HTTP
147141
error code 422 ("Unprocessable Content"), a status code suggesting
@@ -164,25 +158,25 @@ <h2>Background</h2>
164158
<p>
165159
<h2>Task Information</h2>
166160
<p>
167-
168-
<p>
169-
To complete this task,
170-
after the first parameter to <tt>app.get</tt>
161+
To complete this task:
162+
<ol>
163+
<li>After the first parameter to <tt>app.get</tt>
171164
which says <tt>'/invoices'</tt>,
172165
add a new comma-separated parameter.
173-
Start this new parameter with
166+
<li>Start this new parameter with
174167
<tt>query('id')</tt> to select the
175168
<tt>id</tt> parameter for validation (we've filled in this part
176169
to help get you started).
177-
After <tt>query('id')</tt> (and before the terminating comma),
170+
<li>After <tt>query('id')</tt> (and before the terminating comma),
178171
add a period (<tt>.</tt>) and the validation requirement
179172
<tt>isInt()</tt> (<tt>isInt</tt> validates that the named parameter is
180173
an integer).
181-
The <tt>isInt</tt> method takes, as an optional parameter inside
174+
<li>The <tt>isInt</tt> method takes, as an optional parameter inside
182175
its parentheses,
183176
an object providing a minimum and maximum, e.g.,
184177
<tt>isInt({min: YOUR_MINIMUM, max: YOUR_MAXIMUM})</tt>.
185178
Set <tt>min</tt> and <tt>max</tt> to specify the allowed range.
179+
</ol>
186180

187181
<p>
188182
Note: JavaScript names are case-sensitive, so <tt>isint</tt> won't work.
@@ -197,14 +191,18 @@ <h2>Task Information</h2>
197191
<p>
198192
<h2>Interactive Lab (<span id="grade"></span>)</h2>
199193
<p>
194+
<b>The code below accepts the query parameter <tt>id</tt> as input.
195+
Please change it so <tt>id</tt> is only accepted if it is
196+
an integer between 1 and 9999 (including those numbers).</b>
197+
<p>
200198
<!--
201199
You can use this an example for new labs.
202200
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
203201
<textarea id="attempt" rows="2" cols="65">...</textarea>
204202
-->
205203
<form id="lab">
206-
<pre></code>
207-
// Set up Express framework and express-validator library
204+
<pre><code
205+
>// Set up Express framework and express-validator library
208206
const express = require("express");
209207
const app = express();
210208
const { query, matchedData, validationResult } =

docs/labs/input2.html

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
<!-- Sample expected answer -->
1313
<script id="expected0" type="plain/text">
14-
query('id').isLength({max:80}).matches( /^[A-Z]{2}-[0-9]+-[0-9]+$/ ),
14+
query('id').isLength({max:80}).
15+
matches( /^[A-Z]{2}-[0-9]+-[0-9]+$/ ),
1516
</script>
1617

1718
<!-- Full pattern of correct answer.
@@ -78,6 +79,11 @@
7879
text: >
7980
You need to match one or more digits; * allows 0 or more.
8081
A + would be better suited for this task.
82+
- present: |
83+
\s*, , $
84+
text: >
85+
You have two commas at the end. Use only one. You may need to
86+
scroll or increase the text area to see both of them.
8187
successes:
8288
- - query(`id`).isLength( {max:80}).matches(/^[A-Z]{2}-\d+-[0-9]+$/),
8389
- - ' query (`id`) . isLength( {max:80}).matches(/^[A-Z]{2}-\d+-[0-9]+$/ ) , '
@@ -94,15 +100,16 @@
94100
<h1>Lab Exercise input2</h1>
95101
<p>
96102
This is a lab exercise on developing secure software.
97-
For more information, see the <a href="introduction.html">introduction to
103+
For more information, see the <a href="introduction.html" target="_blank">introduction to
98104
the labs</a>.
99105

100106
<p>
101107
<h2>Task</h2>
102108
<p>
103-
<b>Please change the code below so the query parameter
104-
<tt>id</tt> <i>must</i> be no longer than 80 characters
105-
and meets this application's part id format requirement.</b>
109+
<b>
110+
Practice validating specialized text input in a program
111+
using a regular expression.
112+
</b>
106113

107114
<p>
108115
<h2>Background</h2>
@@ -150,17 +157,17 @@ <h2>Task Information</h2>
150157
another dash (<tt>-</tt>), and another sequence of one or more digits.
151158

152159
<p>
153-
To do that,
154-
after the first parameter to <tt>app.get</tt>
155-
which says <tt>'/parts'</tt>,
156-
add a new comma-separated parameter.
157-
Start this new parameter with
160+
To do that:
161+
<ol>
162+
<li>After the first parameter to <tt>app.get</tt>
163+
which says <tt>'/parts'</tt>, add a new comma-separated parameter.
164+
<li>Start this new parameter with
158165
<tt>query('id')</tt> to select the
159-
<tt>id</tt> parameter for validation (we've have <i>not</i> filled
166+
<tt>id</tt> parameter for validation (we have <i>not</i> filled
160167
in this part in this lab).
161-
Add a period (<tt>.</tt>) and the validation requirement
168+
<li>Add a period (<tt>.</tt>) and the validation requirement
162169
<tt>isLength()</tt>
163-
The <tt>isLength</tt> method takes, as an optional parameter inside
170+
<li>The <tt>isLength</tt> method takes, as an optional parameter inside
164171
its parentheses,
165172
an object providing specific information such as a minimum and a maximum.
166173
We only want a maximum,
@@ -169,42 +176,55 @@ <h2>Task Information</h2>
169176
Those familiar with JavaScript will know that the "length" value is
170177
the length of the string in UTF-16 code units; that's fine
171178
for our purposes.
179+
</ol>
172180
</p>
173181

174182
<p>
175183
We also need to verify that the input matches our pattern,
176184
which we can verify using a regular expression.
177-
In this situation, we can do this by appending another
178-
period (<tt>.</tt>) and the validation requirement
185+
In this situation, we can do this by:
186+
<ol>
187+
<li>appending another period (<tt>.</tt>) and the validation requirement
179188
<tt>matches()</tt>.
180-
Inside those parenthesis you should supply slash (<tt>/</tt>, the
189+
<li>Inside those parenthesis you should supply slash (<tt>/</tt>, the
181190
text of the regular expression to match, and another slash (<tt>/</tt>).
182191
In JavaScript, a pair of slashes (<tt>/</tt>) surrounds a regular expression.
183-
Remember to match the <i>entire</i> expression
192+
<li>Remember to match the <i>entire</i> expression
184193
(in other words, use <tt>^</tt> and <tt>$</tt>).
185-
Remember, a way to match a single uppercase character is the
186-
pattern <tt>[A-Z]</tt> ... good luck!
194+
<li>Also, remember that a way to match a single uppercase character is the
195+
pattern <tt>[A-Z]</tt>
196+
</ol>
187197

188198
<p>
189199
<h2>Interactive Lab (<span id="grade"></span>)</h2>
190200
<p>
201+
<b>Please change the code below so the query parameter
202+
<tt>id</tt> is only accepted if it's no longer than 80 characters
203+
and meets this application's part id format requirement.
204+
The format is
205+
two uppercase Latin letters (each <tt>A</tt> through <tt>Z</tt>),
206+
then a dash (<tt>-</tt>), a sequence of one or more digits,
207+
another dash (<tt>-</tt>), and another sequence of one or more digits.
208+
</b>
209+
<p>
210+
191211
<!--
192212
You can use this an example for new labs.
193213
For multi-line inputs, instead of <input id="attempt" type="text" ...>, use
194214
<textarea id="attempt" rows="2" cols="65">...</textarea>
195215
-->
196216
<form id="lab">
197-
<pre><code>
198-
// Set up Express framework and express-validator library
217+
<pre><code
218+
>// Set up Express framework and express-validator library
199219
const express = require("express");
200220
const app = express();
201221
const { query, matchedData, validationResult } =
202222
require('express-validator');
203223

204224
// Implement requests, e.g., http://localhost:3000/parts?id=1
205225
app.get('/parts',
206-
<input id="attempt0" type="text" size="65" spellcheck="false"
207-
value=" ,">
226+
<textarea id="attempt0" rows="2" cols="64" spellcheck="false">
227+
,</textarea>
208228
(req, res) =&gt; { // Execute this code if /invoices seen
209229
const result = validationResult(req); // Retrieve errors
210230
if (result.isEmpty()) { // No errors

docs/labs/regex1.html

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@
8888
<script id="info" type="application/yaml">
8989
---
9090
hints:
91+
- present: |-
92+
/
93+
text: In JavaScript a constant regular expression is surrounded by
94+
forward slashes like /PATTERN/. However, for this exercise we only
95+
want the text inside the slashes (the pattern itself).
96+
examples:
97+
- - "/"
9198
- absent: |-
9299
^\^
93100
text: For input validation, start with '^' to indicate a full match.
@@ -245,14 +252,13 @@
245252
<h1>Lab Exercise regex1</h1>
246253
<p>
247254
This is a lab exercise on developing secure software.
248-
For more information, see the <a href="introduction.html">introduction to
255+
For more information, see the <a href="introduction.html" target="_blank">introduction to
249256
the labs</a>.
250257

251258
<p>
252-
<h2>Task</h2>
259+
<h2>Goal</h2>
253260
<p>
254-
<b>Please create various regular expression (regex) patterns
255-
that meet the criteria below.</b>
261+
<b>Learn how to create simple regular expressions.</b>
256262

257263
<p>
258264
<h2>Background</h2>
@@ -262,8 +268,6 @@ <h2>Background</h2>
262268
Regexes can be used to validate input; when used correctly,
263269
they can counter many attacks.
264270

265-
<p>
266-
<h2>Task Information</h2>
267271
<p>
268272
Different regex languages have slightly different notations,
269273
but they have much in common. Here are some basic rules for regex
@@ -294,6 +298,8 @@ <h2>Task Information</h2>
294298
So for example, "<tt>(yes|no)</tt>" is a way to match either "yes" or "no".
295299
</ol>
296300

301+
<p>
302+
<h2>Task Information</h2>
297303
<p>
298304
We want to use regexes to <i>validate</i> input.
299305
That is, the input should <i>completely</i> match the regex pattern.
@@ -344,6 +350,9 @@ <h2>Task Information</h2>
344350
<p>
345351
<h2>Interactive Lab (<span id="grade"></span>)</h2>
346352

353+
<b>Please create regular expression (regex) patterns
354+
that meet the criteria below.</b>
355+
347356
<h3>Part 1</h2>
348357
<p>
349358
Create a regular expression, for use in ECMAScript (JavaScript),
@@ -353,9 +362,8 @@ <h3>Part 1</h2>
353362
For multi-line inputs, instead of <input id="attempt0" type="text" ...>, use
354363
<textarea id="attempt" rows="2" cols="65">...</textarea>
355364
-->
356-
<form id="lab1">
357-
<pre></code>
358-
<input id="attempt0" type="text" size="60" spellcheck="false" value="">
365+
<form id="lab1"><pre><code
366+
><input id="attempt0" type="text" size="60" spellcheck="false" value="">
359367
</code></pre>
360368
<button type="button" class="hintButton">Hint</button>
361369
<button type="button" class="resetButton">Reset</button>
@@ -372,8 +380,8 @@ <h3>Part 2</h2>
372380
<textarea id="attempt" rows="2" cols="65">...</textarea>
373381
-->
374382
<form id="lab2">
375-
<pre></code>
376-
<input id="attempt1" type="text" size="60" spellcheck="false" value="">
383+
<pre><code
384+
><input id="attempt1" type="text" size="60" spellcheck="false" value="">
377385
</code></pre>
378386
<button type="button" class="hintButton">Hint</button>
379387
<button type="button" class="resetButton">Reset</button>
@@ -390,8 +398,8 @@ <h3>Part 3</h2>
390398
<textarea id="attempt" rows="2" cols="65">...</textarea>
391399
-->
392400
<form id="lab3">
393-
<pre></code>
394-
<input id="attempt2" type="text" size="60" spellcheck="false" value="">
401+
<pre><code
402+
><input id="attempt2" type="text" size="60" spellcheck="false" value="">
395403
</code></pre>
396404
<button type="button" class="hintButton">Hint</button>
397405
<button type="button" class="resetButton">Reset</button>
@@ -409,8 +417,8 @@ <h3>Part 4</h2>
409417
<textarea id="attempt" rows="2" cols="65">...</textarea>
410418
-->
411419
<form id="lab4">
412-
<pre></code>
413-
<input id="attempt3" type="text" size="60" spellcheck="false" value="">
420+
<pre><code
421+
><input id="attempt3" type="text" size="60" spellcheck="false" value="">
414422
</code></pre>
415423
<button type="button" class="hintButton">Hint</button>
416424
<button type="button" class="resetButton">Reset</button>
@@ -429,8 +437,8 @@ <h3>Part 5</h2>
429437
<textarea id="attempt" rows="2" cols="65">...</textarea>
430438
-->
431439
<form id="lab5">
432-
<pre></code>
433-
<input id="attempt4" type="text" size="60" spellcheck="false" value="">
440+
<pre><code
441+
><input id="attempt4" type="text" size="60" spellcheck="false" value="">
434442
</code></pre>
435443
<button type="button" class="hintButton">Hint</button>
436444
<button type="button" class="resetButton">Reset</button>

0 commit comments

Comments
 (0)