discourse/config/initializers
Alan Guo Xiang Tan 6e9fbb5bab
DEV: Do not process requests initiated by browser in a different example (#25809)
Why this change?

We noticed that running `LOAD_PLUGINS=1 rspec --seed=38855 plugins/chat/spec/system/chat_new_message_spec.rb` locally
results in the system tests randomly failing. When we inspected the
request logs closely, we noticed that a `/presence/get` request from a
previous rspec example was being processed when a new rspec example is
already being run. We know it was from the previous rspec example
because inspecting the auth token showed the request using the auth
token of a user from the previous example. However, when a request using
an auth token from a previous example is used it ends up logging out the
same user on the server side because the user id in the cookie is the same
due to the use of `fab!`.

I did some research and there is apparently no way to wait until all
inflight requests by the browser has completed through capybara or
selenium. Therefore, we will add an identifier by attaching a cookie to all non-xhr requests so that
xhr requests which are triggered subsequently will contain the cookie in the request.

In the `BlockRequestsMiddleware` middleware, we will then reject any
requests when the value of the identifier in the cookie does not match the current rspec's example
location.

To see the problem locally, change `Auth::DefaultCurrentUserProvider.find_v1_auth_cookie` to the following:

```
  def self.find_v1_auth_cookie(env)
    return env[DECRYPTED_AUTH_COOKIE] if env.key?(DECRYPTED_AUTH_COOKIE)

    env[DECRYPTED_AUTH_COOKIE] = begin
      request = ActionDispatch::Request.new(env)
      cookie = request.cookies[TOKEN_COOKIE]

      # don't even initialize a cookie jar if we don't have a cookie at all
      if cookie&.valid_encoding? && cookie.present?
        puts "#{env["REQUEST_PATH"]} #{request.cookie_jar.encrypted[TOKEN_COOKIE]&.with_indifferent_access}"
        request.cookie_jar.encrypted[TOKEN_COOKIE]&.with_indifferent_access
      end
    end
  end
```

After which run the following command: `LOAD_PLUGINS=1 rspec --format documentation --seed=38855 plugins/chat/spec/system/chat_new_message_spec.rb`

It takes a few tries but the last spec should fail and you should see something like this:

```
assets/chunk.c16f6ba8b6824baa47ac.d41d8cd9.js {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/assets/chunk.050148142e1d2dc992dd.d41d8cd9.js {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/chat/api/channels/527/messages {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
/uploads/default/test_0/optimized/1X/_129430568242d1b7f853bb13ebea28b3f6af4e7_2_512x512.png {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
    redirects to existing chat channel
    redirects to chat channel if recipients param is missing (PENDING: Temporarily skipped with xit)
  with multiple users
/favicon.ico {"token"=>"9a75c114c4d3401509a23d240f0a46d4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591736}
/chat/new-message {"token"=>"9a75c114c4d3401509a23d240f0a46d4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591736}
/presence/get {"token"=>"37d995a4b65395d3b343ec70fff915b4", "user_id"=>3382, "username"=>"bruce0", "trust_level"=>1, "issued_at"=>1708591735}
 ```
 
 Note how the `/presence/get` request is using a token from the previous example. 

Co-authored-by: David Taylor <david@taylorhq.com>
2024-02-22 19:41:10 +08:00
..
000-development_reload_warnings.rb DEV: Further refine development reload for plugin files (#22141) 2023-06-16 16:15:15 +08:00
000-mini_sql.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
000-post_migration.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
000-trace_pg_connections.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
000-zeitwerk.rb DEV: chat streaming (#25736) 2024-02-20 09:49:19 +01:00
001-redis.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
002-freedom_patches.rb FIX: deprecation warning - initialization autoloaded the constant (#12400) 2021-03-16 09:47:57 +11:00
002-rails_failover.rb FEATURE: Introduce pg_force_readonly_mode GlobalSetting (#19612) 2023-01-19 13:59:11 +00:00
004-message_bus.rb DEV: Patch capybara to ignore client-triggered errors (#19972) 2023-01-24 11:07:29 +00:00
005-site_settings.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
006-ensure_login_hint.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
006-mini_profiler.rb FEATURE: Add experimental option for strict-dynamic CSP (#25664) 2024-02-16 11:16:54 +00:00
008-rack-cors.rb FIX: Ensure app-cdn CORS is not overridden by cors_origin setting (#24661) 2023-12-01 12:57:11 +00:00
009-omniauth.rb DEV: Drop legacy OpenID 2.0 support (#8894) 2020-02-07 17:32:35 +00:00
012-web_hook_events.rb FEATURE: Add webhooks for user suspend and unsuspend (#23684) 2023-09-28 10:51:05 +02:00
013-excon_defaults.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
014-track-setting-changes.rb PERF: Cache ToS and Privacy Policy paths (#21860) 2023-06-07 21:31:20 +03:00
099-anon-cache.rb FEATURE: Add experimental option for strict-dynamic CSP (#25664) 2024-02-16 11:16:54 +00:00
099-drain_pool.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-i18n.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-logster.rb FIX: Logster backlink config in production (#25685) 2024-02-15 13:48:36 +11:00
100-oj.rb DEV: default Oj to compat mode 2020-01-16 07:52:28 +11:00
100-onebox_options.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-push-notifications.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-quiet_logger.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-regex-timeout.rb DEV: Introduce regex_timeout_seconds global setting (#20774) 2023-03-22 12:01:35 +00:00
100-secret_token.rb DEV: enable frozen string literal on all files 2019-05-13 09:31:32 +08:00
100-session_store.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-sidekiq.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-silence_logger.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-strong_parameters.rb DEV: enable frozen string literal on all files 2019-05-13 09:31:32 +08:00
100-verify_config.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
100-wrap_parameters.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
101-lograge.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
102-truncate-logs.rb SECURITY: Add a default limit as to when logs should be truncated 2023-10-16 10:34:38 -04:00
200-first_middlewares.rb DEV: Do not process requests initiated by browser in a different example (#25809) 2024-02-22 19:41:10 +08:00
300-perf.rb FEATURE: add hook after all initializers 2019-08-26 10:49:26 +10:00
400-deprecations.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
assets.rb DEV: Update confirm-email flows to use central 2fa and ember rendering (#25404) 2024-01-30 10:32:42 +00:00
filter_parameter_logging.rb DEV: Apply syntax_tree formatting to config/* 2023-01-09 11:13:29 +00:00
new_framework_defaults_7_0.rb Revert "DEV: Migrate existing cookies to Rails 7 format" 2023-01-12 12:07:49 +01:00