Skip to content

DGI9-593: Perform user swapping during batch invocation.#33

Merged
MorganDawe merged 1 commit into
mainfrom
fix/controlled-file-enumeration-via-user-switch
Feb 9, 2026
Merged

DGI9-593: Perform user swapping during batch invocation.#33
MorganDawe merged 1 commit into
mainfrom
fix/controlled-file-enumeration-via-user-switch

Conversation

@adam-vessey

@adam-vessey adam-vessey commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

Alternate approach to #32 , since it was realized that we already do similar things elsewhere.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced error handling and recovery for batch fixity check operations, with improved logging for troubleshooting
    • Increased robustness of fixity operations through better exception handling and graceful error management

@adam-vessey adam-vessey added the patch Backwards compatible bug fixes. label Feb 5, 2026
@coderabbitai

coderabbitai Bot commented Feb 5, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Admin context switching for fixity operations is introduced, where the admin user (ID 1) is loaded, account context is switched to perform fixity operations, then switched back using a finally block. Error handling added for admin load and view load failures. The batch_size parameter is cast to integer and its public signature relaxed to accept null values.

Changes

Cohort / File(s) Summary
Admin Context Switching for Fixity Operations
src/FixityCheckBatchCheck.php
Adds admin user loading and account context switching wrapping fixity operations with finally-block cleanup. Introduces error handling for admin load and view load failures, per-row exception handling with logging, and batch_size type casting from config/input to integer.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • SUP-6848: Misc fixes #27: Broadens nullable signatures and centralizes switch-back behavior for account context management in the same file, complementing the explicit admin user switching pattern introduced here.

Suggested reviewers

  • jordandukart

Poem

🐰 Hops through the fixity fields with admin's might,
Switching contexts left and right,
Finally blocks ensure the path is clear,
Error handling brings us cheer,
Admin ops secure and tight! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: performing user swapping during batch invocation, which aligns with the code modifications adding admin context switching.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/controlled-file-enumeration-via-user-switch

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@src/FixityCheckBatchCheck.php`:
- Around line 134-137: When the admin account fails to load in
FixityCheckBatchCheck (i.e., $admin_user is not an instance of
AccountInterface), set $context['finished'] = TRUE before returning so the batch
runner does not retry indefinitely; update the failure branch that currently
calls $logger->error('Failed to load the admin account to switch.') to also
assign $context['finished'] = TRUE (and then return) so the operation is marked
finished.
- Around line 28-29: build() currently casts the configured batch size to int
which can yield 0 or negative and thus create a limit:0; validate $batch_size
after casting (from SettingsForm::CONFIG_NAME / SettingsForm::BATCH_SIZE or the
passed $batch_size) and enforce a sensible minimum (e.g. if ((int)$batch_size <
1) $batch_size = <default>), ensuring $batch_size is at least 1 before it is
used by FixityCheckBatchCheck::build and any view query.
- Around line 147-150: When the view fails to load in FixityCheckBatchCheck (the
branch where if (!$view) { ... return; }), set $context['finished'] to a
completed value before returning to avoid Drupal batch retries; e.g. assign
$context['finished'] = 1 (or true) and optionally record an error in
$context['results'] (e.g. increment an errors counter or add a message) so the
batch marks the operation finished and the failure is visible in logs; update
the early-return branch that references $view and $logger to perform these
assignments before returning.

Comment on lines 28 to +29
public static function build(?array $fids = NULL, bool $force = FALSE, ?int $batch_size = NULL) {
$batch_size = is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size;
$batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Consider adding validation for zero/negative batch_size.

If the config value is missing or invalid, the (int) cast could result in batch_size = 0, which would cause the view to return no results (due to limit: 0) and operations to complete immediately without processing anything.

🛡️ Suggested validation
-    $batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);
+    $batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);
+    if ($batch_size <= 0) {
+      throw new \InvalidArgumentException("Batch size must be a positive integer.");
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static function build(?array $fids = NULL, bool $force = FALSE, ?int $batch_size = NULL) {
$batch_size = is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size;
$batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);
public static function build(?array $fids = NULL, bool $force = FALSE, ?int $batch_size = NULL) {
$batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);
if ($batch_size <= 0) {
throw new \InvalidArgumentException("Batch size must be a positive integer.");
}
🤖 Prompt for AI Agents
In `@src/FixityCheckBatchCheck.php` around lines 28 - 29, build() currently casts
the configured batch size to int which can yield 0 or negative and thus create a
limit:0; validate $batch_size after casting (from SettingsForm::CONFIG_NAME /
SettingsForm::BATCH_SIZE or the passed $batch_size) and enforce a sensible
minimum (e.g. if ((int)$batch_size < 1) $batch_size = <default>), ensuring
$batch_size is at least 1 before it is used by FixityCheckBatchCheck::build and
any view query.

Comment thread src/FixityCheckBatchCheck.php
Comment thread src/FixityCheckBatchCheck.php
Comment on lines +23 to +29
* elapsed since the last check has not exceeded the required threshold.
* @param int|null $batch_size
* The number of files to process at a time.
* If not specified it will default to the module's configuration.
*/
public static function build(?array $fids = NULL, bool $force = FALSE, ?int $batch_size = NULL) {
$batch_size = is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size;
$batch_size = (int) (is_null($batch_size) ? \Drupal::config(SettingsForm::CONFIG_NAME)->get(SettingsForm::BATCH_SIZE) : $batch_size);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Few fixes that static analysis/my IDE was flagging that caught my attention.

$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$admin_user = $user_storage->load(1);
if (!($admin_user instanceof AccountInterface)) {
$logger->error('Failed to load the admin account to switch.');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unsure if this would ever be seen, given oddities with batches run via Drush and logs, but given this may be used outside of Drush, could make sense?

Should be a rare error condition, in any case.

Comment thread src/FixityCheckBatchCheck.php
@MorganDawe MorganDawe merged commit 693cbc2 into main Feb 9, 2026
2 checks passed
@MorganDawe MorganDawe deleted the fix/controlled-file-enumeration-via-user-switch branch February 9, 2026 20:12
@github-actions

github-actions Bot commented Feb 9, 2026

Copy link
Copy Markdown

Tag generated by PR: v1.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Backwards compatible bug fixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants