-
-
Notifications
You must be signed in to change notification settings - Fork 449
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?
Conversation
Like that PR. 👍 Do you want to add some tests? (PHPunit/cypress) |
It is fine to me. |
Removed redundant comment block from the save manipulations method.
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.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
|
Problem
This issue was reported first time in 2021 here #1653.
OpenMage frontend and backend forms could allow administrators and users to enter data for addresses, tax rates, tags, and admin accounts. Without explicit whitespace sanitization, leading or trailing spaces may be accidentally included in critical fields. This can result in:
Solution
This PR trims leading and trailing whitespace from string data in several key models. Trimming is performed at the model level before saving, ensuring form input is stored cleanly and consistently in the database.
Modified Files and Rationale
Tag.php
Tags may be created with accidental whitespace, resulting in duplicates or unreliable searching. Trimming ensures consistency.
Rate.php
Tax rates configured in the backend can include spaces, causing validation failures or configuration errors. Trimming guarantees correct setup.
Why is string conversion used with trim() before saving in this file? In this method, several fields (such as code, country, region, postcode, zip range, and rate) are populated from backend forms, where their values may be null, numeric, or string depending on user input, system defaults, or import sources. The PHP trim() function only operates safely on strings. If trim() is called on null or a numeric value, it will silently cast the value to string (e.g., null becomes an empty string, an integer becomes its string representation) and remove any leading or trailing whitespace. This prevents PHP warnings or errors, and ensures all input data is consistently sanitized before storage. Explicitly converting to string before trim() guarantees:
This approach is safe and robust for all typical Magento backend input scenarios, and ensures clean, reliable data storage.
User.php
Whitespace in admin account fields can cause login and notification issues. Passwords are intentionally left untouched.
Address.php
Addresses are manually entered and prone to accidental spaces. Trimming ensures clean data for shipping, validation, and export.
Http.php
This sanitizes input at the earliest stage, preventing whitespace propagation into business logic and storage.
Testing and Compatibility
Summary
This PR standardizes data sanitization for critical backend models in Magento OpenMage LTS, preventing data corruption, improving user experience, and strengthening downstream integrations by ensuring stored values are free of unintended whitespace. Password fields, as well as inputs and textareas where users may intentionally use leading or trailing spaces (such as CMS content, product descriptions, or other free-form text fields), are intentionally left unmodified. This preserves the user's intended formatting and content. Trimming is only applied to fields where whitespace is unlikely to be deliberate and may cause data integrity issues, such as usernames, emails, codes, and other identifiers.