Skip to content

Ignore whitespaces when decoding cookie's content#17

Open
RudolfMan wants to merge 1 commit into
cconstantin:masterfrom
RudolfMan:decode-cookie-with-whitespaces
Open

Ignore whitespaces when decoding cookie's content#17
RudolfMan wants to merge 1 commit into
cconstantin:masterfrom
RudolfMan:decode-cookie-with-whitespaces

Conversation

@RudolfMan

Copy link
Copy Markdown

Our Rails 4 application encodes cookie with Marshal and for some reason its encoded value consists of newlines (\n)
By default Base.decode64/1 throws an ArgumentError

iex> content = "BAh7BkkiD3Nlc3Npb25faWQGOgZFVEkiKTk5ZDQ3YzdiLTdlODItNGUxZS1h\nYTMyLWE4MjIwOWVhNDA5ZQY7AFQ=\n"
iex> Base.decode64!(content)
** (ArgumentError) non-alphabet digit found: "\n" (byte 10)
    (elixir 1.11.0) lib/base.ex:1002: Base.dec64/1
    (elixir 1.11.0) lib/base.ex:1018: Base."-do_decode64/2-lbc$^0/2-0-"/2
    (elixir 1.11.0) lib/base.ex:1012: Base.do_decode64/2

That issue is easily fixed with passing ignore: :whitespace option to Base.decode64!

iex> Base.decode64!(content, ignore: :whitespace) |> ExMarshal.decode()
%{"session_id" => "99d47c7b-7e82-4e1e-aa32-a82209ea409e"}

Plus as a bonus I use with to avoid nested condition

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant