|
369 | 369 | (allow-origin? #{"http://site.com"} {:headers {"referer" "http://attacker.com/"}}) |
370 | 370 | (allow-origin? #{"http://site.com"} {:headers {"referer" "http://site.com.attacker.com/"}})) |
371 | 371 |
|
372 | | -(def ^:private sente-csrf-token-prefix "sente-csrf-token-") |
373 | | - |
374 | 372 | (defn make-channel-socket-server! |
375 | 373 | "Takes a web server adapter[1] and returns a map with keys: |
376 | 374 |
|
|
635 | 633 | ;; undefined): |
636 | 634 | nil) |
637 | 635 |
|
638 | | - sente-csrf-token-pred |
639 | | - (fn [s] |
640 | | - (when (str/starts-with? s sente-csrf-token-prefix) |
641 | | - (subs s (count sente-csrf-token-prefix)))) |
642 | | - |
643 | | - ws-csrf-token |
644 | | - (fn [ring-req] |
645 | | - (let [headers (get ring-req :headers)] |
646 | | - (when-let [ws? (= "websocket" (get headers "upgrade") )] |
647 | | - (let [sec-websocket-protocol (get headers "sec-websocket-protocol") |
648 | | - protocol-vals |
649 | | - (when (string? sec-websocket-protocol) |
650 | | - (str/split sec-websocket-protocol #", *"))] |
651 | | - (enc/rsome sente-csrf-token-pred protocol-vals))))) |
652 | | - |
653 | 636 | bad-csrf? |
654 | 637 | (fn [ring-req] |
655 | 638 | (if (nil? csrf-token-fn) ; Provides a way to disable CSRF check |
|
659 | 642 | (or |
660 | 643 | (get-in ring-req [:params :csrf-token]) |
661 | 644 | (get-in ring-req [:headers "x-csrf-token"]) |
662 | | - (get-in ring-req [:headers "x-xsrf-token"]) |
663 | | - (ws-csrf-token ring-req))] |
| 645 | + (get-in ring-req [:headers "x-xsrf-token"]))] |
664 | 646 |
|
665 | 647 | (not |
666 | 648 | (enc/const-str= |
|
1334 | 1316 | (enc/oget @?node-npm-websocket_ "w3cwebsocket"))] |
1335 | 1317 |
|
1336 | 1318 | (delay |
1337 | | - (let [protocols (get headers :sec-websocket-protocol) |
1338 | | - socket (WebSocket. uri-str protocols)] |
| 1319 | + (let [socket (WebSocket. uri-str)] |
1339 | 1320 | (doto socket |
1340 | 1321 | (aset "onerror" on-error) |
1341 | 1322 | (aset "onmessage" on-message) ; Nb receives both push & cb evs! |
|
1588 | 1569 | {:on-error on-error |
1589 | 1570 | :on-message on-message |
1590 | 1571 | :on-close on-close |
1591 | | - :headers |
1592 | | - (update headers :sec-websocket-protocol |
1593 | | - (fn [x] |
1594 | | - (let [csrf-token |
1595 | | - (str sente-csrf-token-prefix |
1596 | | - (get-client-csrf-token-str :dynamic (:csrf-token @state_)))] |
1597 | | - (cond |
1598 | | - (string? x) [x csrf-token] |
1599 | | - (coll? x) (conj x csrf-token) |
1600 | | - :else csrf-token)))) |
1601 | | - |
| 1572 | + :headers headers |
1602 | 1573 | :uri-str |
1603 | 1574 | (enc/merge-url-with-query-string url |
1604 | | - (assoc params :client-id client-id))})) |
| 1575 | + (merge params ; 1st (don't clobber impl.): |
| 1576 | + {:client-id client-id |
| 1577 | + :csrf-token (get-client-csrf-token-str :dynamic |
| 1578 | + (:csrf-token @state_))}))})) |
1605 | 1579 |
|
1606 | 1580 | (catch #?(:clj Throwable :cljs :default) t |
1607 | 1581 | (timbre/errorf t "Error creating WebSocket client") |
|
0 commit comments