Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MessageVerifier to work with new API #161

Closed
wants to merge 2 commits into from

Conversation

zzak
Copy link
Member

@zzak zzak commented Aug 8, 2023

This API changed in rails/rails@91b2a15 (#47325)

/cc @jonathanhefner

How to reproduce:

>> GlobalID::Locator.locate_signed(Company.first.to_sgid(for: :debug), for: :debug)
  Company Load (0.1ms)  SELECT "companies".* FROM "companies" ORDER BY "companies"."id" ASC LIMIT ?  [["LIMIT", 1]]
  Company Load (0.1ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
=> #<Company:0x00000001052f1560 id: 1, name: "nintendo", created_at: Mon, 07 Aug 2023 10:27:45.223195000 UTC +00:00, updated_at: Mon, 07 Aug 2023 10:27:45.223195000 UTC +00:00>

Previously, using the JSON method, this would return nil.

I wasn't able to install nokogiri 1.6.6.4 on an arm64 m1 macbook.

```
Building nokogiri using packaged libraries.
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.

To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions.  If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/zzak/.rbenv/versions/3.2.2/bin/$(RUBY_BASE_NAME)
        --help
        --clean
        --use-system-libraries
        --enable-static
        --disable-static
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --enable-cross-build
        --disable-cross-build

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/zzak/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/extensions/arm64-darwin-23/3.2.0/nokogiri-1.6.6.4/mkmf.log

extconf failed, exit code 1
```
This API changed in rails/rails@91b2a15 (#47325)

/cc @jonathanhefner

How to reproduce:

```
>> GlobalID::Locator.locate_signed(Company.first.to_sgid(for: :debug), for: :debug)
  Company Load (0.1ms)  SELECT "companies".* FROM "companies" ORDER BY "companies"."id" ASC LIMIT ?  [["LIMIT", 1]]
  Company Load (0.1ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
=> #<Company:0x00000001052f1560 id: 1, name: "nintendo", created_at: Mon, 07 Aug 2023 10:27:45.223195000 UTC +00:00, updated_at: Mon, 07 Aug 2023 10:27:45.223195000 UTC +00:00>
```

Previously, using the JSON method, this would return `nil`.
@zzak
Copy link
Member Author

zzak commented Aug 8, 2023

I should mention this doesn't occur when using other non-JSON message serializers, via:

Rails.application.config.active_support.message_serializer = :message_pack

The new default :json_allow_marshal also returns nil.

@zzak
Copy link
Member Author

zzak commented Aug 8, 2023

Also found #149 as possibly related, but haven't looked into it much further, could be a red herring.

Copy link
Member

@jonathanhefner jonathanhefner left a comment

Choose a reason for hiding this comment

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

rails/rails@91b2a15 did not change these method names; it just factored them out into ActiveSupport::Messages::Codec. The sign_encoded and extract_encoded methods are different.

The problem here is actually the change of the default serializer from Marshal to JSON (via rails/rails#42843). JSON does not roundtrip symbols. So if you change your example to

GlobalID::Locator.locate_signed(Company.first.to_sgid(for: "debug"), for: "debug")

it will work. You can even change it to

GlobalID::Locator.locate_signed(Company.first.to_sgid(for: :debug), for: "debug")

and it will work.

To support symbol purposes for all serializers, we would need to change

metadata['gid'] if pick_purpose(options) == metadata['purpose']

to

        metadata['gid'] if pick_purpose(options)&.to_s == metadata['purpose']&.to_s

(Note that ActiveSupport::MessageVerifier already does this internally when using its :purpose option, but SignedGlobalID handles purpose itself.)

@rafaelfranca
Copy link
Member

I'm trying to write a test to reproduce this error and this one always pass.

Am I missing something?

  test 'foo' do
    verifier = GlobalID::Verifier.new('muchSECRETsoHIDDEN', serializer: JSON)
    assert_equal Person::Child.new('2'), GlobalID::Locator.locate_signed(Person::Child.new('2').to_sgid(for: :debug, verifier: verifier), for: :debug, verifier: verifier)
  end

@jonathanhefner
Copy link
Member

I was testing with a released version of Global ID. It looks like this may be fixed on main by dc9a648 and 12f7629, which route verification through ActiveSupport::MessageVerifier (which calls to_s on the purpose).

@rafaelfranca
Copy link
Member

Thanks! I just got that. I updated the test and the legacy verifier behavior to make sure this works.

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.

None yet

3 participants