Skip to content

Conversation

assain
Copy link
Contributor

@assain assain commented May 29, 2017

@kaspth
Currently ActiveSupport::MessageEncryptor defaults to CBC encryption, but it could stand to benefit from defaulting to aes-256-gcm encryption for the same reasons as cookies.

Gemfile.lock Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove these Gemfile.lock changes. See Eileen's contributing to Rails workshop around amending and rebasing for some help on how: https://youtu.be/7zoD6NZy6vY?t=1h25s

Copy link
Contributor Author

@assain assain May 29, 2017

Choose a reason for hiding this comment

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

Thank you for the feedback @kaspth, I'll fix it and add another commit 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't add another commit. Just rebase/amend this one :)

@kaspth
Copy link
Contributor

kaspth commented May 29, 2017

For some reason Travis didn't run your pull request. Try the rebase/amend then force push.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the setup method used aes-256-cbc cipher while writing this test and had made use of Message Verifier. However, after playing around with the tests, I realized this test was failing because ActiveSupport::MessageVerifier::InvalidSignature was raised by message verifier, and the new default cipher - aes-256-gcm used NullVerifier.

@kaspth
Copy link
Contributor

kaspth commented Jun 3, 2017

Okay, now we'll need a similar setting to what the AEAD cookies has here: https://github.com/rails/rails/pull/28132/files#diff-7f4141d796a8aad409136b2b6dc774b0R15

E.g. that there's a new 5.2 config to default the encryptor to gcm:

# Other configs in new_framework_defaults_5_2.rb…

# Default encrypted messages to use AES-256-GCM encryption.
# Rails.application.active_support.message_encryptor.use_authenticated_encryption = true

Then auto default that to true in load_defaults under "5.2".

If we don't have that config, we break backwardscompatibility — that's what the test failures show.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't say use_authenticated_cookie_encryption but just use_authenticated_encryption 😊

Copy link
Contributor Author

@assain assain Jun 6, 2017

Choose a reason for hiding this comment

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

I had corrected that mistake, don't know how it crept in again. Apologies! :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, we should be able to remove this config and use the below one to infer it. But that's for another time.

cc @mikeycgto

Copy link
Contributor

Choose a reason for hiding this comment

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

Keep the period.

Copy link
Contributor

Choose a reason for hiding this comment

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

self.class.default_cipher

Copy link
Contributor

Choose a reason for hiding this comment

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

This comment doesn't really add anything to me. Let's just document it elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

as default for -> when

Copy link
Contributor

Choose a reason for hiding this comment

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

This all needs to be wrapped in an initializer (with a descriptive name) so it's run when the app boots and not when this file is parsed.

This Railtie subclass is marked as # :nodoc meaning it won't show up in documentation, so just skip the comments here.

I'd rewrite to this within the initializer:

if config.active_support.respond_to?(:use_authenticated_message_encryption)
  ActiveSupport::MessageEncryptor.use_authenticated_message_encryption =
    config.active_support.use_authenticated_message_encryption
end

We have to use respond_to? because config.active_support, an instance of ActiveSupport::OrderedOptions, raises if the key is unassigned.

@kaspth
Copy link
Contributor

kaspth commented Jun 11, 2017

Woah, hey you got the tests to pass! 👏 — make sure you look at Code Climate too, there's one thing missing there. 😊

Now we just need to test that the default change works. Let's just go with setting use_authenticated_message_encryption in the Message Encryptor test and inspecting default_cipher for now.

Also: default_cipher should be marked as def default_cipher # :nodoc:, don't think that should be public API for people.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kaspth
Whenever, I make this change, Travis CI fails, because of sessions_test.rb which outputs:

  1. Failure:
    ApplicationTests::MiddlewareSessionTest#test_session_upgrading_from_AES-CBC-HMAC_encryption_to_AES-GCM_encryption [test/application/middleware/session_test.rb:351]:
    Expected: "1"
    Actual: ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, see the other comment about why 😊

Let's just clip "_as_default" from the initializer title while we're here.

Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

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

So close now!

Copy link
Contributor

Choose a reason for hiding this comment

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

This will be true by default, so there's no need to set this.

We should assign the cbc cipher: to the legacy encryptor in cookies middleware. Then the test passes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, see the other comment about why 😊

Let's just clip "_as_default" from the initializer title while we're here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Needs a # :nodoc same for the use_authenticated_message_encryption accessor.

Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops! My bad!

Copy link
Contributor

Choose a reason for hiding this comment

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

This neither.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use double quoted strings.

- Introduce a method to select default cipher, and maintain backward compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment