Skip to content

Contract on make-id-cookie should accept byte-strings #74

Closed

Description

@LiberalArtist

@soegaard reported on Slack that the contract on make-id-cookie currently requires the name and value arguments to be given as strings, so this program fails:

#lang racket

(require web-server/http/id-cookie
         racket/random)

(make-id-cookie #"aname" #"avalue"
                #:key (crypto-random-bytes 128))

All other cookie-constructing functions permissively accept byte-strings for names and values (converting them with bytes->string/utf-8), and it seems like make-id-cookie should, too—I probably just missed it after I figured out RenaissanceBug/racket-cookies@c745ee5. I will try to make a PR soon.

Activity

  1. LiberalArtist commented on Sep 15, 2019

    @LiberalArtist
    ContributorAuthor

    While working on a patch, I believe I found another bug. In this code:

    (define (mac key v)
    (substring*
    (bytes->string/utf-8
    (base64-encode (HMAC-SHA1 key (write/bytes v)) #""))
    0 -3))

    In a5135d5, I believe @jeapostrophe should have changed -3 to -1 when he supplied #"" to base64-encode for the newline instead of the default #"\r\n".

    A base64-encoded SHA-1 is always 28 bytes long, including exactly one = at the end for padding: since the length is fixed, we can drop the padding and return 27 bytes from mac. Right now, we are also dropping 2 significant digits of the SHA-1 hash.

    I think this should be fixed and plan to include it in this patch. However, people's existing id cookies created with the current version of the library will not validate using a fixed version. I think the potential security implications outweigh the breakage, but I'm open to other opinions.

  2. LiberalArtist commented on Sep 15, 2019

    @LiberalArtist
    ContributorAuthor

    If it seems necessary, one option would be to add a function unverified-id-cookie?. I think it would be like valid-id-cookie?, but #:key would be optional and default to #f. If the key is #f, it would just check the name and timeout and not try to verify the digest. beyond checking that it syntactically could have been generated by some version of this library (i.e. it is 25 or 27 base64 digits). Alternatively, if you supply a key, it would check the digest but allow it to be two digits short.

    That would let programmers make their own decisions about how to handle their existing id cookies. But, personally, I don't feel a need for such a function.

  3. jeapostrophe commented on Sep 16, 2019

    @jeapostrophe
    Contributor

    You should just change the -3 to a -1. It was simply a mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions