From 3df5bcb904a734f87fe0f56d64e3b3d5c6a2dd13 Mon Sep 17 00:00:00 2001 From: zinduolis Date: Sat, 6 Sep 2025 11:04:23 +1000 Subject: [PATCH] Refactor database migration handling in browser details and websocket hooked browser specs for improved synchronization --- .../handlers/browser_details_handler_spec.rb | 17 +++++++++++++---- .../extensions/websocket_hooked_browser_spec.rb | 10 ++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/spec/beef/core/main/handlers/browser_details_handler_spec.rb b/spec/beef/core/main/handlers/browser_details_handler_spec.rb index 14750f293..33497330b 100644 --- a/spec/beef/core/main/handlers/browser_details_handler_spec.rb +++ b/spec/beef/core/main/handlers/browser_details_handler_spec.rb @@ -10,13 +10,19 @@ require_relative '../../../../spec_helper' require_relative '../../../../support/constants' require_relative '../../../../support/beef_test' +MUTEX = Mutex.new + RSpec.describe 'Browser Details Handler', run_on_browserstack: true do before(:all) do @config = BeEF::Core::Configuration.instance db_file = @config.get('beef.database.file') print_info 'Resetting the database for BeEF.' - File.delete(db_file) if File.exist?(db_file) + + if ENV['RESET_DB'] + File.delete(db_file) if File.exist?(db_file) + end + @config.set('beef.credentials.user', 'beef') @config.set('beef.credentials.passwd', 'beef') @username = @config.get('beef.credentials.user') @@ -50,9 +56,12 @@ RSpec.describe 'Browser Details Handler', run_on_browserstack: true do # Migrate (if required) ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')] - context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate if context.needs_migration? - + MUTEX.synchronize do + context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate + end + end BeEF::Core::Migration.instance.update_db! # Spawn HTTP Server diff --git a/spec/beef/extensions/websocket_hooked_browser_spec.rb b/spec/beef/extensions/websocket_hooked_browser_spec.rb index d25ff37d9..fa5f668ba 100644 --- a/spec/beef/extensions/websocket_hooked_browser_spec.rb +++ b/spec/beef/extensions/websocket_hooked_browser_spec.rb @@ -11,6 +11,8 @@ require_relative '../../support/beef_test' require 'core/main/network_stack/websocket/websocket' require 'websocket-client-simple' +MUTEX = Mutex.new + RSpec.describe 'Browser hooking with Websockets', run_on_browserstack: true do before(:all) do @config = BeEF::Core::Configuration.instance @@ -49,8 +51,12 @@ RSpec.describe 'Browser hooking with Websockets', run_on_browserstack: true do end ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')] - context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate if context.needs_migration? + MUTEX.synchronize do + context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate + end + end BeEF::Core::Migration.instance.update_db! # Spawn HTTP Server