Debian Codegraph @

lib/rfc/cookie.scm

raw
1 ;;;
2 ;;; cookie.scm - parse and construct http state information
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Parser and constructor of http "Cookies" defined in
35 ;; RFC 6265 HTTP state managemnet mechanism
36 ;; http://tools.ietf.org/html/rfc6265
37 ;; See also
38 ;; RFC 2964 Use of HTTP state management
39 ;; <ftp://ftp.isi.edu/in-notes/rfc2964.txt>
40 ;; The parser also supports the old Netscape spec
41 ;; <http://www.netscape.com/newsref/std/cookie_spec.html>
42
43 (define-module rfc.cookie
44 (use scheme.list)
45 (use gauche.threads)
46 (use gauche.sequence)
47 (use srfi.13)
48 (use srfi.19)
49 (use util.match)
50 (export parse-cookie-string
51 construct-cookie-string
52
53 parse-cookie-date
54
55 make-cookie-jar
56 cookie-jar-put!
57 cookie-jar-get
58 cookie-jar-purge-ephemeral!
59 )
60 )
61 (select-module rfc.cookie)
62
63 ;;==============================================================
64 ;; Cookie header parser and constructor
65 ;;
66
67 ;; utility fn. breaks ``attr=value;attr=value ... '' into alist.
68 ;; version is a cookie version. if version>0, we allow comma as the
69 ;; delimiter as well as semicolon.
70 (define (parse-av-pairs input version)
71 (define attr-regexp
72 (if (= version 0)
73 #/\s*([\w$_.-]+)\s*([=\;]\s*)?/
74 #/\s*([\w$_.-]+)\s*([=\;,]\s*)?/))
75 (define attr-delim
76 (if (= version 0) #\; #[,\;]))
77
78 (define (read-attr input r)
79 (cond [(string-null? input) (reverse! r)]
80 [(rxmatch attr-regexp input)
81 => (^m (if (and-let* ([delimiter (rxmatch-substring m 2)])
82 (string-prefix? "=" delimiter))
83 (let ([attr (rxmatch-substring m 1)]
84 [rest (rxmatch-after m)])
85 (if (string-prefix? "\"" rest)
86 (read-token-quoted attr (string-drop rest 1) r)
87 (read-token attr rest r)))
88 (read-attr (rxmatch-after m)
89 (acons (rxmatch-substring m 1) #f r))))]
90 [else
91 ;; the input is broken; for now, we ignore the rest.
92 (reverse! r)]))
93 (define (read-token attr input r)
94 (cond [(string-index input attr-delim)
95 => (^i (read-attr (string-drop input (+ i 1))
96 (acons attr
97 (string-trim-right (string-take input i))
98 r)))]
99 [else
100 (reverse! (acons attr (string-trim-right input) r))]))
101 (define (read-token-quoted attr input r)
102 (let loop ([input input]
103 [partial '()])
104 (cond ([string-index input #[\\\"]]
105 => (^i (let1 c (string-ref input i)
106 (if (char=? c #\\)
107 (if (< (string-length input) (+ i 1))
108 (error-unterminated attr)
109 (loop (string-drop input (+ i 2))
110 (list* (string (string-ref input (+ i 1)))
111 (string-take input i)
112 partial)))
113 (read-attr (string-drop input (+ i 1))
114 (acons attr
115 (string-concatenate-reverse
116 (cons (string-take input i)
117 partial))
118 r))))))
119 [else (error-unterminated attr)])))
120 (define (error-unterminated attr)
121 (error "Unterminated quoted value given for attribute" attr))
122
123 (read-attr input '()))
124
125 ;; Parses the header value of "Cookie" request header.
126 ;; If cookie version is known by "Cookie2" request header, it should
127 ;; be passed to version (as integer). Otherwise, it figures out
128 ;; the cookie version from input.
129 ;;
130 ;; Returns the following format.
131 ;; ((<name> <value> [:path <path>] [:domain <domain>] [:port <port>])
132 ;; ...)
133
134 (define (parse-cookie-string input . version)
135 (let1 ver (cond [(and (pair? version) (integer? (car version)))
136 (car version)]
137 [(rxmatch #/^\s*$Version\s*=\s*(\d+)/ input)
138 => (^m (string->number (rxmatch-substring m 1)))]
139 [else 0])
140 (let loop ([av-pairs (parse-av-pairs input ver)]
141 [r '()]
142 [current '()])
143 (cond [(null? av-pairs)
144 (if (null? current)
145 (reverse r)
146 (reverse (cons (reverse current) r)))]
147 [(string-ci=? "$path" (caar av-pairs))
148 (loop (cdr av-pairs) r (list* (cdar av-pairs) :path current))]
149 [(string-ci=? "$domain" (caar av-pairs))
150 (loop (cdr av-pairs) r (list* (cdar av-pairs) :domain current))]
151 [(string-ci=? "$port" (caar av-pairs))
152 (loop (cdr av-pairs) r (list* (cdar av-pairs) :port current))]
153 [else
154 (if (null? current)
155 (loop (cdr av-pairs) r (list (cdar av-pairs) (caar av-pairs)))
156 (loop (cdr av-pairs)
157 (cons (reverse current) r)
158 (list (cdar av-pairs) (caar av-pairs))))]))
159 ))
160
161 ;; Construct a cookie string suitable for Set-Cookie or Set-Cookie2 header.
162 ;; specs is the following format.
163 ;;
164 ;; ((<name> <value> <key> <val> ...) ...)
165 ;;
166 ;; Recognized options:
167 ;; :expires <time-like>
168 ;; The value can be a <date>, a <time>, integer unix seconds,
169 ;; or preformatted datetime string.
170 ;; :max-age <nonnegative-integer>
171 ;; :domain <domain>
172 ;; :path <path>
173 ;; :secure <boolean>
174 ;; :http-only <boolean>
175 ;;
176 ;; The following options are supported for the backward compatibility:
177 ;; :comment <string>
178 ;; :comment-url <url>
179 ;; :discard <boolean>
180 ;; :port <port-list>
181 ;; :version <version>
182 ;;
183 ;; Returns a list of cookie strings for each <name>=<value> pair. In the
184 ;; ``new cookie'' implementation, you can join them by comma and send it
185 ;; at once with Set-cookie2 header. For the old netscape protocol, you
186 ;; must send each of them by Set-cookie header.
187
188 (define (construct-cookie-string specs :optional version_ignore)
189 (map construct-cookie-string-1 specs))
190
191 (define (construct-cookie-string-1 spec)
192 (when (< (length spec) 2)
193 (error "bad cookie spec: at least <name> and <value> required" spec))
194 (let ([name (car spec)]
195 [value (cadr spec)])
196 (let loop ([attr (cddr spec)]
197 [r (list (if value
198 (string-append name "="
199 (quote-if-needed value))
200 name))])
201 (define (next s) (loop (cddr attr) (cons s r)))
202 (define (ignore) (loop (cddr attr) r))
203 (cond
204 [(null? attr) (string-join (reverse r) ";")]
205 [(null? (cdr attr))
206 (errorf "bad cookie spec: attribute ~s requires value" (car attr))]
207 [(eqv? :expires (car attr))
208 (next (make-expires-attr (cadr attr)))]
209 [(eqv? :max-age (car attr))
210 (next (format #f "Max-Age=~a" (cadr attr)))]
211 [(eqv? :domain (car attr))
212 (next (string-append "Domain=" (cadr attr)))]
213 [(eqv? :path (car attr))
214 (next (string-append "Path=" (quote-if-needed (cadr attr))))]
215 [(eqv? :secure (car attr))
216 (if (cadr attr) (next "Secure") (ignore))]
217 [(eqv? :http-only (car attr))
218 (if (cadr attr) (next "HttpOnly") (ignore))]
219 ;; Below are for the backward compatibility:
220 [(eqv? :comment (car attr))
221 (next (string-append "Comment=" (quote-if-needed (cadr attr))))]
222 [(eqv? :comment-url (car attr))
223 (next (string-append "CommentURL=" (quote-value (cadr attr))))]
224 [(eqv? :discard (car attr))
225 (if (cadr attr) (next "Discard") (ignore))]
226 [(eqv? :port (car attr))
227 (next (string-append "Port=" (quote-value (cadr attr))))]
228 [(eqv? :version (car attr))
229 (next (format #f "Version=~a" (cadr attr)))]
230 [else (error "Unknown cookie attribute" (car attr))])
231 ))
232 )
233
234 ;; aux. function to quote value
235 (define (quote-value value)
236 (string-append "\"" (regexp-replace-all #/\"|\\/ value "\\\\\\0") "\""))
237
238 (define (quote-if-needed value)
239 (if (rxmatch #/[\",\;\\ \t\n]/ value)
240 (quote-value value)
241 value))
242
243 (define (make-expires-attr time)
244 (define (ensure-time-string time)
245 (cond
246 [(number? time)
247 (sys-strftime "%a, %d-%b-%Y %H:%M:%S GMT" (sys-gmtime time))]
248 [(is-a? time <date>)
249 (date->string time "~a, ~d-~@b-~Y ~H:~M:~S GMT")]
250 [(is-a? time <time>)
251 (case (time-type time)
252 [(time-utc) (ensure-time-string (time-utc->date time 0))]
253 [(time-tai) (ensure-time-string (time-tai->date time 0))]
254 [(time-monotonic) (ensure-time-string (time-monotonic->date time 0))]
255 [else (errorf "Don't know how to convert a time object ~s to string."
256 time)])]
257 [else time]))
258
259 (format #f "Expires=~a" (ensure-time-string time)))
260
261 ;;==============================================================
262 ;; Cookie-jar, a client-side storage of cookies used by <http-connection>
263 ;;
264
265 ;; From outside, <cookie-jar> should be treated as an opaque object.
266 ;; The opeartions on <cookie-jar> is thread-safe, so that it can easily
267 ;; be shared by multiple connections.
268
269 ;; Client-side cookie representation. Immutable.
270 ;;
271 ;; Max-Age is converted to the absolute time when the cookie shall be
272 ;; discarded.
273 ;;
274 ;; request-host is not an attribute in the cookie string, but should be
275 ;; passed by the caller in client side to set which host the cookie
276 ;; string is sent from. It is used only when the cookie is missing Domain
277 ;; attribute.
278 (define-class <http-cookie> ()
279 ((name :init-keyword :name)
280 (value :init-keyword :value)
281 (domain :init-keyword :domain :init-value #f) ;lower case
282 (request-host :init-keyword :source-domain :init-value #f)
283 (path :init-keyword :path :init-value #f)
284 (lifetime :init-keyword :lifetime :init-value #f) ; #f or <time>
285 (port :init-keyword :port :init-value '()) ; list of port numbers
286 (secure :init-keyword :secure :init-value #f)
287 (version :init-keyword :version :init-value 1)
288 (comment :init-keyword :comment :init-value #f)
289 (comment-url :init-keyword :comment-url :init-value #f)
290 ))
291
292 ;; For now, we just use a linear list. May change if performance becomes
293 ;; the issue. The user should treat <cookie-jar> an opaque object.
294 (define-class <cookie-jar> (<collection>)
295 ((%table :init-keyword :%table))) ;private
296
297
298 ;; RFC6265 5.1.1
299 (define (parse-cookie-date str)
300 (let loop ([tokens ($ string-split str
301 #[\x09\x20-\x2f\x3b-\x40\x5b-\x60\x7b-\x7e])]
302 [hour #f]
303 [minute #f]
304 [second #f]
305 [day #f]
306 [month #f]
307 [year #f])
308 (cond [(null? tokens)
309 (and hour minute second day month year
310 (<= 0 hour 23)
311 (<= 0 minute 59)
312 (<= 0 second 59)
313 (<= 1 day 31)
314 (<= 1601 year)
315 (make-date 0 second minute hour day month year 0))] ;UTC
316 [(#/^(\d{1,2}):(\d{1,2}):(\d{1,2})\D?/ (car tokens))
317 => (^m (if hour
318 (loop (cdr tokens) hour minute second day month year)
319 (loop (cdr tokens)
320 (string->number (m 1))
321 (string->number (m 2))
322 (string->number (m 3))
323 day month year)))]
324 [(#/^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i (car tokens))
325 => (^m (loop
326 (cdr tokens) hour minute second day
327 (or month
328 (find-index (cute string-ci=? <> (m 1))
329 '("" "jan" "feb" "mar" "apr" "may" "jun"
330 "jul" "aug" "sep" "oct" "nov" "dec")))
331 year))]
332 [(#/^\d{1,4}/ (car tokens))
333 => (^m (let1 n (string->number (m 0))
334 (cond [(< n 10)
335 (loop (cdr tokens) hour minute second
336 (or day n) month year)]
337 [(<= 10 n 99)
338 (if day
339 (loop (cdr tokens) hour minute second day
340 month (or year
341 (if (<= 70 n)
342 (+ 1900 n)
343 (+ 2000 n))))
344 (loop (cdr tokens) hour minute second
345 n month year))]
346 [else
347 (loop (cdr tokens) hour minute second day
348 month (or year n))])))]
349 [else (loop (cdr tokens) hour minute second day month year)])))
350
351
352 (define (%put-cookie! jar cookie)
353 ;; ok, this isn't ideal, but just for now...
354 ($ atomic-update! (~ jar'%table)
355 (^[table]
356 (cons cookie
357 (remove (^e (and (equal? (~ e'domain) (~ cookie'domain))
358 (equal? (~ e'name) (~ cookie'name))))
359 table)))))
360
361 (define (%remove-cookie! jar domain name)
362 ($ atomic-update! (~ jar'%table)
363 (^[table] (remove (^e (and (equal? (~ e'domain) domain)
364 (equal? (~ e'name) name)))
365 table))))
366
367 (define (%compute-lifetime max-age expires)
368 ;; max-age has precedence (RFC6265 4.1.2.2)
369 (cond [(and-let* ([ max-age ]
370 [secs (string->number max-age)])
371 (if (< secs 0)
372 (make-time time-utc 0 0)
373 (add-duration (current-time)
374 (make-time time-duration 0 secs))))]
375 [expires (date->time-utc (parse-cookie-date expires))]
376 [else #f]))
377
378 ;; 5.1.4
379 (define (%default-path uri-path)
380 (if (or (equal? uri-path "")
381 (not (eqv? (~ uri-path 0) #\/)))
382 "/"
383 (let1 xs (drop-right (string-split uri-path "/" 'prefix) 1)
384 (if (null? xs)
385 "/"
386 (string-join xs "/" 'prefix)))))
387
388 ;; 5.1.4
389 (define (%path-match request-path cookie-path)
390 (or (equal? cookie-path request-path)
391 (and (string-prefix? cookie-path request-path)
392 (or (boolean (#/\/$/ cookie-path))
393 (eqv? (string-ref request-path (string-length cookie-path))
394 #\/)))))
395
396
397 (define (%domain-belongs-to? sub parent)
398 (let loop ([sub-components (reverse (string-split sub #\.))]
399 [par-components (reverse (string-split parent #\.))])
400 (cond [(null? sub-components) (null? par-components)]
401 [(null? par-components)]
402 [(string-ci=? (car sub-components) (car par-components))
403 (loop (cdr sub-components) (cdr par-components))]
404 [else #f])))
405
406 ;; API
407 (define (make-cookie-jar :optional (proto #f))
408 (assume-type proto (<?> <cookie-jar>))
409 (make <cookie-jar>
410 :%table (if proto
411 (atom (list-copy (atom-ref (~ proto'%table))))
412 (atom '()))))
413
414 ;; API
415 (define (cookie-jar-put! jar request-host request-path parsed-cookies)
416 (define (put-1 parsed-cookie)
417 (match parsed-cookie
418 [(name value . args)
419 (let-keywords args ([path #f] [domain #f] [port #f]
420 [discard #f] [max-age #f] [expires #f]
421 [secure #f] [http-only #f] [version #f]
422 [comment #f] [comment-url #f]
423 . other)
424 (unless (null? other)
425 (error "Invalid cookie attribute in ~s" parsed-cookie))
426 (when (or (not domain)
427 ;; if request-host doesn't belong to domain, just ignore
428 (%domain-belongs-to? request-host domain))
429 (if (or (not value) (equal? value ""))
430 (%remove-cookie! jar (or domain request-host) name)
431 (%put-cookie! jar
432 (make <http-cookie>
433 :name name
434 :value value
435 :domain (string-downcase (or domain request-host))
436 :request-host request-host
437 :path (or path (%default-path request-path))
438 :lifetime (%compute-lifetime max-age expires)
439 :port (if port
440 (string-tokenize port)
441 '())
442 :secure secure
443 :version version
444 :comment comment
445 :comment-url comment-url)))))]
446 [_ (error "Invalid cookie format: ~s" parsed-cookie)]))
447
448 (for-each put-1 parsed-cookies))
449
450 ;; API
451 (define (cookie-jar-get jar request-host request-port request-path)
452 (define now (current-time))
453 (define (alive? c) (or (not (~ c'lifetime))
454 (time<=? (~ c'lifetime) now)))
455 (define (match? c) (and (%domain-belongs-to? request-host (~ c'domain))
456 (%path-match request-path (~ c'path))
457 (or (null? (~ c'port))
458 (memv request-port (~ c'port)))))
459
460 (values-ref
461 ($ atomic-update! (~ jar'%table)
462 (^[cookies]
463 (let loop ([keep '()] [matched '()] [cookies cookies])
464 (match cookies
465 [() (values (reverse keep) (reverse matched))]
466 [(c . cookies)
467 (if (alive? c)
468 (loop (cons c keep)
469 (if (match? c) (cons c matched) matched)
470 cookies)
471 (loop keep matched cookies))]))))
472 1))
473
474 ;; API
475 ;; Discard non-persistent cookies
476 (define (cookie-jar-purge-ephemeral! jar)
477 (let1 now (current-time)
478 (atomic-update! (~ jar'%table)
479 (^[table] (filter (^e (~ e'lifetime)) table)))))