Skip to content

Improve security - #8

Merged
yanyil merged 4 commits into
masterfrom
improve_security
Jul 4, 2022
Merged

Improve security#8
yanyil merged 4 commits into
masterfrom
improve_security

Conversation

@yanyil

@yanyil yanyil commented Jun 30, 2022

Copy link
Copy Markdown
Contributor
  • set default magic token cookie expiry to 1 hour (can be overridden)
  • use cookies.encrypted instead of cookies.signed so cookie cannot be read

@yanyil
yanyil force-pushed the improve_security branch from ad2b60c to c1c36ac Compare June 30, 2022 16:16
@wozza35 wozza35 linked an issue Jul 1, 2022 that may be closed by this pull request

@wozza35 wozza35 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Minor comments

return root unless magic_token.present?

cookies.signed[magic_token_key] = magic_token.token if scope
expiry = MagicLinks.magic_token_cookie_expiry&.from_now

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could extract this line into a cookie_expires_at method for clarity maybe?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I could be wrong, but according to the docs expires: can take a duration. So maybe the from_now call isn't even needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes looks like it accepts a duration!

Comment thread README.md Outdated
environments.

### Magic token cookie expiry
By default, the magic token cookie expiry (not to be confused with the expiry of the magic link) is set to 1 hour. You

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth explaining what the cookie does for people who aren't familiar with the gem?

'A magic token cookie is dropped on the client when a magic link is used. This cookie is used to authenticate subsequent requests. By default...'

expect(request.cookie_jar.encrypted[:user_magic_token]).to eq magic_token.token
end

context 'when cookie expiry is set to nil' do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the to nil bit meant to be there?

# Therefore we need only test that our middleware sets the cookie via ActionDispatch
Rack::MockResponse.new(*subject.call(request.env))
expect(request.cookie_jar.signed[:user_magic_token]).to eq magic_token.token
expect(request.cookie_jar.encrypted[:user_magic_token]).to eq magic_token.token

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some tests that still include the word 'signed' cookie. Change to 'encrypted'?

@yanyil
yanyil force-pushed the improve_security branch from 5bc7312 to 91e577f Compare July 1, 2022 13:54
@yanyil
yanyil force-pushed the improve_security branch from 91e577f to 2cf15fe Compare July 1, 2022 13:57
@yanyil
yanyil merged commit da13307 into master Jul 4, 2022
@yanyil
yanyil deleted the improve_security branch July 4, 2022 12:04
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.

magic token should be stored in an encrypted cookie

2 participants