diff --git a/lib/appmap/handler/rails/sql_handler.rb b/lib/appmap/handler/rails/sql_handler.rb index 818a0f64..fbdad18d 100644 --- a/lib/appmap/handler/rails/sql_handler.rb +++ b/lib/appmap/handler/rails/sql_handler.rb @@ -116,7 +116,7 @@ class ActiveRecordExaminer def issue_warning db_type = database_type return if @@db_version_warning_issued[db_type] - warn("AppMap: Unable to determine database version for #{db_type.inspect}") + warn("AppMap: Unable to determine database version for #{db_type.inspect}") @@db_version_warning_issued[db_type] = true end @@ -136,7 +136,32 @@ def in_transaction? end def execute_query(sql) - ActiveRecord::Base.connection.execute(sql).to_a + execute_in_context { |conn| conn.execute(sql).to_a } + end + + private + + # Rails 7.1 introduced ActiveSupport::IsolatedExecutionState which resolves issues related to Enumerator and + # Threads. The `graphql-ruby` gem uses the `async` gem to run queries in a separate fiber. When the Fiber + # executing the ActiveRecord query within AppMap, the `.transfer` is not handed back to the root Fiber. This + # causes the Fiber to remain in a "suspended" state instead of "terminated". By wrapping the execution with + # a Fiber, the transfer is handled and terminated correctly. + # + # @see Demonstration of Fibers not being transfered to the root Fiber + # https://replit.com/@coderberry/Fiber-Transfer-Example + # + # Also, when a connection is manually checked out, Rails does not automatically release the connection. By + # wrapping the execution with `ActiveRecord::Base.connection_pool.with_connection`, we ensure the connection + # is properly released. + def execute_in_context + if defined?(ActiveSupport::IsolatedExecutionState) && ActiveSupport::IsolatedExecutionState.context == :fiber + # This is not working + Fiber.new do + ActiveRecord::Base.connection_pool.with_connection { yield } + end.resume + else + ActiveRecord::Base.connection_pool.with_connection { |conn| block.call(conn) } + end end end end diff --git a/spec/fixtures/database.yml b/spec/fixtures/database.yml index 451dbb80..d1dafbe1 100644 --- a/spec/fixtures/database.yml +++ b/spec/fixtures/database.yml @@ -2,6 +2,7 @@ default: &default url: <%= ENV['DATABASE_URL'] %> adapter: postgresql database: <%= ENV['TEST_DATABASE'] || 'appland-rails7-test' %> + pool: 5 development: <<: *default diff --git a/spec/fixtures/rails7_users_app/Gemfile b/spec/fixtures/rails7_users_app/Gemfile index db1799d6..b3c7c8ae 100644 --- a/spec/fixtures/rails7_users_app/Gemfile +++ b/spec/fixtures/rails7_users_app/Gemfile @@ -2,15 +2,17 @@ source "https://rubygems.org" git_source(:github) { |repo| "https://github.com/#{repo}.git" } -gem "rails", "~> 7.0.2", ">= 7.0.2.3" +gem "rails", "~> 7.1" gem 'sprockets-rails' gem 'haml-rails' gem "pg", "~> 1.1" +gem "graphql", ">= 2" +gem "async" gem 'activerecord', require: false gem 'sequel', '>= 5.43.0', require: false gem 'sequel-rails', require: false gem 'sequel_secure_password', require: false -gem "puma", "~> 5.0" +gem "puma", "~> 6.0" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] diff --git a/spec/fixtures/rails7_users_app/app/controllers/graphql_controller.rb b/spec/fixtures/rails7_users_app/app/controllers/graphql_controller.rb new file mode 100644 index 00000000..20b2c7f1 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/controllers/graphql_controller.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +class GraphqlController < ApplicationController + # If accessing from outside this domain, nullify the session + # This allows for outside API access while preventing CSRF attacks, + # but you'll have to authenticate your user separately + protect_from_forgery with: :null_session + + def execute + variables = prepare_variables(params[:variables]) + query = params[:query] + operation_name = params[:operationName] + context = { + # Query context goes here, for example: + # current_user: current_user, + } + result = AppSchema.execute(query, variables: variables, context: context, operation_name: operation_name).to_h + + # Append connection pool enhanced_stats to result if the orm_module is ActiveRecord + # This data is used in spec/fixtures/rails7_users_app/spec/controllers/graphql_controller_spec.rb + if defined?(ActiveRecord) + connection_pool_stats = ActiveRecord::Base.connection_pool.enhanced_stats + result["data"]["connection_pool_stats"] = connection_pool_stats + end + + render json: result + rescue StandardError => e + raise e unless Rails.env.development? + handle_error_in_development(e) + end + + private + + # Handle variables in form data, JSON body, or a blank value + def prepare_variables(variables_param) + case variables_param + when String + if variables_param.present? + JSON.parse(variables_param) || {} + else + {} + end + when Hash + variables_param + when ActionController::Parameters + variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables. + when nil + {} + else + raise ArgumentError, "Unexpected parameter: #{variables_param}" + end + end + + def handle_error_in_development(e) + logger.error e.message + logger.error e.backtrace.join("\n") + + render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500 + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/app_schema.rb b/spec/fixtures/rails7_users_app/app/graphql/app_schema.rb new file mode 100644 index 00000000..25148c2d --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/app_schema.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +class AppSchema < GraphQL::Schema + mutation(Types::MutationType) + query(Types::QueryType) + + # For batch-loading (see https://graphql-ruby.org/dataloader/overview.html) + use GraphQL::Dataloader::AsyncDataloader + + # GraphQL-Ruby calls this when something goes wrong while running a query: + def self.type_error(err, context) + # if err.is_a?(GraphQL::InvalidNullError) + # # report to your bug tracker here + # return nil + # end + super + end + + # Union and Interface Resolution + def self.resolve_type(abstract_type, obj, ctx) + # TODO: Implement this method + # to return the correct GraphQL object type for `obj` + raise(GraphQL::RequiredImplementationMissingError) + end + + # Stop validating when it encounters this many errors: + validate_max_errors(100) + + # Relay-style Object Identification: + + # Return a string UUID for `object` + def self.id_from_object(object, type_definition, query_ctx) + # For example, use Rails' GlobalID library (https://github.com/rails/globalid): + object.to_gid_param + end + + # Given a string UUID, find the object + def self.object_from_id(global_id, query_ctx) + # For example, use Rails' GlobalID library (https://github.com/rails/globalid): + GlobalID.find(global_id) + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/mutations/.keep b/spec/fixtures/rails7_users_app/app/graphql/mutations/.keep new file mode 100644 index 00000000..e69de29b diff --git a/spec/fixtures/rails7_users_app/app/graphql/mutations/base_mutation.rb b/spec/fixtures/rails7_users_app/app/graphql/mutations/base_mutation.rb new file mode 100644 index 00000000..0ff6c4ee --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/mutations/base_mutation.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Mutations + class BaseMutation < GraphQL::Schema::RelayClassicMutation + argument_class Types::BaseArgument + field_class Types::BaseField + input_object_class Types::BaseInputObject + object_class Types::BaseObject + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/resolvers/base_resolver.rb b/spec/fixtures/rails7_users_app/app/graphql/resolvers/base_resolver.rb new file mode 100644 index 00000000..89b7f9da --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/resolvers/base_resolver.rb @@ -0,0 +1,4 @@ +module Resolvers + class BaseResolver < GraphQL::Schema::Resolver + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/.keep b/spec/fixtures/rails7_users_app/app/graphql/types/.keep new file mode 100644 index 00000000..e69de29b diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_argument.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_argument.rb new file mode 100644 index 00000000..2e2278c5 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_argument.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module Types + class BaseArgument < GraphQL::Schema::Argument + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_connection.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_connection.rb new file mode 100644 index 00000000..366c69e8 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_connection.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Types + class BaseConnection < Types::BaseObject + # add `nodes` and `pageInfo` fields, as well as `edge_type(...)` and `node_nullable(...)` overrides + include GraphQL::Types::Relay::ConnectionBehaviors + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_edge.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_edge.rb new file mode 100644 index 00000000..e0d2f79c --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_edge.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Types + class BaseEdge < Types::BaseObject + # add `node` and `cursor` fields, as well as `node_type(...)` override + include GraphQL::Types::Relay::EdgeBehaviors + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_enum.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_enum.rb new file mode 100644 index 00000000..cf43fea4 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_enum.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module Types + class BaseEnum < GraphQL::Schema::Enum + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_field.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_field.rb new file mode 100644 index 00000000..611eb056 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_field.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Types + class BaseField < GraphQL::Schema::Field + argument_class Types::BaseArgument + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_input_object.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_input_object.rb new file mode 100644 index 00000000..27951132 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_input_object.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Types + class BaseInputObject < GraphQL::Schema::InputObject + argument_class Types::BaseArgument + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_interface.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_interface.rb new file mode 100644 index 00000000..18899387 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_interface.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Types + module BaseInterface + include GraphQL::Schema::Interface + edge_type_class(Types::BaseEdge) + connection_type_class(Types::BaseConnection) + + field_class Types::BaseField + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_object.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_object.rb new file mode 100644 index 00000000..487af2f5 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_object.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Types + class BaseObject < GraphQL::Schema::Object + edge_type_class(Types::BaseEdge) + connection_type_class(Types::BaseConnection) + field_class Types::BaseField + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_scalar.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_scalar.rb new file mode 100644 index 00000000..719bc808 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_scalar.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module Types + class BaseScalar < GraphQL::Schema::Scalar + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/base_union.rb b/spec/fixtures/rails7_users_app/app/graphql/types/base_union.rb new file mode 100644 index 00000000..95941696 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/base_union.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Types + class BaseUnion < GraphQL::Schema::Union + edge_type_class(Types::BaseEdge) + connection_type_class(Types::BaseConnection) + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/mutation_type.rb b/spec/fixtures/rails7_users_app/app/graphql/types/mutation_type.rb new file mode 100644 index 00000000..5b9c1ade --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/mutation_type.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Types + class MutationType < Types::BaseObject + # TODO: remove me + field :test_field, String, null: false, + description: "An example field added by the generator" + def test_field + "Hello World" + end + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/node_type.rb b/spec/fixtures/rails7_users_app/app/graphql/types/node_type.rb new file mode 100644 index 00000000..c71ec3ee --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/node_type.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Types + module NodeType + include Types::BaseInterface + # Add the `id` field + include GraphQL::Types::Relay::NodeBehaviors + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/query_type.rb b/spec/fixtures/rails7_users_app/app/graphql/types/query_type.rb new file mode 100644 index 00000000..153ef88a --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/query_type.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Types + class QueryType < Types::BaseObject + field :node, Types::NodeType, null: true, description: "Fetches an object given its ID." do + argument :id, ID, required: true, description: "ID of the object." + end + + def node(id:) + context.schema.object_from_id(id, context) + end + + field :nodes, [Types::NodeType, null: true], null: true, description: "Fetches a list of objects given a list of IDs." do + argument :ids, [ID], required: true, description: "IDs of the objects." + end + + def nodes(ids:) + ids.map { |id| context.schema.object_from_id(id, context) } + end + + # POST /graphql?query={users{id,login}} + field :users, [Types::UserType], null: true + def users + User.all + end + end +end diff --git a/spec/fixtures/rails7_users_app/app/graphql/types/user_type.rb b/spec/fixtures/rails7_users_app/app/graphql/types/user_type.rb new file mode 100644 index 00000000..2b752a90 --- /dev/null +++ b/spec/fixtures/rails7_users_app/app/graphql/types/user_type.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Types + class UserType < Types::BaseObject + field :id, ID, null: false + field :login, String, null: false + end +end diff --git a/spec/fixtures/rails7_users_app/bin/rake b/spec/fixtures/rails7_users_app/bin/rake index 9275675e..4fbf10b9 100755 --- a/spec/fixtures/rails7_users_app/bin/rake +++ b/spec/fixtures/rails7_users_app/bin/rake @@ -1,29 +1,4 @@ #!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'rake' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("rake", "rake") +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/spec/fixtures/rails7_users_app/bin/setup b/spec/fixtures/rails7_users_app/bin/setup index ec47b79b..c246fdab 100755 --- a/spec/fixtures/rails7_users_app/bin/setup +++ b/spec/fixtures/rails7_users_app/bin/setup @@ -5,7 +5,7 @@ require "fileutils" APP_ROOT = File.expand_path("..", __dir__) def system!(*args) - system(*args) || abort("\n== Command #{args} failed ==") + system(*args, exception: true) end FileUtils.chdir APP_ROOT do @@ -17,11 +17,6 @@ FileUtils.chdir APP_ROOT do system! "gem install bundler --conservative" system("bundle check") || system!("bundle install") - # puts "\n== Copying sample files ==" - # unless File.exist?("config/database.yml") - # FileUtils.cp "config/database.yml.sample", "config/database.yml" - # end - puts "\n== Preparing database ==" system! "bin/rails db:prepare" diff --git a/spec/fixtures/rails7_users_app/config/application.rb b/spec/fixtures/rails7_users_app/config/application.rb index 74562479..88280ad8 100644 --- a/spec/fixtures/rails7_users_app/config/application.rb +++ b/spec/fixtures/rails7_users_app/config/application.rb @@ -22,7 +22,6 @@ def orm_module require 'database_cleaner-active_record' if Rails.env.test? end - # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) @@ -30,7 +29,7 @@ def orm_module module App class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 7.0 + config.load_defaults 7.1 # Configuration for the application, engines, and railties goes here. # @@ -44,5 +43,8 @@ class Application < Rails::Application Rails.autoloaders.main.ignore << File.join(Rails.root, 'app/models') config.autoload_paths << File.join(Rails.root, "app/models/#{orm_module}") config.middleware.insert_before(-1, Hijacker) + + # Allows testing of fiber-based connection pools + config.active_support.isolation_level = ENV.fetch("ISOLATION_LEVEL", "thread").to_sym end end diff --git a/spec/fixtures/rails7_users_app/config/environments/development.rb b/spec/fixtures/rails7_users_app/config/environments/development.rb index ee24aabc..c5e94b37 100644 --- a/spec/fixtures/rails7_users_app/config/environments/development.rb +++ b/spec/fixtures/rails7_users_app/config/environments/development.rb @@ -6,7 +6,7 @@ # In the development environment your application's code is reloaded any time # it changes. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. - config.cache_classes = false + config.enable_reloading = true # Do not eager load code on boot. config.eager_load = false @@ -50,6 +50,6 @@ # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true - # Uncomment if you wish to allow Action Cable access from any origin. - # config.action_cable.disable_request_forgery_protection = true + # Raise error when a before_action's only/except options reference missing actions + # config.action_controller.raise_on_missing_callback_actions = true end diff --git a/spec/fixtures/rails7_users_app/config/environments/production.rb b/spec/fixtures/rails7_users_app/config/environments/production.rb index 847f6272..ce0b0291 100644 --- a/spec/fixtures/rails7_users_app/config/environments/production.rb +++ b/spec/fixtures/rails7_users_app/config/environments/production.rb @@ -4,7 +4,7 @@ # Settings specified here will take precedence over those in config/application.rb. # Code is not reloaded between requests. - config.cache_classes = true + config.enable_reloading = false # Eager load code on boot. This eager loads most of Rails and # your application in memory, allowing both threaded web servers @@ -13,15 +13,13 @@ config.eager_load = true # Full error reports are disabled and caching is turned on. - config.consider_all_requests_local = false - config.action_controller.perform_caching = true + config.consider_all_requests_local = false - # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] - # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # Ensures that a master key has been made available in ENV["RAILS_MASTER_KEY"], config/master.key, or an environment + # key such as config/credentials/production.key. This key is used to decrypt credentials (and other encrypted files). # config.require_master_key = true - # Disable serving static files from the `/public` folder by default since - # Apache or NGINX already handles this. + # Disable serving static files from `public/`, relying on NGINX/Apache to do so instead. config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? # Enable serving of images, stylesheets, and JavaScripts from an asset server. @@ -31,32 +29,33 @@ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX - # Mount Action Cable outside main process or domain. - # config.action_cable.mount_path = nil - # config.action_cable.url = "wss://example.com/cable" - # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] + # Assume all access to the app is happening through a SSL-terminating reverse proxy. + # Can be used together with config.force_ssl for Strict-Transport-Security and secure cookies. + # config.assume_ssl = true # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true - # Include generic and useful information about system operation, but avoid logging too much - # information to avoid inadvertent exposure of personally identifiable information (PII). - config.log_level = :info + # Log to STDOUT by default + config.logger = ActiveSupport::Logger.new(STDOUT) + .tap { |logger| logger.formatter = ::Logger::Formatter.new } + .then { |logger| ActiveSupport::TaggedLogging.new(logger) } # Prepend all log lines with the following tags. config.log_tags = [ :request_id ] + # "info" includes generic and useful information about system operation, but avoids logging too much + # information to avoid inadvertent exposure of personally identifiable information (PII). If you + # want to log everything, set the level to "debug". + config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") + # Use a different cache store in production. # config.cache_store = :mem_cache_store # Use a real queuing backend for Active Job (and separate queues per environment). - # config.active_job.queue_adapter = :resque + # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "app_production" - # Ignore bad email addresses and do not raise email delivery errors. - # Set this to true and configure the email server for immediate delivery to raise delivery errors. - # config.action_mailer.raise_delivery_errors = false - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true @@ -64,16 +63,11 @@ # Don't log any deprecations. config.active_support.report_deprecations = false - # Use default logging formatter so that PID and timestamp are not suppressed. - config.log_formatter = ::Logger::Formatter.new - - # Use a different logger for distributed setups. - # require "syslog/logger" - # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") - - if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) - logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) - end + # Enable DNS rebinding protection and other `Host` header attacks. + # config.hosts = [ + # "example.com", # Allow requests from example.com + # /.*\.example\.com/ # Allow requests from subdomains like `www.example.com` + # ] + # Skip DNS rebinding protection for the default health check endpoint. + # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } end diff --git a/spec/fixtures/rails7_users_app/config/environments/test.rb b/spec/fixtures/rails7_users_app/config/environments/test.rb index 69a3f47f..d349c355 100644 --- a/spec/fixtures/rails7_users_app/config/environments/test.rb +++ b/spec/fixtures/rails7_users_app/config/environments/test.rb @@ -8,12 +8,13 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. - # Turn false under Spring and add config.action_view.cache_template_loading = true. - config.cache_classes = true + # While tests run files are not watched, reloading is not necessary. + config.enable_reloading = false - # Eager loading loads your whole application. When running a single test locally, - # this probably isn't necessary. It's a good idea to do in a continuous integration - # system, or in some way before deploying your code. + # Eager loading loads your entire application. When running a single test locally, + # this is usually not necessary, and can slow down your test suite. However, it's + # recommended that you enable it in continuous integration systems to ensure eager + # loading is working properly before deploying your code. config.eager_load = ENV["CI"].present? # Configure public file server for tests with Cache-Control for performance. @@ -27,8 +28,8 @@ config.action_controller.perform_caching = false config.cache_store = :null_store - # Raise exceptions instead of rendering exception templates. - config.action_dispatch.show_exceptions = false + # Render exception templates for rescuable exceptions and raise for other exceptions. + config.action_dispatch.show_exceptions = :rescuable # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false @@ -47,4 +48,7 @@ # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true + + # Raise error when a before_action's only/except options reference missing actions + config.action_controller.raise_on_missing_callback_actions = true end diff --git a/spec/fixtures/rails7_users_app/config/initializers/connection_pool.rb b/spec/fixtures/rails7_users_app/config/initializers/connection_pool.rb new file mode 100644 index 00000000..484d77a1 --- /dev/null +++ b/spec/fixtures/rails7_users_app/config/initializers/connection_pool.rb @@ -0,0 +1,29 @@ +# This is an internal tool + +if defined?(ActiveRecord) + class ActiveRecord::ConnectionAdapters::ConnectionPool + # Based on ConnectionPool#stat + def enhanced_stats + synchronize do + current_context = ActiveSupport::IsolatedExecutionState.context + { + size: size, + connections: @connections.size, + busy: @connections.count { |c| c.in_use? && c.owner.alive? }, + dead: @connections.count { |c| c.in_use? && !c.owner.alive? }, + idle: @connections.count { |c| !c.in_use? }, + waiting: num_waiting_in_queue, + checkout_timeout: checkout_timeout, + + # Added stats + isolation_level: ActiveSupport::IsolatedExecutionState.isolation_level, + thread_id: Thread.current.object_id, + in_use: @connections.select(&:in_use?).map(&:object_id), + owner_alive: @connections.select { |c| c.owner&.alive? }.map(&:object_id), + owner: @connections.map { |c| c.owner.inspect }, + current_context: current_context.inspect + } + end + end + end +end diff --git a/spec/fixtures/rails7_users_app/config/initializers/content_security_policy.rb b/spec/fixtures/rails7_users_app/config/initializers/content_security_policy.rb index 3621f97f..b3076b38 100644 --- a/spec/fixtures/rails7_users_app/config/initializers/content_security_policy.rb +++ b/spec/fixtures/rails7_users_app/config/initializers/content_security_policy.rb @@ -1,8 +1,8 @@ # Be sure to restart your server when you modify this file. -# Define an application-wide content security policy -# For further information see the following documentation -# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy +# Define an application-wide content security policy. +# See the Securing Rails Applications Guide for more information: +# https://guides.rubyonrails.org/security.html#content-security-policy-header # Rails.application.configure do # config.content_security_policy do |policy| @@ -16,11 +16,10 @@ # # policy.report_uri "/csp-violation-report-endpoint" # end # -# # Generate session nonces for permitted importmap and inline scripts +# # Generate session nonces for permitted importmap, inline scripts, and inline styles. # config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } -# config.content_security_policy_nonce_directives = %w(script-src) +# config.content_security_policy_nonce_directives = %w(script-src style-src) # -# # Report CSP violations to a specified URI. See: -# # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# # Report violations without enforcing the policy. # # config.content_security_policy_report_only = true # end diff --git a/spec/fixtures/rails7_users_app/config/initializers/filter_parameter_logging.rb b/spec/fixtures/rails7_users_app/config/initializers/filter_parameter_logging.rb index adc6568c..c2d89e28 100644 --- a/spec/fixtures/rails7_users_app/config/initializers/filter_parameter_logging.rb +++ b/spec/fixtures/rails7_users_app/config/initializers/filter_parameter_logging.rb @@ -1,8 +1,8 @@ # Be sure to restart your server when you modify this file. -# Configure parameters to be filtered from the log file. Use this to limit dissemination of -# sensitive information. See the ActiveSupport::ParameterFilter documentation for supported -# notations and behaviors. +# Configure parameters to be partially matched (e.g. passw matches password) and filtered from the log file. +# Use this to limit dissemination of sensitive information. +# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors. Rails.application.config.filter_parameters += [ :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn ] diff --git a/spec/fixtures/rails7_users_app/config/initializers/new_framework_defaults_7_1.rb b/spec/fixtures/rails7_users_app/config/initializers/new_framework_defaults_7_1.rb new file mode 100644 index 00000000..4ba02970 --- /dev/null +++ b/spec/fixtures/rails7_users_app/config/initializers/new_framework_defaults_7_1.rb @@ -0,0 +1,284 @@ +# Be sure to restart your server when you modify this file. +# +# This file eases your Rails 7.1 framework defaults upgrade. +# +# Uncomment each configuration one by one to switch to the new default. +# Once your application is ready to run with all new defaults, you can remove +# this file and set the `config.load_defaults` to `7.1`. +# +# Read the Guide for Upgrading Ruby on Rails for more info on each option. +# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html + +### +# No longer add autoloaded paths into `$LOAD_PATH`. This means that you won't be able +# to manually require files that are managed by the autoloader, which you shouldn't do anyway. +# +# This will reduce the size of the load path, making `require` faster if you don't use bootsnap, or reduce the size +# of the bootsnap cache if you use it. +# +# To set this configuration, add the following line to `config/application.rb` (NOT this file): +# config.add_autoload_paths_to_load_path = false + +### +# Remove the default X-Download-Options headers since it is used only by Internet Explorer. +# If you need to support Internet Explorer, add back `"X-Download-Options" => "noopen"`. +#++ +# Rails.application.config.action_dispatch.default_headers = { +# "X-Frame-Options" => "SAMEORIGIN", +# "X-XSS-Protection" => "0", +# "X-Content-Type-Options" => "nosniff", +# "X-Permitted-Cross-Domain-Policies" => "none", +# "Referrer-Policy" => "strict-origin-when-cross-origin" +# } + +### +# Do not treat an `ActionController::Parameters` instance +# as equal to an equivalent `Hash` by default. +#++ +# Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false + +### +# Active Record Encryption now uses SHA-256 as its hash digest algorithm. +# +# There are 3 scenarios to consider. +# +# 1. If you have data encrypted with previous Rails versions, and you have +# +config.active_support.key_generator_hash_digest_class+ configured as SHA1 (the default +# before Rails 7.0), you need to configure SHA-1 for Active Record Encryption too: +#++ +# Rails.application.config.active_record.encryption.hash_digest_class = OpenSSL::Digest::SHA1 +# +# 2. If you have +config.active_support.key_generator_hash_digest_class+ configured as SHA256 (the new default +# in 7.0), then you need to configure SHA-256 for Active Record Encryption: +#++ +# Rails.application.config.active_record.encryption.hash_digest_class = OpenSSL::Digest::SHA256 +# +# 3. If you don't currently have data encrypted with Active Record encryption, you can disable this setting to +# configure the default behavior starting 7.1+: +#++ +# Rails.application.config.active_record.encryption.support_sha1_for_non_deterministic_encryption = false + +### +# No longer run after_commit callbacks on the first of multiple Active Record +# instances to save changes to the same database row within a transaction. +# Instead, run these callbacks on the instance most likely to have internal +# state which matches what was committed to the database, typically the last +# instance to save. +#++ +# Rails.application.config.active_record.run_commit_callbacks_on_first_saved_instances_in_transaction = false + +### +# Configures SQLite with a strict strings mode, which disables double-quoted string literals. +# +# SQLite has some quirks around double-quoted string literals. +# It first tries to consider double-quoted strings as identifier names, but if they don't exist +# it then considers them as string literals. Because of this, typos can silently go unnoticed. +# For example, it is possible to create an index for a non existing column. +# See https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted for more details. +#++ +# Rails.application.config.active_record.sqlite3_adapter_strict_strings_by_default = true + +### +# Disable deprecated singular associations names. +#++ +# Rails.application.config.active_record.allow_deprecated_singular_associations_name = false + +### +# Enable the Active Job `BigDecimal` argument serializer, which guarantees +# roundtripping. Without this serializer, some queue adapters may serialize +# `BigDecimal` arguments as simple (non-roundtrippable) strings. +# +# When deploying an application with multiple replicas, old (pre-Rails 7.1) +# replicas will not be able to deserialize `BigDecimal` arguments from this +# serializer. Therefore, this setting should only be enabled after all replicas +# have been successfully upgraded to Rails 7.1. +#++ +# Rails.application.config.active_job.use_big_decimal_serializer = true + +### +# Specify if an `ArgumentError` should be raised if `Rails.cache` `fetch` or +# `write` are given an invalid `expires_at` or `expires_in` time. +# Options are `true`, and `false`. If `false`, the exception will be reported +# as `handled` and logged instead. +#++ +# Rails.application.config.active_support.raise_on_invalid_cache_expiration_time = true + +### +# Specify whether Query Logs will format tags using the SQLCommenter format +# (https://open-telemetry.github.io/opentelemetry-sqlcommenter/), or using the legacy format. +# Options are `:legacy` and `:sqlcommenter`. +#++ +# Rails.application.config.active_record.query_log_tags_format = :sqlcommenter + +### +# Specify the default serializer used by `MessageEncryptor` and `MessageVerifier` +# instances. +# +# The legacy default is `:marshal`, which is a potential vector for +# deserialization attacks in cases where a message signing secret has been +# leaked. +# +# In Rails 7.1, the new default is `:json_allow_marshal` which serializes and +# deserializes with `ActiveSupport::JSON`, but can fall back to deserializing +# with `Marshal` so that legacy messages can still be read. +# +# In Rails 7.2, the default will become `:json` which serializes and +# deserializes with `ActiveSupport::JSON` only. +# +# Alternatively, you can choose `:message_pack` or `:message_pack_allow_marshal`, +# which serialize with `ActiveSupport::MessagePack`. `ActiveSupport::MessagePack` +# can roundtrip some Ruby types that are not supported by JSON, and may provide +# improved performance, but it requires the `msgpack` gem. +# +# For more information, see +# https://guides.rubyonrails.org/v7.1/configuring.html#config-active-support-message-serializer +# +# If you are performing a rolling deploy of a Rails 7.1 upgrade, wherein servers +# that have not yet been upgraded must be able to read messages from upgraded +# servers, first deploy without changing the serializer, then set the serializer +# in a subsequent deploy. +#++ +# Rails.application.config.active_support.message_serializer = :json_allow_marshal + +### +# Enable a performance optimization that serializes message data and metadata +# together. This changes the message format, so messages serialized this way +# cannot be read by older versions of Rails. However, messages that use the old +# format can still be read, regardless of whether this optimization is enabled. +# +# To perform a rolling deploy of a Rails 7.1 upgrade, wherein servers that have +# not yet been upgraded must be able to read messages from upgraded servers, +# leave this optimization off on the first deploy, then enable it on a +# subsequent deploy. +#++ +# Rails.application.config.active_support.use_message_serializer_for_metadata = true + +### +# Set the maximum size for Rails log files. +# +# `config.load_defaults 7.1` does not set this value for environments other than +# development and test. +#++ +# if Rails.env.local? +# Rails.application.config.log_file_size = 100 * 1024 * 1024 +# end + +### +# Enable raising on assignment to attr_readonly attributes. The previous +# behavior would allow assignment but silently not persist changes to the +# database. +#++ +# Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true + +### +# Enable validating only parent-related columns for presence when the parent is mandatory. +# The previous behavior was to validate the presence of the parent record, which performed an extra query +# to get the parent every time the child record was updated, even when parent has not changed. +#++ +# Rails.application.config.active_record.belongs_to_required_validates_foreign_key = false + +### +# Enable precompilation of `config.filter_parameters`. Precompilation can +# improve filtering performance, depending on the quantity and types of filters. +#++ +# Rails.application.config.precompile_filter_parameters = true + +### +# Enable before_committed! callbacks on all enrolled records in a transaction. +# The previous behavior was to only run the callbacks on the first copy of a record +# if there were multiple copies of the same record enrolled in the transaction. +#++ +# Rails.application.config.active_record.before_committed_on_all_records = true + +### +# Disable automatic column serialization into YAML. +# To keep the historic behavior, you can set it to `YAML`, however it is +# recommended to explicitly define the serialization method for each column +# rather than to rely on a global default. +#++ +# Rails.application.config.active_record.default_column_serializer = nil + +### +# Enable a performance optimization that serializes Active Record models +# in a faster and more compact way. +# +# To perform a rolling deploy of a Rails 7.1 upgrade, wherein servers that have +# not yet been upgraded must be able to read caches from upgraded servers, +# leave this optimization off on the first deploy, then enable it on a +# subsequent deploy. +#++ +# Rails.application.config.active_record.marshalling_format_version = 7.1 + +### +# Run `after_commit` and `after_*_commit` callbacks in the order they are defined in a model. +# This matches the behaviour of all other callbacks. +# In previous versions of Rails, they ran in the inverse order. +#++ +# Rails.application.config.active_record.run_after_transaction_callbacks_in_order_defined = true + +### +# Whether a `transaction` block is committed or rolled back when exited via `return`, `break` or `throw`. +#++ +# Rails.application.config.active_record.commit_transaction_on_non_local_return = true + +### +# Controls when to generate a value for has_secure_token declarations. +#++ +# Rails.application.config.active_record.generate_secure_token_on = :initialize + +### +# ** Please read carefully, this must be configured in config/application.rb ** +# +# Change the format of the cache entry. +# +# Changing this default means that all new cache entries added to the cache +# will have a different format that is not supported by Rails 7.0 +# applications. +# +# Only change this value after your application is fully deployed to Rails 7.1 +# and you have no plans to rollback. +# When you're ready to change format, add this to `config/application.rb` (NOT +# this file): +# config.active_support.cache_format_version = 7.1 + + +### +# Configure Action View to use HTML5 standards-compliant sanitizers when they are supported on your +# platform. +# +# `Rails::HTML::Sanitizer.best_supported_vendor` will cause Action View to use HTML5-compliant +# sanitizers if they are supported, else fall back to HTML4 sanitizers. +# +# In previous versions of Rails, Action View always used `Rails::HTML4::Sanitizer` as its vendor. +#++ +# Rails.application.config.action_view.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor + + +### +# Configure Action Text to use an HTML5 standards-compliant sanitizer when it is supported on your +# platform. +# +# `Rails::HTML::Sanitizer.best_supported_vendor` will cause Action Text to use HTML5-compliant +# sanitizers if they are supported, else fall back to HTML4 sanitizers. +# +# In previous versions of Rails, Action Text always used `Rails::HTML4::Sanitizer` as its vendor. +#++ +# Rails.application.config.action_text.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor + + +### +# Configure the log level used by the DebugExceptions middleware when logging +# uncaught exceptions during requests. +#++ +# Rails.application.config.action_dispatch.debug_exception_log_level = :error + + +### +# Configure the test helpers in Action View, Action Dispatch, and rails-dom-testing to use HTML5 +# parsers. +# +# Nokogiri::HTML5 isn't supported on JRuby, so JRuby applications must set this to :html4. +# +# In previous versions of Rails, these test helpers always used an HTML4 parser. +#++ +# Rails.application.config.dom_testing_default_html_version = :html5 diff --git a/spec/fixtures/rails7_users_app/config/initializers/permissions_policy.rb b/spec/fixtures/rails7_users_app/config/initializers/permissions_policy.rb index 00f64d71..7db3b957 100644 --- a/spec/fixtures/rails7_users_app/config/initializers/permissions_policy.rb +++ b/spec/fixtures/rails7_users_app/config/initializers/permissions_policy.rb @@ -1,11 +1,13 @@ +# Be sure to restart your server when you modify this file. + # Define an application-wide HTTP permissions policy. For further -# information see https://developers.google.com/web/updates/2018/06/feature-policy -# -# Rails.application.config.permissions_policy do |f| -# f.camera :none -# f.gyroscope :none -# f.microphone :none -# f.usb :none -# f.fullscreen :self -# f.payment :self, "https://secure.example.com" +# information see: https://developers.google.com/web/updates/2018/06/feature-policy + +# Rails.application.config.permissions_policy do |policy| +# policy.camera :none +# policy.gyroscope :none +# policy.microphone :none +# policy.usb :none +# policy.fullscreen :self +# policy.payment :self, "https://secure.example.com" # end diff --git a/spec/fixtures/rails7_users_app/config/routes.rb b/spec/fixtures/rails7_users_app/config/routes.rb index b2b468ad..7a953b75 100644 --- a/spec/fixtures/rails7_users_app/config/routes.rb +++ b/spec/fixtures/rails7_users_app/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + post "/graphql", to: "graphql#execute" + namespace :api do resources :users, only: %i[index create] end diff --git a/spec/fixtures/rails7_users_app/spec/controllers/graphql_controller_spec.rb b/spec/fixtures/rails7_users_app/spec/controllers/graphql_controller_spec.rb new file mode 100644 index 00000000..0492d369 --- /dev/null +++ b/spec/fixtures/rails7_users_app/spec/controllers/graphql_controller_spec.rb @@ -0,0 +1,65 @@ +require "rails_helper" +require "rack/test" + +RSpec.describe GraphqlController, type: :controller do + let(:isolation_level) { nil } + + before do + User.create(login: "alice") + User.create(login: "bob") + User.create(login: "charles") + + @previous_isolation_level = ActiveSupport::IsolatedExecutionState.isolation_level + ActiveSupport::IsolatedExecutionState.isolation_level = isolation_level + end + + after do + ActiveSupport::IsolatedExecutionState.isolation_level = @previous_isolation_level + end + + context "with thread-based connection pool" do + let(:isolation_level) { :thread } + + it "returns the users without leaking the connection pool 6 times" do + 6.times do |i| + post :execute, params: { query: "query { users { id, login } }" } + expect(response.status).to eq(200) + + results = JSON.parse(response.body) + + users = results["data"]["users"] + expect(users.map { |r| r["login"] }.sort).to eq(%w[alice bob charles]) + + stats = results["data"]["connection_pool_stats"].symbolize_keys + expect(stats[:size]).to eq(5) + expect(stats[:connections]).to eq(1) + expect(stats[:dead]).to eq(0) + expect(stats[:idle]).to eq(0) + expect(stats[:waiting]).to eq(0) + end + end + end + + context "with fiber-based connection pool" do + let(:isolation_level) { :fiber } + + it "returns the users without leaking the connection pool 6 times" do + 6.times do |i| + post :execute, params: { query: "query { users { id, login } }" } + expect(response.status).to eq(200) + + results = JSON.parse(response.body) + + users = results["data"]["users"] + expect(users.map { |r| r["login"] }.sort).to eq(%w[alice bob charles]) + + stats = results["data"]["connection_pool_stats"].symbolize_keys + expect(stats[:size]).to eq(5) + expect(stats[:connections]).to eq(1) + expect(stats[:dead]).to eq(0) + expect(stats[:idle]).to eq(0) + expect(stats[:waiting]).to eq(0) + end + end + end +end diff --git a/spec/record_sql_rails_pg_spec.rb b/spec/record_sql_rails_pg_spec.rb index 4aa94ab8..63f2a48c 100644 --- a/spec/record_sql_rails_pg_spec.rb +++ b/spec/record_sql_rails_pg_spec.rb @@ -43,12 +43,25 @@ def self.check_queries(cases) def run_specs(orm_module) @app.prepare_db + + run_user_specs(orm_module) + run_graphql_specs if rails_version >= 7 + end + + def run_user_specs(orm_module) @app.run_cmd \ './bin/rspec spec/controllers/users_controller_api_spec.rb:8 spec/controllers/users_controller_api_spec.rb:29', 'ORM_MODULE' => orm_module, 'RAILS_ENV' => 'test' end + def run_graphql_specs + @app.run_cmd \ + './bin/rspec spec/controllers/graphql_controller.rb', + 'ORM_MODULE' => 'activerecord', + 'RAILS_ENV' => 'test' + end + let(:appmap_json) { File.join tmpdir, "appmap/rspec/#{test_case}.appmap.json" } let(:appmap) { JSON.parse(File.read(appmap_json)) } let(:tmpdir) { app.tmpdir }