Make WordPress Core

Opened 9 years ago

Last modified 6 weeks ago

#43010 new enhancement

Attribute Name Escape

Reported by: joe_bopper Owned by:
Priority: normal Milestone: Awaiting Review
Component: Formatting Version:
Severity: normal Keywords: needs-testing needs-unit-tests has-patch
Cc: Focuses:

Description

The HTML5 spec allows us to arbitrarily named attributes for tags, e.g. data-my-arb-attr-name="attr value". This allows for generated attribute names and thus, a need to escape to avoid potential security implications.

I have seen several occasions of developers using esc_attr to resolve this case, however this is far from correct - the requirements of the name of an attribute are very different to that of the value, the best example of this simply being whitespace.

The requirements of an attribute name can be found here: ​https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

There is a need for an esc_attr_name function to avoid compromises in html.

I have provided a simple addition patch to wp-includes/formatting.php which should resolve this issue.

Attachments (3)

esc-attr-name.diff​ (1.1 KB ) - added by joe_bopper 9 years ago.
EscAttrName.php​ (974 bytes ) - added by joe_bopper 9 years ago.
Unit test
ticket-43010-playground.png​ (58.6 KB ) - added by arkaprabhachowdhury 3 months ago.
WordPress Playground test results for PR #12915 / ticket #43010.

Download all attachments as: .zip

Change History (18)

#1 @joe_bopper
9 years ago

  • Keywords has-patch needs-unit-tests added

#2 @joe_bopper
9 years ago

  • Focuses template coding-standards added

#3 @swissspidy
9 years ago

  • Component Formatting → General
  • Focuses template coding-standards removed

#4 @joe_bopper
9 years ago

  • Keywords needs-testing added; needs-unit-tests removed

@swissspidy Cheers for having a look. For future reference, why would this be a General component and not Formatting?

#5 @swissspidy
9 years ago

  • Component General → Formatting

Whoops, guess I've changed the component by accident. Formatting is correct. Sorry about the confusion :-) And thanks for the tests!

#6 @joe_bopper
9 years ago

No problem. :)

#7 @sc0ttkclark
5 years ago

Finding myself needing this as I dive into building form fields for WP again. Maybe we can get an eye on this for after WP 5.9 goes out. I'll try and return and get a PR w/ test set up for this to see if helps it progress forward.

#8 @sc0ttkclark
5 years ago

Should this function use dash or underscore? Right now it's using underscore and it feels like dash may be more consistent with the data-* standard.

I also was doing some checking and there are multiple areas in WP core which output attribute names without any escaping.

You can find a number of cases in the WP core files by searching for: . '="'

You'll find lots of usage like this:

$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';

#9 @sc0ttkclark
5 years ago

Should the pattern match be something more like: [^a-zA-Z0-9\-_\[\]]+

This may be more comprehensive to only allow certain character ranges versus just replacing a few characters.

#10 @swissspidy
5 years ago

  • Keywords needs-patch needs-unit-tests added; has-patch removed

Related: wp_kses_one_attr and wp_kses_attr_check also have some logic to sanitize (not escape) attribute names based on the KSES allowlist.

This ticket was mentioned in ​PR #12915 on ​WordPress/wordpress-develop by ​@sainathpoojary.


3 months ago
#11

  • Keywords has-patch added; needs-patch removed

The HTML5 spec allows arbitrarily named attributes (e.g., data-*), but WordPress lacked a dedicated function to escape dynamically generated attribute names. Developers sometimes incorrectly used esc_attr() for this, which only escapes attribute values.

This PR introduces esc_attr_name() in formatting.php to resolve this. It uses a strict allowlist regex ([a-zA-Z0-9_.:\[\]-]+/u) to strip any characters not explicitly permitted in an HTML attribute name per the HTML5 specification (such as spaces, quotes, control characters, <>, =, etc.).

Trac ticket: #43010

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

@arkaprabhachowdhury
3 months ago

WordPress Playground test results for PR #12915 / ticket #43010.

#12 @arkaprabhachowdhury
3 months ago

Test Report

Tested PR #12915 in a fresh WordPress Playground instance for #43010.

Environment:

  • WordPress Playground Core PR build (?core-pr=12915)
  • Chrome

Steps:

  1. Opened the PR build in WordPress Playground.
  2. Ran esc_attr_name() through a Playground runPHP test with valid and invalid attribute names:
    • data-id
    • aria-label
    • data bad
    • data"x
    • data<>x
    • data-[foo]:bar
  3. Compared each returned value with the expected sanitized value.

Expected:
Allowed attribute-name characters should be preserved, while disallowed characters should be removed.

Results:

  • Pass: data-id, aria-label, and data-[foo]:bar were preserved.
  • Pass: spaces, quotes, and angle brackets were removed from invalid names.
  • Pass: all six cases returned the expected output.

Screenshot: attachment:ticket-43010-playground.png​

The proposed esc_attr_name() behavior works as intended in the PR Playground build.

#13 @dmsnell
7 weeks ago

Of note, the HTML API provides set_attribute() and rejects invalid attribute names. There is no way to escape attribute names, and by stripping away bytes from a supplied name, we open up parser-differential style vulnerabilities, where one piece of code things we have two separate attributes but another sees them as duplicates.

It is preferable to outright reject operations given unrepresentable attribute names, and code interacting with such should prefer generating content through the Tag Processor.

<?php
$processor = new WP_HTML_Tag_Processor( '<div>' );
$processor->next_token();

foreach ( $attributes as $name => $value ) {
        $processor->set_attribute( $name, $value );
}

echo $processor->get_updated_html();

#14 @jamesbregenzer
6 weeks ago

Tested PR #12915 at 3a36209b3784a72a0f6bf6dc732a93073d2b020f against 044a0d01175fc9982cf18fb2cf6de692c6e617ac.
This adds a new test finding for the current patch. Required build profile completed.
Relevant checks:

  • Required build profile completed.
  • Focused PHPUnit profile completed.

#15 @jamesbregenzer
6 weeks ago

Tested PR #12915 at 3a36209b3784a72a0f6bf6dc732a93073d2b020f against 044a0d01175fc9982cf18fb2cf6de692c6e617ac.

I ran the ticket-specific automated tests for the Formatting component using the current patch.
The focused PHPUnit run completed 1 test and 1 assertion with no reported failures.

This gives maintainers current evidence that the patch behaves as intended in the Formatting component paths covered by ticket #43010.

Note: See TracTickets for help on using tickets.