Skip to content

Commit 53983f9

Browse files
committed
Pass CSRF token using Sec-WebSocket-Protocol header
The proposed change tries to respect other values in the Sec-WebSocket-Protocol header, that the caller may have set in the `headers` option when calling `make-channel-socket-client!` [Re: #418]
1 parent e4cba96 commit 53983f9

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/taoensso/sente.cljc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,8 @@
13161316
(enc/oget @?node-npm-websocket_ "w3cwebsocket"))]
13171317

13181318
(delay
1319-
(let [socket (WebSocket. uri-str)]
1319+
(let [protocols (:sec-websocket-protocol headers)
1320+
socket (WebSocket. uri-str protocols)]
13201321
(doto socket
13211322
(aset "onerror" on-error)
13221323
(aset "onmessage" on-message) ; Nb receives both push & cb evs!
@@ -1569,13 +1570,19 @@
15691570
{:on-error on-error
15701571
:on-message on-message
15711572
:on-close on-close
1572-
:headers headers
1573+
:headers (update headers :sec-websocket-protocol
1574+
(fn [x]
1575+
(let [csrf-token (str "sente-csrf-token-"
1576+
(get-client-csrf-token-str :dynamic
1577+
(:csrf-token @state_)))]
1578+
(cond
1579+
(string? x) [x csrf-token]
1580+
(coll? x) (conj x csrf-token)
1581+
:else csrf-token))))
15731582
:uri-str
15741583
(enc/merge-url-with-query-string url
15751584
(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_))}))}))
1585+
{:client-id client-id}))}))
15791586

15801587
(catch #?(:clj Throwable :cljs :default) t
15811588
(timbre/errorf t "Error creating WebSocket client")

0 commit comments

Comments
 (0)