Fix auditable-tables shield crashing on insert_all/upsert_all#158
Merged
Conversation
The connection shield assumes its first argument is a SQL String and calls `sql.b`, but `exec_insert_all` receives an ActiveRecord::InsertAll, which doesn't respond to `#b`. So any `insert_all`/`upsert_all` run from an audited console raised `NoMethodError: undefined method 'b' for an instance of ActiveRecord::InsertAll`. It only surfaced in the console because the check is guarded by `executing_user_command?`. Coerce the argument before the auditable-tables check: for a non-String, use its target table name (`InsertAll#model.table_name`) so the regexp still detects access to audited tables. The underlying adapter still receives the original argument untouched, and string SQL is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes a console-only crash in the ActiveRecord auditable-tables shield when exec_insert_all receives an ActiveRecord::InsertAll object (which doesn’t implement #b) by normalizing the “SQL” input used for the auditable-table regexp check.
Tip
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.
Changes:
- Introduced
auditable_sql(sql)to safely derive a String for audited-table detection (supportsStringand InsertAll-like objects viamodel.table_namefallback). - Updated the guarded adapter method wrappers to use
auditable_sql(sql)instead of calling#bdirectly on the first argument. - Added regression tests covering InsertAll-shaped args for both auditable and non-auditable tables, plus existing string-SQL behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/console1984/ext/active_record/protected_auditable_tables.rb | Normalizes the first adapter argument into a String before applying the auditable-table regexp check to avoid #b crashes on non-String inputs. |
| test/ext/active_record/protected_auditable_tables_test.rb | Adds targeted tests ensuring exec_insert_all works with InsertAll-like inputs and still blocks access to auditable tables. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The auditable-tables shield (
ProtectedAuditableTables) wraps the connection's query methods and does:It assumes
args.firstis a SQL String. Butexec_insert_allreceives anActiveRecord::InsertAll, which doesn't respond to#b. So anyinsert_all/upsert_allrun from an audited console raises:It's invisible in normal web/job traffic because the check is short-circuited by
executing_user_command?— it only bites in the console. In practice it blows up routine maintenance: e.g. updating a record whose callbacksinsert_all(assignment summaries, backlinks, …) dies mid-way, sometimes rolling back the whole transaction.Fix
Coerce the argument before the auditable-tables check. For a non-String, use its target table name (
InsertAll#model.table_name) so the regexp still correctly detects — and forbids — access to audited tables. The underlying adapter still receives the original argument untouched, and string SQL behavior is unchanged.Tests
Added
test/ext/active_record/protected_auditable_tables_test.rb:InsertAll-shaped arg for a non-auditable table is allowed and passed through untouched (the regression);InsertAllfor an auditable table is still forbidden;Full suite: 86 runs, 374 assertions, 0 failures, 0 errors.
bin/rubocopclean.🤖 Generated with Claude Code