Skip to content

Commit 5a31b1c

Browse files
Merge pull request #741 from ossf/assert_no_yaml
Assert no yaml
2 parents 2a5a8a2 + 6236659 commit 5a31b1c

File tree

2 files changed

+84
-91
lines changed

2 files changed

+84
-91
lines changed

docs/labs/assert.html

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,101 +7,11 @@
77
<link rel="stylesheet" href="checker.css">
88
<script src="js-yaml.min.js"></script>
99
<script src="checker.js"></script>
10+
<script src="assert.js"></script>
1011
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">
1112

1213
<!-- See create_labs.md for how to create your own lab! -->
1314

14-
<!-- Sample expected answer -->
15-
<script id="expected0" type="plain/text">
16-
if (bindingResult.hasErrors()) {
17-
return "form";
18-
}
19-
</script>
20-
21-
<!-- Full pattern of correct answer -->
22-
<script id="correct0" type="plain/text">
23-
\s* if \( bindingResult \. hasErrors \( \) \) \{
24-
return "form" ;
25-
\} \s*
26-
</script>
27-
28-
<script id="info" type="application/yaml">
29-
---
30-
hints:
31-
- present: |
32-
assert
33-
text: The whole point of this exercise is to NOT use `assert`
34-
as a way to validate input from untrusted users.
35-
examples:
36-
- - |
37-
assert !bindingResult.hasErrors();
38-
- absent: |
39-
^\s*if
40-
text: Begin with `if` so you can return a result if there are errors.
41-
examples:
42-
- - |
43-
return "form";
44-
- present: (bindingresult|BindingResult)
45-
text: Java is case-sensitive. Use `bindingResult`, not
46-
`bindingresult` nor `BindingResult`.
47-
- present: (haserrors|HasErrors)
48-
text: Java is case-sensitive. Use `hasErrors`, not
49-
`haserrors` nor `HasErrors`.
50-
# https://docs.oracle.com/javase/specs/jls/se23/html/jls-14.html#jls-14.9
51-
- present: |
52-
^\s*if\s*[^\(\s]
53-
text: In Java, after the keyword `if` you must have an open left parenthesis.
54-
Conventionally there is one space between the `if` keyword and the
55-
open left parenthesis.
56-
examples:
57-
- - |
58-
if bindingResult.hasErrors
59-
- present: |
60-
^\s*if\s*\(\s*\!binding
61-
text: You have an extraneous `!` (not operator).
62-
Use the expression if (bindingResult.hasErrors()) ...
63-
examples:
64-
- - |
65-
if (!bindingResult.hasErrors())
66-
- absent: |
67-
^ if \( bindingResult \. hasErrors \( \) \)
68-
text: Begin the answer with the text
69-
`if (bindingResult.hasErrors())` so that a statement will
70-
be executed if that condition is true.
71-
- present: |
72-
if \( bindingResult \. hasErrors \( \) \) [^\{\s]
73-
text: Follow the conditional with an open brace, e.g.,
74-
`if (bindingResult.hasErrors()) {...`.
75-
- absent: |
76-
return "form"
77-
text: You need to use `return "form";` somewhere.
78-
- present: |
79-
return "form"
80-
absent:
81-
return "form" ;
82-
text: You need to use `;` (semicolon) after `return "form"` because
83-
in Java statements must be followed by a semicolon.
84-
- absent: |
85-
\} $
86-
text: The answer needs to end with `}` (closing brace).
87-
successes:
88-
- - |
89-
if ( bindingResult.hasErrors() ) {
90-
return "form";
91-
}
92-
- |
93-
if ( bindingResult . hasErrors ( ) ) { return "form" ; }
94-
failures:
95-
- |
96-
if ( bindingResult . hasErrors ( ) ) { return "form" }
97-
- |
98-
if ( ! bindingResult . hasErrors ( ) ) { return "form" ; }
99-
- |
100-
if bindingResult . hasErrors ( ) { return "form" ; }
101-
- |
102-
if ( bindingResult . hasErrors ) { return "form" ; }
103-
# debug: true
104-
</script>
10515
</head>
10616
<body>
10717
<!-- For GitHub Pages formatting: -->

docs/labs/assert.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
info =
2+
{
3+
hints: [
4+
{
5+
present: "assert",
6+
text: "The whole point of this exercise is to NOT use `assert` as a way to validate input from untrusted users.",
7+
"examples": [
8+
[ "assert !bindingResult.hasErrors();\n" ]
9+
]
10+
},
11+
{
12+
absent: String.raw`^\s* if `,
13+
text: "Begin with `if` so you can return a result if there are errors.",
14+
"examples": [
15+
[ "return \"form\";" ]
16+
]
17+
},
18+
{
19+
present: "(bindingresult|BindingResult)",
20+
text: "Java is case-sensitive. Use `bindingResult`, not `bindingresult` nor `BindingResult`."
21+
},
22+
{
23+
present: "(haserrors|HasErrors)",
24+
text: "Java is case-sensitive. Use `hasErrors`, not `haserrors` nor `HasErrors`."
25+
},
26+
{
27+
present: String.raw`^\s*if\s*[^\(\s]`,
28+
text: "In Java, after the keyword `if` you must have an open left parenthesis. Conventionally there is one space between the `if` keyword and the open left parenthesis.",
29+
"examples": [
30+
[ "if bindingResult.hasErrors" ]
31+
]
32+
},
33+
{
34+
present: String.raw`^\s*if\s*\(\s*\!binding`,
35+
text: "You have an extraneous `!` (not operator). Use the expression if (bindingResult.hasErrors()) ...",
36+
"examples": [
37+
[ "if (!bindingResult.hasErrors())" ]
38+
]
39+
},
40+
{
41+
absent: String.raw`^ if \( bindingResult \. hasErrors \( \) \) `,
42+
text: "Begin the answer with the text `if (bindingResult.hasErrors())` so that a statement will be executed if that condition is true."
43+
},
44+
{
45+
present: String.raw`if \( bindingResult \. hasErrors \( \) \) [^\{\s] `,
46+
text: "Follow the conditional with an open brace, e.g., `if (bindingResult.hasErrors()) {...`."
47+
},
48+
{
49+
absent: String.raw`return "form"
50+
`,
51+
text: "You need to use `return \"form\";` somewhere."
52+
},
53+
{
54+
present: String.raw`return "form"`,
55+
absent: String.raw`return "form" ;`,
56+
text: "You need to use `;` (semicolon) after `return \"form\"` because in Java statements must be followed by a semicolon."
57+
},
58+
{
59+
absent: String.raw`\} $`,
60+
text: "The answer needs to end with `}` (closing brace)."
61+
},
62+
],
63+
expected: [
64+
` if (bindingResult.hasErrors()) {
65+
return "form";
66+
}`
67+
],
68+
correct: [
69+
String.raw`\s* if \( bindingResult \. hasErrors \( \) \) \{
70+
return "form" ;
71+
\} \s*`,
72+
],
73+
successes: [
74+
[ "if ( bindingResult.hasErrors() ) {\n return \"form\";\n}\n" ],
75+
[ "if ( bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" ],
76+
],
77+
failures: [
78+
[ "if ( bindingResult . hasErrors ( ) ) { return \"form\" }\n" ],
79+
[ "if ( ! bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" ],
80+
[ "if bindingResult . hasErrors ( ) { return \"form\" ; }\n" ],
81+
[ "if ( bindingResult . hasErrors ) { return \"form\" ; }\n" ],
82+
]
83+
}

0 commit comments

Comments
 (0)