New API to concatenate an array of HTML attributes #4688
Merged
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$agreetext
must 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. ($img
here still needs to be trusted not to have malicious input.)To showcase this new feature, all usages of
e107::getPref()['agree_text']
indownload_shortcodes
have 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_form
did not expect constants to be replaced.How Has This Been Tested?
e_parseTest
now has new methods demoing the newe_parse::toAttributes()
feature.Types of Changes
Checklist