Repository navigation
Refuse a too-long public answer before saving it #293
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2917f5
Refuse a too-long public answer before saving it
PhilReinking df99601
Add PR number to changelog line
PhilReinking cc75cd9
Check nested answer lists, fix changelog wording
PhilReinking 5bf280a
Changelog: say plain letters, move line to end of Fixed
PhilReinking 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace App\Rules; | ||
|
|
||
| use App\Models\FormSessionResponse; | ||
| use Closure; | ||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
|
|
||
| class AnswerFitsColumn implements ValidationRule | ||
| { | ||
| public function validate(string $attribute, mixed $value, Closure $fail): void | ||
| { | ||
| if ($this->tooLong($value)) { | ||
| $fail('This answer is too long. Please shorten it and send it again.'); | ||
| } | ||
| } | ||
|
|
||
| // walks lists at any depth, the way FormBlock::submit does | ||
| private function tooLong(mixed $value): bool | ||
| { | ||
| if (! is_array($value)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (has_string_keys($value)) { | ||
| // measured the way it is stored: encrypted, or as JSON in debug mode | ||
| return strlen(FormSessionResponse::toStored($value['payload'] ?? null)) > FormSessionResponse::VALUE_MAX_BYTES; | ||
| } | ||
|
|
||
| return collect($value)->contains(fn ($answer) => $this->tooLong($answer)); | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A list inside a list skips this check and gets a 500 again.
FormBlock::submitwalks lists at any depth (app/Models/FormBlock.php:211), but this rule looks only one level deep. For[[{actionId, payload: 70,000 × "a"}]],$answer['payload']is null, so the check passes and MariaDB throws SQLSTATE 22001. On main, the 30,000 cap measured the whole value, so the same request got a 422. Only a hand-made API call sends this, and nothing is saved, but it is the 500 this PR removes.Walk the answer the same way the save does:
With this, that request gets the 422.
FormSessionTest.phpplus my checks (checkbox lists, numbers, null, a 33,000-byte answer) pass: 38 tests on MariaDB, debug on and off.