Skip to content

Conversation

@karkowg
Copy link
Contributor

@karkowg karkowg commented Jan 12, 2026

The package provides:

  • A BitmaskCast for Eloquent models to seamlessly cast integer values to and from Bitmask objects.
  • Eloquent scopes (HasBitmaskScopes) for querying bitmask columns with various conditions (has flag, has all flags, has any flag, etc.).
  • A make:bitmask-flags Artisan command to generate bitmask enum classes.
  • A ValidBitmask validation rule to ensure input values conform to bitmask constraints, optionally tied to an enum.
  • Blade directives for conditional rendering based on bitmask flags.
  • Custom Blueprint macros for defining bitmask columns in migrations.
  • Comprehensive unit tests covering all functionalities.

Greptile Overview

Greptile Summary

This PR adds Laravel integration for the gksh/bitmask package, providing Eloquent casts, query scopes, validation rules, Blade directives, and migration helpers for working with bitmask fields.

Key Changes:

  • Added BitmaskCast for automatic casting between integers and Bitmask objects in Eloquent models
  • Implemented HasBitmaskScopes trait with query scopes for filtering records by bitmask flags
  • Created ValidBitmask validation rule with optional enum-based range checking
  • Added make:bitmask-flags Artisan command for generating bitmask enum classes
  • Registered Blade directives (@hasBitmaskFlag, @hasAnyBitmaskFlag, @hasAllBitmaskFlags)
  • Added Blueprint macros (tinyBitmask, smallBitmask, mediumBitmask, bitmask) for migrations
  • Comprehensive test coverage using Pest

Critical Issues Found:

  • SQL injection vulnerability in all HasBitmaskScopes methods - column names are interpolated without sanitization
  • laravel/prompts dependency is in require-dev but used in production code
  • array_sum() used instead of bitwise OR for combining bitmask values (works but semantically incorrect)

Confidence Score: 1/5

  • This PR has critical security vulnerabilities that must be fixed before merging
  • Score reflects multiple SQL injection vulnerabilities in HasBitmaskScopes trait that could allow attackers to execute arbitrary SQL queries. Additionally, incorrect dependency configuration will cause runtime errors in production environments.
  • Pay immediate attention to src/Concerns/HasBitmaskScopes.php (SQL injection) and composer.json (dependency misplacement)

Important Files Changed

File Analysis

Filename Score Overview
src/Concerns/HasBitmaskScopes.php 1/5 Critical SQL injection vulnerabilities in all scope methods - column names need sanitization. Also uses array_sum instead of bitwise OR.
composer.json 2/5 laravel/prompts incorrectly placed in require-dev instead of require - will cause runtime errors in production.
src/Casts/BitmaskCast.php 5/5 Clean implementation of Eloquent cast - properly handles null values and size parameters.
src/Commands/MakeBitmaskFlagsCommand.php 4/5 Well-structured command for generating bitmask enums - good validation and uses proper bit shifting syntax.
src/Rules/ValidBitmask.php 5/5 Solid validation rule - correctly uses bitwise OR for max value calculation, handles edge cases properly.
src/TransistorServiceProvider.php 5/5 Well-organized service provider - registers all features correctly with proper type hints and PHPStan annotations.

Sequence Diagram

sequenceDiagram
    participant App as Laravel Application
    participant SP as TransistorServiceProvider
    participant Model as Eloquent Model
    participant Cast as BitmaskCast
    participant Scope as HasBitmaskScopes
    participant DB as Database
    participant Bitmask as Bitmask Object

    Note over App,SP: Service Provider Registration
    App->>SP: boot()
    SP->>SP: registerCommands()
    SP->>SP: registerBladeDirectives()
    SP->>SP: registerValidationRules()
    SP->>SP: registerBlueprintMacros()

    Note over Model,Bitmask: Eloquent Cast (Retrieval)
    Model->>Cast: get(model, key, value, attributes)
    Cast->>Cast: Check if value is null
    Cast->>Cast: Convert to int
    Cast->>Bitmask: Create Bitmask instance based on size
    Bitmask-->>Model: Return Bitmask object

    Note over Model,Bitmask: Eloquent Cast (Storage)
    Model->>Cast: set(model, key, value, attributes)
    Cast->>Cast: Check value type
    alt value is Bitmask
        Cast->>Bitmask: value()
        Bitmask-->>Cast: Return integer
    else value is int
        Cast->>Cast: Cast to int
    end
    Cast-->>Model: Return integer for storage

    Note over Model,DB: Query Scopes
    App->>Model: whereHasBitmaskFlag('permissions', flag)
    Model->>Scope: scopeWhereHasBitmaskFlag(query, column, flag)
    Scope->>Scope: maskValue(flag)
    Scope->>DB: whereRaw("column & ? = ?", [value, value])
    DB-->>App: Return filtered results
Loading

(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!

The package provides:
- A `BitmaskCast` for Eloquent models to seamlessly cast integer values
  to and from `Bitmask` objects.
- Eloquent scopes (`HasBitmaskScopes`) for querying bitmask columns with
  various conditions (has flag, has all flags, has any flag, etc.).
- A `make:bitmask-flags` Artisan command to generate bitmask enum
  classes.
- A `ValidBitmask` validation rule to ensure input values conform to
  bitmask constraints, optionally tied to an enum.
- Blade directives for conditional rendering based on bitmask flags.
- Custom Blueprint macros for defining bitmask columns in migrations.
- Comprehensive unit tests covering all functionalities.
@karkowg
Copy link
Contributor Author

karkowg commented Jan 12, 2026

@greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

7 files reviewed, 6 comments

Edit Code Review Agent Settings | Greptile

{
$value = maskValue($flag);

return $query->whereRaw("{$column} & ? = ?", [$value, $value]);
Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: $column is directly interpolated into raw SQL without sanitization. An attacker could pass malicious input like permissions; DROP TABLE users-- as the column name.

Wrap the column name with backticks or use DB::raw() to prevent injection:

Suggested change
return $query->whereRaw("{$column} & ? = ?", [$value, $value]);
return $query->whereRaw("`{$column}` & ? = ?", [$value, $value]);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Concerns/HasBitmaskScopes.php
Line: 28:28

Comment:
SQL injection vulnerability: `$column` is directly interpolated into raw SQL without sanitization. An attacker could pass malicious input like `permissions; DROP TABLE users--` as the column name.

Wrap the column name with backticks or use `DB::raw()` to prevent injection:

```suggestion
        return $query->whereRaw("`{$column}` & ? = ?", [$value, $value]);
```

How can I resolve this? If you propose a fix, please make it concise.

{
$value = $this->combinedMaskValue($flags);

return $query->whereRaw("{$column} & ? = ?", [$value, $value]);
Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: $column needs sanitization.

Suggested change
return $query->whereRaw("{$column} & ? = ?", [$value, $value]);
return $query->whereRaw("`{$column}` & ? = ?", [$value, $value]);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Concerns/HasBitmaskScopes.php
Line: 42:42

Comment:
SQL injection vulnerability: `$column` needs sanitization.

```suggestion
        return $query->whereRaw("`{$column}` & ? = ?", [$value, $value]);
```

How can I resolve this? If you propose a fix, please make it concise.

{
$value = $this->combinedMaskValue($flags);

return $query->whereRaw("{$column} & ? != 0", [$value]);
Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: $column needs sanitization.

Suggested change
return $query->whereRaw("{$column} & ? != 0", [$value]);
return $query->whereRaw("`{$column}` & ? != 0", [$value]);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Concerns/HasBitmaskScopes.php
Line: 56:56

Comment:
SQL injection vulnerability: `$column` needs sanitization.

```suggestion
        return $query->whereRaw("`{$column}` & ? != 0", [$value]);
```

How can I resolve this? If you propose a fix, please make it concise.

{
$value = maskValue($flag);

return $query->whereRaw("{$column} & ? != ?", [$value, $value]);
Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: $column needs sanitization.

Suggested change
return $query->whereRaw("{$column} & ? != ?", [$value, $value]);
return $query->whereRaw("`{$column}` & ? != ?", [$value, $value]);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Concerns/HasBitmaskScopes.php
Line: 69:69

Comment:
SQL injection vulnerability: `$column` needs sanitization.

```suggestion
        return $query->whereRaw("`{$column}` & ? != ?", [$value, $value]);
```

How can I resolve this? If you propose a fix, please make it concise.

{
$value = $this->combinedMaskValue($flags);

return $query->whereRaw("{$column} & ? = 0", [$value]);
Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: $column needs sanitization.

Suggested change
return $query->whereRaw("{$column} & ? = 0", [$value]);
return $query->whereRaw("`{$column}` & ? = 0", [$value]);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Concerns/HasBitmaskScopes.php
Line: 83:83

Comment:
SQL injection vulnerability: `$column` needs sanitization.

```suggestion
        return $query->whereRaw("`{$column}` & ? = 0", [$value]);
```

How can I resolve this? If you propose a fix, please make it concise.

"illuminate/support": "^10.0|^11.0|^12.0"
},
"require-dev": {
"laravel/pint": "^1.18",
Copy link

Choose a reason for hiding this comment

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

laravel/prompts is used in production code (MakeBitmaskFlagsCommand) but listed in require-dev. Move it to require:

Suggested change
"laravel/pint": "^1.18",
"illuminate/support": "^10.0|^11.0|^12.0",
"laravel/prompts": "^0.1|^0.2|^0.3"
Prompt To Fix With AI
This is a comment left during a code review.
Path: composer.json
Line: 20:20

Comment:
`laravel/prompts` is used in production code (`MakeBitmaskFlagsCommand`) but listed in `require-dev`. Move it to `require`:

```suggestion
        "illuminate/support": "^10.0|^11.0|^12.0",
        "laravel/prompts": "^0.1|^0.2|^0.3"
```

How can I resolve this? If you propose a fix, please make it concise.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants