New API to concatenate an array of HTML attributes#4688
Merged
CaMer0n merged 2 commits intoe107inc:masterfrom Feb 22, 2022
Merged
New API to concatenate an array of HTML attributes#4688CaMer0n merged 2 commits intoe107inc:masterfrom
CaMer0n merged 2 commits intoe107inc:masterfrom
Conversation
`e_parse::toAttributes()` is an expansion of the formerly private method `e_form::attributes()`. Now, all client code can use `e_parse::toAttributes()` to make it easy to concatenate variable-length HTML attributes. Values are guaranteed to be encoded so that they cannot escape an HTML attribute value. All client code usages are encouraged to build HTML tags with this new method to prevent cross-site scripting (XSS) attacks and prevent breaking the HTML validity due to improperly escaped HTML attributes. This new method is an extension to `e_parse::toAttribute()`, which escaped one single HTML attribute value.
…lert box Extract all accesses of the `agree_text` pref and reformat the value into a JavaScript `alert()` box Uses the new `e_parse::toAttributes()` method Fixes: e107inc#4686
|
Code Climate has analyzed commit cf86267 and detected 1 issue on this pull request. Here's the issue category breakdown:
The test coverage on the diff in this pull request is 78.0% (80% is the threshold). This pull request will bring the total coverage in the repository to 34.4% (0.0% change). View more on Code Climate. |
Member
|
Thank you! 👍 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation and Context
Throughout e107, HTML attributes are concatenated in a cumbersome and error-prone way: simple string concatenation.
Example from
download_shortcodes::sc_download_list_link():Notice the cumbersome string concatenation:
The client code must consider whether the value of each attribute can be vulnerable to cross-site scripting (XSS) attacks. In the example above,
$this->var['download_id']and$agreetextmust be trusted not to contain a string like'></a><script type="text/javascript">alert("This would have been malicious.");</script><a href='.This insecure-by-default behavior should be discouraged, and an easy-to-use option to eliminate XSS attacks should be introduced.
Description
Enter
e_parse::toAttributes(). Let's rewrite this:As this:
This is a readable and secure alternative to the original because
e_parse::toAttributes()understands each component of the HTML attributes. It knows that the keys of the array are the HTML attribute name and that the values are all to be passed throughhtmlspecialchars(), so there is no way to perform XSS in the HTML attributes. ($imghere still needs to be trusted not to have malicious input.)To showcase this new feature, all usages of
e107::getPref()['agree_text']indownload_shortcodeshave been replaced with thee_parse::toAttributes()equivalent. This fixes #4686 as a side effect.Additionally, this feature was previewed previously in
e_form::attributes(), a private method. The body of this method has been replaced with a proxy toe_parse::toAttributes(). For legacy compatibility, a second argument,$pure, was added to skip the call toe_parse::replaceConstants()done insidee_parse::toAttribute(), because the usages ine_formdid not expect constants to be replaced.How Has This Been Tested?
e_parseTestnow has new methods demoing the newe_parse::toAttributes()feature.Types of Changes
Checklist