Skip to content

Enhanced RGBA color validation - #4718

Open
girishpanchal30 wants to merge 3 commits into
developmentfrom
bugfix/rgba-function-color-structure
Open

girishpanchal30 wants to merge 3 commits into
developmentfrom
bugfix/rgba-function-color-structure

Conversation

@girishpanchal30

Copy link
Copy Markdown
Contributor

Summary

Improved CSS function color validation and sanitization by refining the detection regex to handle a broader range of valid and invalid color values. Expanded test coverage to cover additional edge cases and ensure more reliable color handling.

Check before Pull Request is ready:

Closes #4706 (comment)

@pirate-bot

pirate-bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Plugin build for a62cc7b is ready 🛎️!

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The stricter color check works, but the ability routes still store malformed colors without it.

  • inc/abilities/site_set_theme_mod.php:175 — correctness The ability routes store color fields without the color check this PR tightens. site_set_theme_mod saves set_theme_mod( $key, $this->sanitize_value( $input['value'] ) ) at line 175, and sanitize_value() only runs text sanitization (lines 191-200). The header and footer row-style abilities do the same for text_color, background.colorValue and background.overlayColorValue (inc/abilities/builder_configure.php:188-194). So rgb(1.2.3,4,5), which neve_sanitize_colors() now rejects, is stored and printed as a CSS variable (inc/core/styles/css_prop.php:227-242). Run each color-bearing field of these abilities through neve_sanitize_colors() before storage, and reject the call when it returns ''. Add ability tests that send rgb(1.2.3,4,5) to each route.
Validation details
  • Files reviewed: 2/2 changed files.
  • The sanitization class passes at HEAD in a WordPress sandbox with PHP 8.3: cd /var/www/html/wp-content/themes/neve && set -o pipefail; timeout 900 phpunit --do-not-cache-result --filter 'TestSani… printed OK (12 tests, 145 assertions) (run R9)
  • Both changed tests fail with the pr-base sanitizer, so they detect the change: set -o pipefail; T=/var/www/html/wp-content/themes/neve; cd $T && timeout 900 phpunit --do-not-cache-result --filter 'Te… printed Failures: 2 (run R12)
  • An isolated run compared the old and new patterns on 28 inputs. Only invalid CSS values changed to rejected, and long inputs showed no backtracking: timeout 30 php /workspace/checks/C3/check.php | tee /workspace/checks/C3/output.txt (run R7, exit 0)
Untested areas
  • Malformed colors inside var() fallbacks and gradients still pass neve_sanitize_colors(). No color picker was found that emits them, so this stays unconfirmed.
  • Palette colors saved under the old pattern were not traced to a real producer.
  • WordPress versions between 5.5 and 7.1 were not read.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • assets/apps/components/src/Common/ColorPickerFix.js:53-66 — unverified: deciding whether malformed user input survives requires the version-matched WordPress ColorPicker.commitValues/GradientPicker contract. Preview outputs themselves interpolate value…
    • assets/apps/components/src/Controls/ColorControl.js:46-52,175-181 — unverified: deciding whether malformed user input survives requires the version-matched WordPress ColorPicker.commitValues/GradientPicker contract. Preview outputs themselves interpolate value…
    • class-wp-customize-setting.php:650-660,657-667 — unverified:
    • /deps/wordpress/5.5/wp-includes/class-wp-customize-setting.php:650-660 — unverified: persistence routing is established at 5.5 and 7.1: saving obtains post_value() after configured sanitization (:518-521 and :525-528; manager guards below). Corresponding source for…
    • /deps/wordpress/7.1/wp-includes/class-wp-customize-setting.php:657-667 — unverified: persistence routing is established at 5.5 and 7.1: saving obtains post_value() after configured sanitization (:518-521 and :525-528; manager guards below). Corresponding source for…
    • not reached: complete server-rendering path for header-footer-grid/Core/Components/Utility/SearchIconButton.php:239-267

🤖 Automated review · run code-review-agent_6b0564ed20f950.57009720.


🤖 Review agent — review posted ✅ on bebe609c · changes requested · 1 finding · 32 min

Run code-review-agent_6b0564ed20f950.57009720 · trail

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The new checks fix most ability color routes. The menu icon button appearance colors still skip the color check.

Questions for the author

  • Issue #4708 reports that the Customizer row Text Color is cleaned only with wp_filter_nohtml_kses. Is that setting checked as a color anywhere before it reaches the CSS?
Validation details
  • Files reviewed: 8/8 changed files. One question was not settled: "Failure direction: Too strict".
  • The new ability tests pass at HEAD: set -o pipefail; cd /repo && git status --short | head -3; rm -rf /workspace/ab && mkdir -p /workspace/ab/head/themes /… printed OK (6 tests, 22 assertions) (run R5)
  • Four of them fail with the pr-base code, so they detect the change: set -o pipefail; cd /workspace/ab/head/themes/neve && timeout 280 phpunit --do-not-cache-result --testdox --filter 'Test… printed Tests: 6, Assertions: 14, Failures: 4. (run R6)
  • The earlier finding is fixed for builder rows and for _color and _background keys.
Untested areas
  • "Failure direction: Too strict" was not settled for the background check.
  • The color pickers in ColorPickerFix.js and ColorControl.js were not traced to the values they emit.
  • WordPress versions between 5.5 and 7.1 were not read.
  • Import and migration color copies were not traced.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • class-wp-customize-setting.php:650-660,657-667 — unverified: The unversioned place resolves to the two endpoint versions ruled above; behavior at intermediate supported WordPress versions was not read.
    • not reached: migration/import writes in inc/core/admin.php:1097 and header-footer-grid/functions-migration.php:84
    • assets/apps/components/src/Common/ColorPickerFix.js:53-66 — unverified: Presence-only checks reach inherited commitValues(colors). Whether malformed numeric components survive that implementation remains unknown; version-matched readable @wordpress/com…
    • assets/apps/components/src/Controls/ColorControl.js:46-52,175-181 — unverified: RGB interpolation and gradient forwarding lack local structural validation, but whether the upstream pickers emit malformed components remains unresolved. The dependency range in p…

🤖 Automated review · run code-review-agent_6b056e5b78b701.95767916.


🤖 Review agent — review posted ✅ on a4f93901 · changes requested · 2 findings · 21 min

Run code-review-agent_6b056e5b78b701.95767916 · trail

Comment thread inc/abilities/site_set_theme_mod.php Resolved

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The earlier findings are fixed. Four color paths outside the diff still store malformed colors and need the same check.

  • inc/abilities/appearance_update.php:585 — correctness The appearance ability still stores malformed hex button colors. Its check accepts any hex of 3 to 8 digits, so buttons.primary.background: "#12345" or a 7-digit value is saved to neve_button_appearance (lines 535-559). That value reaches --primarybtnbg unchanged and the browser drops it. Validate every button field (background, text and their hover values, primary and secondary) with neve_sanitize_colors(). Reject a non-empty value it returns as ''. Add a case in tests/test-neve-ability-color-inputs.php that sends #12345 for each field and expects the request to fail.
  • header-footer-grid/Core/Builder/Abstract_Builder.php:531 — correctness The header and footer row Text Color is still not checked as a color. Its setting uses 'sanitize_callback' => 'wp_filter_nohtml_kses', which only strips HTML. The saved value is printed as --color without escaping (Abstract_Builder.php:1040-1043, css_prop.php:227-242). So rgb(1.2.3,4,5) is stored, and #222;}body{background:lime}/* can close the rule and add CSS (issue #4708). Use neve_sanitize_colors as the callback for every row Text Color setting. Add tests in tests/test-neve-sanitization.php that sanitize a header row and a footer row Text Color with both values and expect ''.
  • inc/core/admin.php:1097 — correctness Switching from Neve to a child theme copies every theme mod without the new color checks. migrate_options() runs on after_switch_theme and copies stored values as they are. A color saved before this fix, such as rgb(1.2.3,4,5) in neve_scroll_to_top_icon_color, is copied and printed as --color in the child theme. Run each copied color, background and button appearance mod through its Customizer sanitizer before the copy, and drop a value it blanks. Add a test that stores a malformed color, runs migrate_options() for a child theme, and expects the copied mod to be empty.
  • globals/sanitize-functions.php:36 — correctness Malformed function colors still pass when they sit inside var() or a gradient. The var() fallback uses the loose $color_fn pattern [0-9a-z.,%\/\s-]+, and the gradient pattern accepts any such call. So var(--missing,rgb(1.2.3,4,5)) and linear-gradient(rgb(1.2.3,4,5),#fff) are saved unchanged and printed into CSS. Extend the component grammar of neve_is_css_function_color() to accept the valid slash alpha form, such as rgb(1 2 3 / 50%). Then check every rgb(), rgba(), hsl() and hsla() call inside a var() fallback or a gradient with that grammar. Add both malformed values to tests/test-neve-sanitization.php and expect ''.
Validation details
  • Files reviewed: 4/4 files changed since the prior review. Run code-review-agent_6b056e5b78b701.95767916 reviewed the other 4 files, not rerun.
  • The ability color tests pass at HEAD: du -sh /repo 2>/dev/null | tail -1; rm -rf /tmp/c3copy; git -C /repo worktree list; cp -a /repo /tmp/c3copy && cd /tmp/c… printed OK (10 tests, 39 assertions) (run R3)
  • The color sanitizer keeps nested malformed colors unchanged at HEAD and pr-base: timeout 30 php /workspace/checks/C8/run.php (run R9, exit 0)
  • Question for the author: CONTRIBUTING.md puts new unit tests under tests/php/unit. Is that rule outdated?
Untested areas
  • The valid button appearance test passed with the pr-base code. That run has no record.
  • The suite fails to load an unchanged menu icon test in the checkout. A clean copy passes.
  • The color picker scripts were not traced to the values they emit.
  • Only WordPress 5.5 and 7.1 were read. The child theme copy was checked on 7.1 only.
  • The variant pass did not settle these places, where the fixed cause may still reach:
    • not reached: previously unverified ColorPickerFix.js:53-66, ColorControl.js:46-52,175-181 and migration writes
    • assets/apps/components/src/Common/ColorPickerFix.js:53-66 — unverified: Presence checks forward components to inherited commitValues; the resolved upstream implementation and its numeric normalization remain unread.
    • assets/apps/components/src/Controls/ColorControl.js:46-52,175-181 — unverified: RGB interpolation and gradient forwarding depend on upstream picker output validation. JavaScript dependencies are absent (/workspace/review/context.md:79); the runtime component i…
    • class-wp-customize-setting.php:650-660,657-667 — unverified: WordPress 5.5 and 7.1 retain their supplied per-route rulings. Intermediate supported versions’ sanitize-before-write contracts remain unread; endpoint evidence does not establish …

🤖 Automated review · run code-review-agent_6b0575fabc1e97.12013384.


🤖 Review agent — review posted ✅ on a62cc7ba · changes requested · 6 findings · 45 min

Run code-review-agent_6b0575fabc1e97.12013384 · trail

Comment on lines +196 to +200
* Sanitize a value for its setting: colors and backgrounds as the Customizer does, the rest as text.
*
* @param string $key The allowlisted theme_mod key.
* @param mixed $value The incoming value.
* @return mixed Sanitized value, or null when a color or background is invalid.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments The sanitize_setting() docblock is now stale. It says "colors and backgrounds as the Customizer does, the rest as text" and "null when a color or background is invalid". The new _button_appearance branch at line 211 follows neither sentence. Write "colors, backgrounds and button appearances as the Customizer does, the rest as text". Change the @return text to "null when a color, background or button appearance is invalid".

Comment on lines +94 to +109
public function test_set_theme_mod_rejects_button_appearance_with_malformed_color(): void {
$result = ( new Site_Set_Theme_Mod() )->execute(
array(
'action' => 'set',
'key' => 'nav-icon_button_appearance',
'value' => array(
'type' => 'fill',
'background' => self::MALFORMED_COLOR,
),
)
);

$this->assertFalse( $result['success'] );
$this->assertSame( 'invalid_value', $result['error']['code'] );
$this->assertFalse( get_theme_mod( 'nav-icon_button_appearance' ) );
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests Add cases for the two new rejection paths of sanitize_button_appearance_input(). A non-array value returns null (inc/abilities/abstract_ability.php:279-281). A color field sent as an array is blanked by neve_sanitize_colors(), and blanks_sent_field() rejects it (abstract_ability.php:296-303). No test sends either. Add cases that set nav-icon_button_appearance to 'fill' and to array( 'type' => 'fill', 'background' => array( '#fff' ) ). Assert the invalid_value code and that get_theme_mod() returns false.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants