Skip to content
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

Fix: Sanitize Donation Form CSS #7378

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/DonationForms/Properties/FormSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class FormSettings implements Arrayable, Jsonable
* @since 3.7.0 Added formExcerpt

/**
* @unreleased Sanitize customCSS property
* @since 3.2.0 Added registrationNotification
* @since 3.0.0
*/
Expand Down Expand Up @@ -273,7 +274,7 @@ public static function fromArray(array $array): self
$self->secondaryColor = $array['secondaryColor'] ?? '#f49420';
$self->goalAmount = $array['goalAmount'] ?? 0;
$self->registrationNotification = $array['registrationNotification'] ?? false;
$self->customCss = $array['customCss'] ?? '';
$self->customCss = wp_strip_all_tags($array['customCss'] ?? '');
$self->pageSlug = $array['pageSlug'] ?? '';
$self->goalAchievedMessage = $array['goalAchievedMessage'] ?? __(
'Thank you to all our donors, we have met our fundraising goal.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function formExports(): array
}

/**
* @unreleased Sanitize customCSS property
* @since 3.0.0
*/
public function render(): string
Expand Down Expand Up @@ -111,7 +112,7 @@ public function render(): string

<?php
if ($customCss): ?>
<style><?= $customCss ?></style>
<style><?php echo wp_strip_all_tags($customCss); ?></style>
<?php
endif; ?>

Expand Down
3 changes: 2 additions & 1 deletion src/DonationForms/ViewModels/DonationFormViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public function exports(): array
* 5. Finally, call the specific WP function wp_print_footer_scripts()
* - This will only print the footer scripts that are enqueued within our route.
*
* @unreleased Sanitize customCSS property
* @since 3.0.0
*/
public function render(): string
Expand All @@ -266,7 +267,7 @@ public function render(): string
<?php
if ($this->previewMode || $this->formSettings->customCss): ?>
<style id="root-givewp-donation-form-style"><?php
echo $this->formSettings->customCss; ?></style>
echo wp_strip_all_tags($this->formSettings->customCss); ?></style>
<?php
endif; ?>

Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/DonationForms/Properties/FormSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Unit\DonationForms\Properties;

use Give\DonationForms\Properties\FormSettings;
use Give\Tests\TestCase;

/**
* @unreleased
*/
class FormSettingsTest extends TestCase
{
public function testSanitizationRemovesHtmlTagsFromCustomCss()
{
$formSettings = FormSettings::fromArray([
'customCss' => '<script>alert("hi!")</script>',
]);

$this->assertEmpty($formSettings->customCss);
}

public function testSanitizationPreservesCssWhileRemovingHtmlTags()
{
$formSettings = FormSettings::fromArray([
'customCss' => '.test { color: green; }</style><script>alert("hi!")</script><style>',
]);

$this->assertSame('.test { color: green; }', $formSettings->customCss);
}
}
Loading