Skip to content

Commit 6d1447f

Browse files
committed
Improved iq-quests-raw.
Most notably, we now check the downloaded IQ quests against a hash.
1 parent 7c89b51 commit 6d1447f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

simalq/un_iq.hy

+13-9
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,25 @@
149149
(.mkdir cache-dir :parents T :exist-ok T)
150150
(setv path (/ cache-dir "infinity_quests_2.zip"))
151151
(unless (.exists path)
152-
(import http.client contextlib)
153-
(with [con (contextlib.closing (http.client.HTTPConnection "arfer.net"))]
152+
(import http.client contextlib hashlib)
153+
(setv data (with [con
154+
(contextlib.closing (http.client.HTTPConnection "arfer.net"))]
154155
(.request con "GET" "/downloads/infinity_quests_2.zip"
155156
:headers {"User-Agent" "Infinitesimal Quest 2 + epsilon"})
156157
(setv r (.getresponse con))
157-
(assert (= r.status 200))
158-
(.write-bytes path (.read r))))
158+
(when (!= r.status 200)
159+
(raise (ValueError f"The HTTP request to get IQ quests returned status code {r.status}")))
160+
(.read r)))
161+
(setv value (.hexdigest (hashlib.sha512 data)))
162+
(unless (= value "044b0c16fcc3bda2869d5b97ecaf32b39177988e510230e44456a33485c2df4e929443b9765463654961b7190ecf431a6871417a52c81e81c61f089d2793ab5d")
163+
(raise (ValueError f"Bad SHA-512 hash for IQ quests: {value}")))
164+
(.write-bytes path data))
159165

160166
; Read the files from the archive.
161-
(setv prefix "infinity_quests_2/")
162167
(with [z (ZipFile path "r")] (dfor
163-
q (.namelist z)
164-
:setv v (.read z q)
165-
:if v
166-
(.removeprefix q prefix) v)))
168+
member (.namelist z)
169+
:if (not (.endswith member "/"))
170+
(.removeprefix member "infinity_quests_2/") (.read z member))))
167171

168172
(defn read-quest [name inp]
169173
"Parse `bytes` into a `Quest`."

0 commit comments

Comments
 (0)