Repository navigation
FOUR-34303 SQLi /api/1.0/processes/{process}/aggregation #9130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devmiguelangel
wants to merge
10
commits into
develop
Choose a base branch
from
FOUR-34303
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f27a9db
feat: add JsonDataPath class for safe mysql json path construction
devmiguelangel 15dcd1f
test: add unit tests for JsonDataPath class validation and quoting
devmiguelangel a6b6c5f
refactor: enhance filter field validation in BaseFilter using JsonDat…
devmiguelangel 106cedf
feat: implement aggregation JSON path resolution in Process model
devmiguelangel 56ca127
feat: add AggregationVariable validation rule and integrate it into P…
devmiguelangel 130ff6f
test: add unit tests for AggregationVariable validation rule
devmiguelangel 4360371
feat: integrate HasAggregationAttribute trait into Process and Proces…
devmiguelangel 6b2ccfa
test: enhance aggregation validation tests to reject SQL injection pa…
devmiguelangel d86e1fd
feat: enhance aggregation handling in ProcessController and ProcessVe…
devmiguelangel 96d1fd9
fix(sq): replace count check with empty check in JsonDataPath
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Rules; | ||
|
|
||
| use Closure; | ||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use ProcessMaker\Support\JsonDataPath; | ||
|
|
||
| /** | ||
| * Validates the variable name used to aggregate request data in the process stages. | ||
| * | ||
| * Accepts dot separated variable names (e.g. "amount", "grant.total"). Mirrors the modeler | ||
| * validation in AgregationProperty.vue so the UI and the API agree. | ||
| */ | ||
| class AggregationVariable implements ValidationRule | ||
| { | ||
| /** | ||
| * Run the validation rule. | ||
| * | ||
| * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
| */ | ||
| public function validate(string $attribute, mixed $value, Closure $fail): void | ||
| { | ||
| if (!is_string($value) || !JsonDataPath::isValid($value)) { | ||
| $fail(__('The :attribute must be a valid variable name.', ['attribute' => $attribute])); | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Support; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| /** | ||
| * Builds a safe MySQL JSON path (e.g. $."a"."b") from user supplied key names. | ||
| * | ||
| * Every segment is validated against a strict allow-list and then wrapped in double quotes, | ||
| * so key names can never be interpreted as path syntax. The returned path is meant to be | ||
| * passed as a bound parameter to JSON_EXTRACT(), never interpolated into raw SQL. | ||
| */ | ||
| final class JsonDataPath | ||
| { | ||
| /** | ||
| * Any Unicode letter or number plus underscore, hyphen and space. Used by advanced filters. | ||
| */ | ||
| public const FILTER_SEGMENT = '/^[\p{L}\p{N}_\- ]+$/u'; | ||
|
|
||
| /** | ||
| * Variable-name style segments. Mirrors the modeler AgregationProperty.vue validation. | ||
| */ | ||
| public const VARIABLE_SEGMENT = '/^[A-Za-z][0-9A-Za-z_$]*$/'; | ||
|
|
||
| /** | ||
| * @param array<int, string> $segments | ||
| * | ||
| * @throws InvalidArgumentException when there are no segments or any segment is invalid | ||
| */ | ||
| public static function fromSegments(array $segments, string $pattern = self::VARIABLE_SEGMENT): string | ||
| { | ||
| if (empty($segments)) { | ||
| throw new InvalidArgumentException('The JSON path must contain at least one segment.'); | ||
| } | ||
|
|
||
| foreach ($segments as $segment) { | ||
| // Compare with \z semantics: reject a trailing newline that "$" would accept. | ||
| if (!is_string($segment) || $segment === '' || str_contains($segment, "\n") || !preg_match($pattern, $segment)) { | ||
| throw new InvalidArgumentException('Invalid JSON path segment.'); | ||
| } | ||
| } | ||
|
|
||
| return '$."' . implode('"."', $segments) . '"'; | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException when the dotted path is invalid | ||
| */ | ||
| public static function fromDotNotation(string $path, string $pattern = self::VARIABLE_SEGMENT): string | ||
| { | ||
| return self::fromSegments(explode('.', $path), $pattern); | ||
| } | ||
|
|
||
| public static function isValid(?string $path, string $pattern = self::VARIABLE_SEGMENT): bool | ||
| { | ||
| if ($path === null || $path === '') { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| self::fromDotNotation($path, $pattern); | ||
| } catch (InvalidArgumentException) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Traits; | ||
|
|
||
| use Illuminate\Validation\ValidationException; | ||
| use ProcessMaker\Models\Process; | ||
| use ProcessMaker\Support\JsonDataPath; | ||
|
|
||
| /** | ||
| * Guards the `aggregation` attribute of process models. | ||
| * | ||
| * The value ends up addressing a JSON key of the request data, so it must be a plain variable | ||
| * name. New writes are rejected. A copy of a value already stored before that rule existed | ||
| * (a version snapshot or a duplicated process) is replaced with the default instead, so | ||
| * saving BPMN is not blocked by a legacy row. | ||
| */ | ||
| trait HasAggregationAttribute | ||
| { | ||
| /** | ||
| * @throws ValidationException when a new value is not a valid variable name | ||
| */ | ||
| public function setAggregationAttribute(?string $value): void | ||
| { | ||
| $this->attributes['aggregation'] = static::prepareAggregation( | ||
| $value, | ||
| $this->rejectsInvalidAggregation() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Process rejects invalid names. ProcessVersion does not: every save copies the current | ||
| * aggregation onto a version, including values stored before validation existed. | ||
| */ | ||
| protected function rejectsInvalidAggregation(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @throws ValidationException when $rejectInvalid is true and the value is not a variable name | ||
| */ | ||
| public static function prepareAggregation(?string $value, bool $rejectInvalid = true): ?string | ||
| { | ||
| if ($value === null || $value === '' || JsonDataPath::isValid($value)) { | ||
| return $value; | ||
| } | ||
|
|
||
| if ($rejectInvalid) { | ||
| throw ValidationException::withMessages([ | ||
| 'aggregation' => __('The :attribute must be a valid variable name.', ['attribute' => 'aggregation']), | ||
| ]); | ||
| } | ||
|
|
||
| return Process::DEFAULT_AGGREGATION; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.