Browse Source

spam protection improved

master
Christian Mueller 11 years ago
parent
commit
6759abd466
  1. 22
      src/notehub/handler.clj
  2. 10
      test/notehub/test/handler.clj

22
src/notehub/handler.clj

@ -14,6 +14,9 @@
[notehub.storage :as storage] [notehub.storage :as storage]
[cheshire.core :refer :all])) [cheshire.core :refer :all]))
(defn current-timestamp []
(quot (System/currentTimeMillis) 100000000))
; note page cache ; note page cache
(def C (atom (cache/lru-cache-factory {}))) (def C (atom (cache/lru-cache-factory {})))
@ -69,7 +72,10 @@
(GET "/:year/:month/:day/:title/edit" [year month day title] (GET "/:year/:month/:day/:title/edit" [year month day title]
(note-update-page year month day title)) (note-update-page year month day title))
(GET "/new" [] (new-note-page (storage/sign (str (rand-int Integer/MAX_VALUE))))) (GET "/new" [] (new-note-page
(str
(current-timestamp)
(storage/sign (rand-int Integer/MAX_VALUE)))))
(GET "/:year/:month/:day/:title" [year month day title :as params] (GET "/:year/:month/:day/:title" [year month day title :as params]
(let [params (assoc (:query-params params) (let [params (assoc (:query-params params)
@ -93,15 +99,19 @@
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
(redirect long-url)))) (redirect long-url))))
(POST "/post-note" [session note signature password] (POST "/post-note" [session note signature password]
(if (= signature (storage/sign session note)) (if (and session
(.startsWith session
(str (current-timestamp)))
(= signature (storage/sign session note)))
(let [pid "NoteHub" (let [pid "NoteHub"
psk (storage/get-psk pid) psk (storage/get-psk pid)
params {:session session :note note :signature signature params {:note note
:password password :pid pid}] :pid pid
:signature (storage/sign pid psk note)
:password password}]
(if (storage/valid-publisher? pid) (if (storage/valid-publisher? pid)
(let [resp (api/post-note (assoc params :signature (storage/sign pid psk note)))] (let [resp (api/post-note params)]
(if (get-in resp [:status :success]) (if (get-in resp [:status :success])
(redirect (:longURL resp)) (redirect (:longURL resp))
(response 400))) (response 400)))

10
test/notehub/test/handler.clj

@ -10,6 +10,7 @@
(def date [2012 6 3]) (def date [2012 6 3])
(def test-title "some-title") (def test-title "some-title")
(def test-note "# This is a test note.\nHello _world_. Motörhead, тест.") (def test-note "# This is a test note.\nHello _world_. Motörhead, тест.")
(def session-key (str (quot (System/currentTimeMillis) 100000000) "somemd5hash"))
(defn create-testnote-fixture [f] (defn create-testnote-fixture [f]
(add-note (build-key date test-title) test-note "testPID") (add-note (build-key date test-title) test-note "testPID")
@ -30,8 +31,7 @@
(is (= (:body (send-request (url 2012 6 3 "some-title" "export"))) test-note)))) (is (= (:body (send-request (url 2012 6 3 "some-title" "export"))) test-note))))
(deftest note-creation (deftest note-creation
(let [session-key "somemd5hash" (let [date (get-date)
date (get-date)
title "this-is-a-test-note" title "this-is-a-test-note"
[year month day] date] [year month day] date]
(testing "Note creation" (testing "Note creation"
@ -49,8 +49,7 @@
(not (note-exists? (build-key date title))))))))) (not (note-exists? (build-key date title)))))))))
(deftest note-creation-utf (deftest note-creation-utf
(let [session-key "somemd5hash" (let [date (get-date)
date (get-date)
title "радуга" title "радуга"
note "# Радуга\nкаждый охотник желает знать, где сидят фазаны." note "# Радуга\nкаждый охотник желает знать, где сидят фазаны."
[year month day] date] [year month day] date]
@ -68,8 +67,7 @@
(not (note-exists? (build-key date title)))))))) (not (note-exists? (build-key date title))))))))
(deftest note-update (deftest note-update
(let [session-key "somemd5hash" (let [date (get-date)
date (get-date)
title "this-is-a-test-note" title "this-is-a-test-note"
[year month day] date [year month day] date
hash (sign session-key test-note)] hash (sign session-key test-note)]

Loading…
Cancel
Save