Skip to content

Commit d3b694c

Browse files
committed
feat: add missing answers
1 parent 3be647b commit d3b694c

File tree

3 files changed

+123
-18
lines changed

3 files changed

+123
-18
lines changed

modules/6-cookies.livemd

+50-12
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,57 @@ In the Phoenix Framework, you would use functionality found within the [Plug lib
181181

182182
_Fill out the `put_resp_cookie/4` function arguments with the settings outlined in the previous section, no other code changes should be necessary._
183183

184+
<!-- livebook:{"attrs":"eyJzb3VyY2UiOiIjIENPT0tJRV9TRUNVUklUWToxIFxuXG5jb29raWVfbmFtZSA9IFwiQ0hBTkdFX01FXCJcblxuIyBVbmNvbW1lbnQgYW5kIGNoYW5nZSB0aGUgcHV0X3Jlc3BfY29va2llIGNhbGwgYmVsb3dcbiMgY29ubiA9XG4jICAgUGx1Zy5Db25uLnB1dF9yZXNwX2Nvb2tpZShcbiMgICAgIGNvbm4sXG4jICAgICBjb29raWVfbmFtZSxcbiMgICAgIDw8MDo6OCwgNDI6Ojg+PixcbiMgICAgIGRvbWFpbjogLi4uLFxuIyAgICAgcGF0aDogLi4uLFxuIyAgICAgc2VjdXJlOiAuLi4sXG4jICAgICBodHRwX29ubHk6IC4uLixcbiMgICAgIHNhbWVfc2l0ZTogLi4uXG4jICAgKVxuXG5jb29raWUgPSBcbiAgY29ublxuICB8PiBQbHVnLkNvbm4uZmV0Y2hfY29va2llcygpXG4gIHw+IFBsdWcuQ29ubi5nZXRfcmVzcF9jb29raWVzKClcbiAgfD4gTWFwLmZldGNoIShjb29raWVfbmFtZSlcblxue2Nvb2tpZSwgYmluYXJ5X3BhcnQoY29va2llX25hbWUsIDAsIDYpfSJ9","chunks":null,"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
185+
184186
```elixir
185-
cookie_name = "CHANGE_ME_TOO"
186-
187-
conn
188-
|> Plug.Conn.put_resp_cookie(
189-
cookie_name,
190-
<<42::16>>
191-
# domain: ,
192-
# path: ,
193-
# secure: ,
194-
# http_only: ,
195-
# same_site:
196-
)
187+
result =
188+
(
189+
cookie_name = "CHANGE_ME"
190+
191+
cookie =
192+
conn
193+
|> Plug.Conn.fetch_cookies()
194+
|> Plug.Conn.get_resp_cookies()
195+
|> Map.fetch!(cookie_name)
196+
197+
{cookie, binary_part(cookie_name, 0, 6)}
198+
)
199+
200+
[module_id, question_id] =
201+
"# COOKIE_SECURITY:1 \n\ncookie_name = \"CHANGE_ME\"\n\n# Uncomment and change the put_resp_cookie call below\n# conn =\n# Plug.Conn.put_resp_cookie(\n# conn,\n# cookie_name,\n# <<0::8, 42::8>>,\n# domain: ...,\n# path: ...,\n# secure: ...,\n# http_only: ...,\n# same_site: ...\n# )\n\ncookie = \n conn\n |> Plug.Conn.fetch_cookies()\n |> Plug.Conn.get_resp_cookies()\n |> Map.fetch!(cookie_name)\n\n{cookie, binary_part(cookie_name, 0, 6)}"
202+
|> String.split("\n", parts: 2)
203+
|> hd()
204+
|> String.trim_leading("#")
205+
|> String.split(":", parts: 2)
206+
207+
module_id =
208+
case %{
209+
"COOKIE_SECURITY" => COOKIE_SECURITY,
210+
"ELIXIR_SECURITY" => ELIXIR_SECURITY,
211+
"GRAPHQL" => GRAPHQL,
212+
"OWASP" => OWASP,
213+
"SDLC" => SDLC
214+
}[String.trim(module_id)] do
215+
nil -> raise "invalid module id: #{module_id}"
216+
module_id -> module_id
217+
end
218+
219+
question_id =
220+
case Integer.parse(String.trim(question_id)) do
221+
{id, ""} -> id
222+
_ -> raise "invalid question id: #{question_id}"
223+
end
224+
225+
case GradingClient.check_answer(module_id, question_id, result) do
226+
:correct ->
227+
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
228+
229+
{:incorrect, help_text} when is_binary(help_text) ->
230+
IO.puts([IO.ANSI.red(), "Incorrect: ", IO.ANSI.reset(), help_text])
231+
232+
_ ->
233+
IO.puts([IO.ANSI.red(), "Incorrect.", IO.ANSI.reset()])
234+
end
197235
```
198236

199237
## Data Privacy For Cookies

modules/7-anti-patterns.livemd

+51-5
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,59 @@ Penguin.slide([3, 4, 5, 2, 1])
7575

7676
_Uncomment the line with your answer._
7777

78+
<!-- livebook:{"attrs":"eyJzb3VyY2UiOiIjIEFOVElQQVRURVJOUzoxXG5cbmlucHV0ID0gS2luby5JbnB1dC5zZWxlY3QoXCJBbnN3ZXJcIiwgW1xuICBhOiBcIkJ1YmJsZSBTb3J0XCIsXG4gIGI6IFwiTWVyZ2UgU29ydFwiLFxuICBjOiBcIlF1aWNrIFNvcnRcIixcbiAgZDogXCJSYW5kb20gU29ydFwiXG5dKVxuXG5LaW5vLnJlbmRlcihpbnB1dClcblxuS2luby5JbnB1dC5yZWFkKGlucHV0KSJ9","chunks":null,"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
79+
7880
```elixir
79-
# answer = :bubble_sort
80-
# answer = :merge_sort
81-
# answer = :quick_sort
82-
# answer = :random_sort
81+
result =
82+
(
83+
input =
84+
Kino.Input.select("Answer",
85+
a: "Bubble Sort",
86+
b: "Merge Sort",
87+
c: "Quick Sort",
88+
d: "Random Sort"
89+
)
90+
91+
Kino.render(input)
92+
Kino.Input.read(input)
93+
)
94+
95+
[module_id, question_id] =
96+
"# ANTIPATTERNS:1\n\ninput = Kino.Input.select(\"Answer\", [\n a: \"Bubble Sort\",\n b: \"Merge Sort\",\n c: \"Quick Sort\",\n d: \"Random Sort\"\n])\n\nKino.render(input)\n\nKino.Input.read(input)"
97+
|> String.split("\n", parts: 2)
98+
|> hd()
99+
|> String.trim_leading("#")
100+
|> String.split(":", parts: 2)
101+
102+
module_id =
103+
case %{
104+
"ANTIPATTERNS" => ANTIPATTERNS,
105+
"COOKIE_SECURITY" => COOKIE_SECURITY,
106+
"ELIXIR_SECURITY" => ELIXIR_SECURITY,
107+
"GRAPHQL" => GRAPHQL,
108+
"OWASP" => OWASP,
109+
"SDLC" => SDLC
110+
}[String.trim(module_id)] do
111+
nil -> raise "invalid module id: #{module_id}"
112+
module_id -> module_id
113+
end
114+
115+
question_id =
116+
case Integer.parse(String.trim(question_id)) do
117+
{id, ""} -> id
118+
_ -> raise "invalid question id: #{question_id}"
119+
end
83120

84-
IO.puts(answer)
121+
case GradingClient.check_answer(module_id, question_id, result) do
122+
:correct ->
123+
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
124+
125+
{:incorrect, help_text} when is_binary(help_text) ->
126+
IO.puts([IO.ANSI.red(), "Incorrect: ", IO.ANSI.reset(), help_text])
127+
128+
_ ->
129+
IO.puts([IO.ANSI.red(), "Incorrect.", IO.ANSI.reset()])
130+
end
85131
```
86132

87133
## Frontend Authorization Checks

modules/grading_client/priv/answers.exs

+22-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,30 @@ elixir_security_questions = [
6969
}
7070
]
7171

72+
cookie_security_questions = [
73+
%{
74+
question_id: 1,
75+
answer: {
76+
%{value: <<0, 42>>, path: "/", secure: true, http_only: true, same_site: "Strict"},
77+
"__Host"
78+
},
79+
help_text: "Read the section about the __Host prefix."
80+
}
81+
]
82+
83+
antipatterns_questions = [
84+
%{
85+
question_id: 1,
86+
answer: :c,
87+
help_text: "Look-up the pseudocode for each algorithm."
88+
}
89+
]
90+
7291
List.flatten([
7392
to_answers.(OWASP, owasp_questions),
7493
to_answers.(SDLC, sdlc_questions),
7594
to_answers.(GRAPHQL, graphql_questions),
76-
to_answers.(ELIXIR_SECURITY, elixir_security_questions)
95+
to_answers.(ELIXIR_SECURITY, elixir_security_questions),
96+
to_answers.(COOKIE_SECURITY, cookie_security_questions),
97+
to_answers.(ANTIPATTERNS, antipatterns_questions)
7798
])

0 commit comments

Comments
 (0)