forked from yllen/pdf
-
Notifications
You must be signed in to change notification settings - Fork 14
fix: prevent PDF export crash on tables with nested quotes in inline styles #86
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
Open
Herafia
wants to merge
5
commits into
main
Choose a base branch
from
fix/tcpdf-nested-quotes-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−17
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| <phpunit bootstrap="tests/bootstrap.php" colors="true" testdox="true"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| <testsuites> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| <testsuite name="Tests"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| <directory>tests</directory> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| </testsuite> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| </testsuites> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| </phpunit> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And add |
||||||||||||||||||||||||||||||||||||||||||||||||||||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * LICENSE | ||
| * | ||
| * This file is part of PDF plugin for GLPI. | ||
| * | ||
| * PDF is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * PDF is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with Reports. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| * @author Nelly Mahu-Lasson, Remi Collet, Teclib | ||
| * @copyright Copyright (c) 2009-2022 PDF plugin team | ||
| * @license AGPL License 3.0 or (at your option) any later version | ||
| * @link https://github.com/pluginsGLPI/pdf/ | ||
| * @link http://www.glpi-project.org/ | ||
| * @package pdf | ||
| * @since 2009 | ||
| * http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| * -------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace GlpiPlugin\Pdf\Tests\Units; | ||
|
|
||
| use Glpi\Tests\GLPITestCase; | ||
| use PluginPdfSimplePDF; | ||
| use ReflectionMethod; | ||
|
|
||
| class SimplePdfTest extends GLPITestCase | ||
| { | ||
| /** | ||
| * Test nested quote url in a style attribute | ||
| * used to break cleanTableHtml regex and crash TCPDF | ||
| */ | ||
| public function testCleanTableHtmlWithNestedQuoteUrlStyle(): void | ||
| { | ||
| $html = <<<HTML | ||
| <table style="width: 614px; border-collapse: collapse;" border="0" width="614"> | ||
| <tbody> | ||
| <tr> | ||
| <td style="width: 51.8pt; height: 15.75pt; background-image: url('https://example.com/pics/a.jpg');">Content</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
| HTML; | ||
|
|
||
| $pdf = new PluginPdfSimplePDF(); | ||
|
|
||
| $method = new ReflectionMethod(PluginPdfSimplePDF::class, 'cleanTableHtml'); | ||
| $cleaned = $method->invoke($pdf, $html); | ||
|
|
||
| // no duplicated attribute on <table> | ||
| $this->assertMatchesRegularExpression('/^<table[^>]*>/', $cleaned); | ||
| preg_match('/^<table([^>]*)>/', $cleaned, $matches); | ||
| $tableAttributes = $matches[1]; | ||
|
|
||
| $this->assertSame(1, substr_count($tableAttributes, 'border='), 'the <table> tag must have a single border attribute'); | ||
| $this->assertSame(1, substr_count($tableAttributes, 'style='), 'the <table> tag must have a single style attribute'); | ||
|
|
||
| // width/height stripped, nested url untouched | ||
| $this->assertStringNotContainsStringIgnoringCase('width: 614px', $cleaned); | ||
| $this->assertStringNotContainsStringIgnoringCase('width: 51.8pt', $cleaned); | ||
| $this->assertStringNotContainsStringIgnoringCase('height: 15.75pt', $cleaned); | ||
| $this->assertStringContainsString("url('https://example.com/pics/a.jpg')", $cleaned); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * ------------------------------------------------------------------------- | ||||||||||
| * LICENSE | ||||||||||
| * | ||||||||||
| * This file is part of PDF plugin for GLPI. | ||||||||||
| * | ||||||||||
| * PDF is free software: you can redistribute it and/or modify | ||||||||||
| * it under the terms of the GNU Affero General Public License as published by | ||||||||||
| * the Free Software Foundation, either version 3 of the License, or | ||||||||||
| * (at your option) any later version. | ||||||||||
| * | ||||||||||
| * PDF is distributed in the hope that it will be useful, | ||||||||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
| * GNU Affero General Public License for more details. | ||||||||||
| * | ||||||||||
| * You should have received a copy of the GNU Affero General Public License | ||||||||||
| * along with Reports. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| * | ||||||||||
| * @author Nelly Mahu-Lasson, Remi Collet, Teclib | ||||||||||
| * @copyright Copyright (c) 2009-2022 PDF plugin team | ||||||||||
| * @license AGPL License 3.0 or (at your option) any later version | ||||||||||
| * @link https://github.com/pluginsGLPI/pdf/ | ||||||||||
| * @link http://www.glpi-project.org/ | ||||||||||
| * @package pdf | ||||||||||
| * @since 2009 | ||||||||||
| * http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||||||||||
| * -------------------------------------------------------------------------- | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| use function Safe\realpath; | ||||||||||
|
|
||||||||||
| $current_plugin_folder = basename(realpath(__DIR__ . '/../')); | ||||||||||
|
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use pluginsGLPI/empty for example
Suggested change
|
||||||||||
|
|
||||||||||
| require __DIR__ . '/../../../tests/bootstrap.php'; | ||||||||||
| require dirname(__DIR__) . '/vendor/autoload.php'; | ||||||||||
|
|
||||||||||
| if (!Plugin::isPluginActive($current_plugin_folder)) { | ||||||||||
| throw new RuntimeException(sprintf('Plugin %s is not active in the test database', $current_plugin_folder)); | ||||||||||
| } | ||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a unit test for
cleanTableHtml()with a nested-quoteurl('...')style value (case from !45825)? Verified locally that the old regex code produced a<table>with duplicateborder/styleattributes on this input, the actual crash mechanism, and that the new code doesn't; a test would pin the regression.