Skip to content

Commit 19d56d3

Browse files
committed
fix: use comment instead of kino inputs
1 parent 23ff44b commit 19d56d3

File tree

2 files changed

+69
-83
lines changed

2 files changed

+69
-83
lines changed

modules/2-owasp.livemd

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!-- livebook:{"persist_outputs":true} -->
2-
31
# ESCT: Part 2 - OWASP
42

53
```elixir
@@ -103,17 +101,9 @@ Notable CWEs included are CWE-259: Use of Hard-coded Password, CWE-327: Broken o
103101

104102
_Please uncomment the function call that you believe is correct._
105103

106-
<!-- livebook:{"attrs":"eyJtb2R1bGVfaWQiOm51bGwsInF1ZXN0aW9uX2lkIjpudWxsLCJzb3VyY2UiOiJkZWZtb2R1bGUgUGFzc3dvcmRDb21wYXJlIGRvXG4gIGRlZiBvcHRpb25fb25lKHBhc3N3b3JkLCBtZDVfaGFzaCkgZG9cbiAgICBjYXNlIDpjcnlwdG8uaGFzaCg6bWQ1LCBwYXNzd29yZCkgPT0gbWQ1X2hhc2ggZG9cbiAgICAgIHRydWUgLT4gOmVudHJ5X2dyYW50ZWRfb3AxXG4gICAgICBmYWxzZSAtPiA6ZW50cnlfZGVuaWVkX29wMVxuICAgIGVuZFxuICBlbmRcblxuICBkZWYgb3B0aW9uX3R3byhwYXNzd29yZCwgYmNyeXB0X3NhbHRlZF9oYXNoKSBkb1xuICAgIGNhc2UgQmNyeXB0LnZlcmlmeV9wYXNzKHBhc3N3b3JkLCBiY3J5cHRfc2FsdGVkX2hhc2gpIGRvXG4gICAgICB0cnVlIC0+IDplbnRyeV9ncmFudGVkX29wMlxuICAgICAgZmFsc2UgLT4gOmVudHJ5X2RlbmllZF9vcDJcbiAgICBlbmRcbiAgZW5kXG5lbmRcblxuIyBETyBOT1QgQ0hBTkdFIENPREUgQUJPVkUgVEhJUyBMSU5FID09PT09PT09PT09PT09PT09PT09PT09PT1cblxuIyBQYXNzd29yZENvbXBhcmUub3B0aW9uX29uZShcInVzZXJzX3Bhc3N3b3JkXCIsIG1kNV9oYXNoKVxuIyBQYXNzd29yZENvbXBhcmUub3B0aW9uX3R3byhcInVzZXJzX3Bhc3N3b3JkXCIsIGJjcnlwdF9zYWx0ZWRfaGFzaCkifQ","chunks":[[0,178],[180,859]],"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
104+
<!-- livebook:{"attrs":"eyJtb2R1bGVfaWQiOm51bGwsInF1ZXN0aW9uX2lkIjpudWxsLCJzb3VyY2UiOiIjT1dBU1A6MVxuZGVmbW9kdWxlIFBhc3N3b3JkQ29tcGFyZSBkb1xuICBkZWYgb3B0aW9uX29uZShwYXNzd29yZCwgbWQ1X2hhc2gpIGRvXG4gICAgY2FzZSA6Y3J5cHRvLmhhc2goOm1kNSwgcGFzc3dvcmQpID09IG1kNV9oYXNoIGRvXG4gICAgICB0cnVlIC0+IDplbnRyeV9ncmFudGVkX29wMVxuICAgICAgZmFsc2UgLT4gOmVudHJ5X2RlbmllZF9vcDFcbiAgICBlbmRcbiAgZW5kXG5cbiAgZGVmIG9wdGlvbl90d28ocGFzc3dvcmQsIGJjcnlwdF9zYWx0ZWRfaGFzaCkgZG9cbiAgICBjYXNlIEJjcnlwdC52ZXJpZnlfcGFzcyhwYXNzd29yZCwgYmNyeXB0X3NhbHRlZF9oYXNoKSBkb1xuICAgICAgdHJ1ZSAtPiA6ZW50cnlfZ3JhbnRlZF9vcDJcbiAgICAgIGZhbHNlIC0+IDplbnRyeV9kZW5pZWRfb3AyXG4gICAgZW5kXG4gIGVuZFxuZW5kXG5cbiMgRE8gTk9UIENIQU5HRSBDT0RFIEFCT1ZFIFRISVMgTElORSA9PT09PT09PT09PT09PT09PT09PT09PT09XG5cbiMgUGFzc3dvcmRDb21wYXJlLm9wdGlvbl9vbmUoXCJ1c2Vyc19wYXNzd29yZFwiLCBtZDVfaGFzaClcbiMgUGFzc3dvcmRDb21wYXJlLm9wdGlvbl90d28oXCJ1c2Vyc19wYXNzd29yZFwiLCBiY3J5cHRfc2FsdGVkX2hhc2gpIn0","chunks":null,"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
107105

108106
```elixir
109-
module_id = Kino.Input.select("Module", [{OWASP, "OWASP"}])
110-
question_id = Kino.Input.number("Question ID")
111-
Kino.render(Kino.Layout.grid([module_id, question_id], columns: 2))
112-
nil
113-
114-
module_id = Kino.Input.read(module_id)
115-
question_id = Kino.Input.read(question_id)
116-
117107
result =
118108
defmodule PasswordCompare do
119109
def option_one(password, md5_hash) do
@@ -131,6 +121,25 @@ result =
131121
end
132122
end
133123

124+
[module_id, question_id] =
125+
"#OWASP:1\ndefmodule PasswordCompare do\n def option_one(password, md5_hash) do\n case :crypto.hash(:md5, password) == md5_hash do\n true -> :entry_granted_op1\n false -> :entry_denied_op1\n end\n end\n\n def option_two(password, bcrypt_salted_hash) do\n case Bcrypt.verify_pass(password, bcrypt_salted_hash) do\n true -> :entry_granted_op2\n false -> :entry_denied_op2\n end\n end\nend\n\n# DO NOT CHANGE CODE ABOVE THIS LINE =========================\n\n# PasswordCompare.option_one(\"users_password\", md5_hash)\n# PasswordCompare.option_two(\"users_password\", bcrypt_salted_hash)"
126+
|> String.split("\n", parts: 2)
127+
|> hd()
128+
|> String.trim_leading("#")
129+
|> String.split(":", parts: 2)
130+
131+
module_id =
132+
case %{"OWASP" => OWASP}[String.trim(module_id)] do
133+
nil -> raise "invalid module id: #{module_id}"
134+
module_id -> module_id
135+
end
136+
137+
question_id =
138+
case Integer.parse(String.trim(question_id)) do
139+
{id, ""} -> id
140+
_ -> raise "invalid question id: #{question_id}"
141+
end
142+
134143
case GradingClient.check_answer(module_id, question_id, result) do
135144
:correct ->
136145
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
@@ -143,18 +152,6 @@ case GradingClient.check_answer(module_id, question_id, result) do
143152
end
144153
```
145154

146-
<!-- livebook:{"output":true} -->
147-
148-
```
149-
Incorrect: Research MD5 Rainbow Tables
150-
```
151-
152-
<!-- livebook:{"output":true} -->
153-
154-
```
155-
:ok
156-
```
157-
158155
<!-- livebook:{"branch_parent_index":3} -->
159156

160157
## Injection
@@ -279,17 +276,9 @@ _Please change the atom below to the name of the vulnerable package installed in
279276

280277
_HINT: Check the changelogs for each dependency._
281278

282-
<!-- livebook:{"attrs":"eyJtb2R1bGVfaWQiOm51bGwsInF1ZXN0aW9uX2lkIjpudWxsLCJzb3VyY2UiOiJhbnN3ZXIgPSBcbiAgS2luby5JbnB1dC5zZWxlY3QoXCJBbnN3ZXJcIiwgW1xuICAgIHs6ZWN0bywgXCJFY3RvIHYyLjIuMlwifSxcbiAgICB7Om54LCBcIk54IHYwLjUuMFwifSxcbiAgICB7OnBsdWcsIFwiUGx1ZyB2MS4zLjJcIn1cbiAgXSlcblxuS2luby5yZW5kZXIoYW5zd2VyKVxuXG5LaW5vLklucHV0LnJlYWQoYW5zd2VyKSJ9","chunks":[[0,178],[180,631]],"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
279+
<!-- livebook:{"attrs":"eyJtb2R1bGVfaWQiOm51bGwsInF1ZXN0aW9uX2lkIjpudWxsLCJzb3VyY2UiOiIjT1dBU1A6MlxuYW5zd2VyID0gXG4gIEtpbm8uSW5wdXQuc2VsZWN0KFwiQW5zd2VyXCIsIFtcbiAgICB7OmVjdG8sIFwiRWN0byB2Mi4yLjJcIn0sXG4gICAgezpueCwgXCJOeCB2MC41LjBcIn0sXG4gICAgezpwbHVnLCBcIlBsdWcgdjEuMy4yXCJ9XG4gIF0pXG5cbktpbm8ucmVuZGVyKGFuc3dlcilcblxuS2luby5JbnB1dC5yZWFkKGFuc3dlcikifQ","chunks":null,"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
283280

284281
```elixir
285-
module_id = Kino.Input.select("Module", [{OWASP, "OWASP"}])
286-
question_id = Kino.Input.number("Question ID")
287-
Kino.render(Kino.Layout.grid([module_id, question_id], columns: 2))
288-
nil
289-
290-
module_id = Kino.Input.read(module_id)
291-
question_id = Kino.Input.read(question_id)
292-
293282
result =
294283
(
295284
answer =
@@ -303,6 +292,25 @@ result =
303292
Kino.Input.read(answer)
304293
)
305294

295+
[module_id, question_id] =
296+
"#OWASP:2\nanswer = \n Kino.Input.select(\"Answer\", [\n {:ecto, \"Ecto v2.2.2\"},\n {:nx, \"Nx v0.5.0\"},\n {:plug, \"Plug v1.3.2\"}\n ])\n\nKino.render(answer)\n\nKino.Input.read(answer)"
297+
|> String.split("\n", parts: 2)
298+
|> hd()
299+
|> String.trim_leading("#")
300+
|> String.split(":", parts: 2)
301+
302+
module_id =
303+
case %{"OWASP" => OWASP}[String.trim(module_id)] do
304+
nil -> raise "invalid module id: #{module_id}"
305+
module_id -> module_id
306+
end
307+
308+
question_id =
309+
case Integer.parse(String.trim(question_id)) do
310+
{id, ""} -> id
311+
_ -> raise "invalid question id: #{question_id}"
312+
end
313+
306314
case GradingClient.check_answer(module_id, question_id, result) do
307315
:correct ->
308316
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
@@ -315,18 +323,6 @@ case GradingClient.check_answer(module_id, question_id, result) do
315323
end
316324
```
317325

318-
<!-- livebook:{"output":true} -->
319-
320-
```
321-
Incorrect: Check the changelog for the next minor or major release.
322-
```
323-
324-
<!-- livebook:{"output":true} -->
325-
326-
```
327-
:ok
328-
```
329-
330326
<!-- livebook:{"branch_parent_index":3} -->
331327

332328
## Identification and Authentication Failures
@@ -457,18 +453,4 @@ case HTTPoison.get(user_inputted_url) do
457453
end
458454
```
459455

460-
<!-- livebook:{"output":true} -->
461-
462-
```
463-
This is the IP belonging to your Livebook instance:
464-
179.241.241.114
465-
466-
```
467-
468-
<!-- livebook:{"output":true} -->
469-
470-
```
471-
:ok
472-
```
473-
474456
[**<- Previous Module: Introduction**](./1-introduction.livemd) || [**Next Module: Secure SDLC Concepts ->**](./3-ssdlc.livemd)

modules/grading_client/lib/grading_client/graded_cell.ex

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ defmodule GradingClient.GradedCell do
1111
editor: [source: source, language: "elixir"]}
1212
end
1313

14-
@impl true
15-
def handle_connect(ctx) do
16-
{:ok, %{}, ctx}
17-
end
18-
1914
@impl true
2015
def handle_editor_change(source, ctx) do
2116
{:ok, assign(ctx, source: source)}
@@ -32,29 +27,41 @@ defmodule GradingClient.GradedCell do
3227

3328
@impl true
3429
def to_source(attrs) do
35-
dbg(attrs)
36-
37-
options = Enum.map(GradingClient.Answers.get_modules(), &{&1, inspect(&1)})
38-
39-
inputs =
40-
quote do
41-
module_id = Kino.Input.select("Module", unquote(options))
42-
question_id = Kino.Input.number("Question ID")
43-
44-
Kino.render(Kino.Layout.grid([module_id, question_id], columns: 2))
45-
nil
46-
end
30+
modules = Map.new(GradingClient.Answers.get_modules(), &{inspect(&1), &1})
4731

4832
source_ast =
4933
try do
50-
source = Code.string_to_quoted!(attrs["source"])
34+
source_attr = attrs["source"]
35+
source = Code.string_to_quoted!(source_attr)
5136

5237
quote do
53-
module_id = Kino.Input.read(module_id)
54-
question_id = Kino.Input.read(question_id)
55-
5638
result = unquote(source)
5739

40+
[module_id, question_id] =
41+
unquote(source_attr)
42+
|> String.split("\n", parts: 2)
43+
|> hd()
44+
|> String.trim_leading("#")
45+
|> String.split(":", parts: 2)
46+
47+
module_id =
48+
case unquote(Macro.escape(modules))[String.trim(module_id)] do
49+
nil ->
50+
raise "invalid module id: #{module_id}"
51+
52+
module_id ->
53+
module_id
54+
end
55+
56+
question_id =
57+
case Integer.parse(String.trim(question_id)) do
58+
{id, ""} ->
59+
id
60+
61+
_ ->
62+
raise "invalid question id: #{question_id}"
63+
end
64+
5865
case GradingClient.check_answer(module_id, question_id, result) do
5966
:correct ->
6067
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
@@ -72,10 +79,7 @@ defmodule GradingClient.GradedCell do
7279
{:<<>>, [delimiter: ~s["""]], [attrs["source"] <> "\n"]}
7380
end
7481

75-
[
76-
Kino.SmartCell.quoted_to_string(inputs),
77-
Kino.SmartCell.quoted_to_string(source_ast)
78-
]
82+
Kino.SmartCell.quoted_to_string(source_ast)
7983
end
8084

8185
@impl true

0 commit comments

Comments
 (0)