-
-
Notifications
You must be signed in to change notification settings - Fork 452
Sanitize Input Data: Trim Whitespace on Key Backend Models for Consistent Data Storage #4956
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
base: main
Are you sure you want to change the base?
Changes from all commits
aa84fc8
2cb504f
2553366
b40b707
2b89804
5466aaf
fdc5bc0
89ca17d
83f62c1
3a816d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -612,4 +612,30 @@ public function getInternallyForwarded() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $this->_internallyForwarded; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Retrieve a parameter from request (GET/POST) and trim whitespace for string and array values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param mixed $default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return mixed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function getParam($key, $default = null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get the parameter value from parent (Zend_Controller_Request_Http) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $value = parent::getParam($key, $default); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Trim whitespace for string values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (is_string($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $value = trim($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For array values, trim each string element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (is_array($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $value = array_map(function ($v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return is_string($v) ? trim($v) : $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, $value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+633
to
+640
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For array values, trim each string element | |
| if (is_array($value)) { | |
| $value = array_map(function ($v) { | |
| return is_string($v) ? trim($v) : $v; | |
| }, $value); | |
| } | |
| return $value; | |
| } | |
| // For array values, trim each string element recursively | |
| if (is_array($value)) { | |
| $value = $this->_trimRecursive($value); | |
| } | |
| return $value; | |
| } | |
| /** | |
| * Recursively trim all string values in an array, with a depth limit to prevent excessive recursion. | |
| * | |
| * @param mixed $value | |
| * @param int $depth | |
| * @return mixed | |
| */ | |
| private function _trimRecursive($value, $depth = 10) | |
| { | |
| if ($depth < 0) { | |
| // Prevent excessive recursion | |
| return $value; | |
| } | |
| if (is_array($value)) { | |
| foreach ($value as $k => $v) { | |
| $value[$k] = $this->_trimRecursive($v, $depth - 1); | |
| } | |
| return $value; | |
| } | |
| if (is_string($value)) { | |
| return trim($value); | |
| } | |
| return $value; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -124,11 +124,19 @@ public function getOrder() | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Before object save manipulations | ||||||||||||||||||||||||||||||
| * Trim whitespace for all string data to prevent unwanted spaces on save | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @return $this | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| protected function _beforeSave() | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| // Trim all string fields before saving (for clean data storage) | ||||||||||||||||||||||||||||||
| foreach ($this->getData() as $key => $value) { | ||||||||||||||||||||||||||||||
| if (is_string($value)) { | ||||||||||||||||||||||||||||||
| $this->setData($key, trim($value)); | ||||||||||||||||||||||||||||||
|
Comment on lines
+133
to
+136
|
||||||||||||||||||||||||||||||
| // Trim all string fields before saving (for clean data storage) | |
| foreach ($this->getData() as $key => $value) { | |
| if (is_string($value)) { | |
| $this->setData($key, trim($value)); | |
| // Trim only known string fields before saving (for clean data storage) | |
| $fieldsToTrim = array( | |
| 'firstname', 'lastname', 'middlename', 'prefix', 'suffix', | |
| 'company', 'street', 'city', 'region', 'postcode', 'country_id', | |
| 'telephone', 'fax', 'email' | |
| ); | |
| foreach ($fieldsToTrim as $field) { | |
| $value = $this->getData($field); | |
| if (is_string($value)) { | |
| $this->setData($field, trim($value)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,15 @@ protected function _construct() | |
| */ | ||
| protected function _beforeSave() | ||
| { | ||
| // Trim whitespace for all relevant fields before validation and save | ||
| $this->setCode(trim((string) $this->getCode())); | ||
| $this->setTaxCountryId(trim((string) $this->getTaxCountryId())); | ||
| $this->setTaxRegionId(trim((string) $this->getTaxRegionId())); | ||
| $this->setTaxPostcode(trim((string) $this->getTaxPostcode())); | ||
| $this->setRate(trim((string) $this->getRate())); | ||
| $this->setZipFrom(trim((string) $this->getZipFrom())); | ||
| $this->setZipTo(trim((string) $this->getZipTo())); | ||
|
Comment on lines
+73
to
+79
|
||
|
|
||
| if ($this->getCode() === '' || $this->getTaxCountryId() === '' || $this->getRate() === '' | ||
| || $this->getZipIsRange() && ($this->getZipFrom() === '' || $this->getZipTo() === '') | ||
| ) { | ||
|
|
||
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.
Using array_map with an anonymous function for every array parameter could impact performance for large arrays. Consider using a foreach loop or array_walk for better performance, especially since this method is called frequently for request parameters.