-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement #71517: Implement SVG support for getimagesize() and friends #16670
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
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,178 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#ifdef HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include "php.h" | ||
#include "image_svg.h" | ||
#include "php_libxml.h" | ||
|
||
#include "ext/standard/php_image.h" | ||
|
||
#include <libxml/xmlreader.h> | ||
|
||
#ifdef HAVE_LIBXML | ||
|
||
static int svg_image_type_id; | ||
|
||
static int php_libxml_svg_stream_read(void *context, char *buffer, int len) | ||
{ | ||
return php_stream_read(context, buffer, len); | ||
} | ||
|
||
/* Sanity check that the input only contains characters valid for a dimension (numbers with units, e.g. 5cm). | ||
* This also protects the user against injecting XSS. | ||
* Only accept [0-9]+[a-zA-Z]* */ | ||
static bool php_libxml_parse_dimension(const xmlChar *input, const xmlChar **unit_position) | ||
{ | ||
if (!(*input >= '0' && *input <= '9')) { | ||
return false; | ||
} | ||
|
||
input++; | ||
|
||
while (*input) { | ||
if (!(*input >= '0' && *input <= '9')) { | ||
if ((*input >= 'a' && *input <= 'z') || (*input >= 'A' && *input <= 'Z')) { | ||
break; | ||
} | ||
return false; | ||
} | ||
input++; | ||
} | ||
|
||
*unit_position = input; | ||
|
||
while (*input) { | ||
if (!((*input >= 'a' && *input <= 'z') || (*input >= 'A' && *input <= 'Z'))) { | ||
return false; | ||
} | ||
input++; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
zend_result php_libxml_svg_image_handle(php_stream *stream, struct php_gfxinfo **result) | ||
{ | ||
if (php_stream_rewind(stream)) { | ||
return FAILURE; | ||
} | ||
|
||
/* Early check before doing more expensive work */ | ||
if (php_stream_getc(stream) != '<') { | ||
return FAILURE; | ||
} | ||
|
||
if (php_stream_rewind(stream)) { | ||
return FAILURE; | ||
} | ||
|
||
PHP_LIBXML_SANITIZE_GLOBALS(reader_for_stream); | ||
xmlTextReaderPtr reader = xmlReaderForIO( | ||
php_libxml_svg_stream_read, | ||
NULL, | ||
stream, | ||
NULL, | ||
NULL, | ||
XML_PARSE_NOWARNING | XML_PARSE_NOERROR | XML_PARSE_NONET | ||
); | ||
PHP_LIBXML_RESTORE_GLOBALS(reader_for_stream); | ||
|
||
if (!reader) { | ||
return FAILURE; | ||
} | ||
|
||
bool is_svg = false; | ||
while (xmlTextReaderRead(reader) == 1) { | ||
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { | ||
/* Root must be an svg element */ | ||
const xmlChar *name = xmlTextReaderConstLocalName(reader); | ||
if (!name || strcasecmp((const char *) name, "svg") != 0) { | ||
break; | ||
} | ||
|
||
xmlChar *width = xmlTextReaderGetAttribute(reader, BAD_CAST "width"); | ||
xmlChar *height = xmlTextReaderGetAttribute(reader, BAD_CAST "height"); | ||
const xmlChar *width_unit_position, *height_unit_position; | ||
if (!width || !height | ||
|| !php_libxml_parse_dimension(width, &width_unit_position) | ||
|| !php_libxml_parse_dimension(height, &height_unit_position)) { | ||
xmlFree(width); | ||
xmlFree(height); | ||
break; | ||
} | ||
|
||
is_svg = true; | ||
if (result) { | ||
*result = ecalloc(1, sizeof(**result)); | ||
(*result)->width = ZEND_STRTOL((const char *) width, NULL, 10); | ||
(*result)->height = ZEND_STRTOL((const char *) height, NULL, 10); | ||
if (*width_unit_position) { | ||
(*result)->width_unit = zend_string_init((const char*) width_unit_position, | ||
xmlStrlen(width_unit_position), false); | ||
} | ||
if (*height_unit_position) { | ||
(*result)->height_unit = zend_string_init((const char*) height_unit_position, | ||
xmlStrlen(height_unit_position), false); | ||
} | ||
} | ||
|
||
xmlFree(width); | ||
xmlFree(height); | ||
break; | ||
} | ||
} | ||
|
||
xmlFreeTextReader(reader); | ||
|
||
return is_svg ? SUCCESS : FAILURE; | ||
} | ||
|
||
zend_result php_libxml_svg_image_identify(php_stream *stream) | ||
{ | ||
return php_libxml_svg_image_handle(stream, NULL); | ||
} | ||
|
||
struct php_gfxinfo *php_libxml_svg_image_get_info(php_stream *stream) | ||
{ | ||
struct php_gfxinfo *result = NULL; | ||
zend_result status = php_libxml_svg_image_handle(stream, &result); | ||
ZEND_ASSERT((status == SUCCESS) == (result != NULL)); | ||
return result; | ||
} | ||
|
||
static const struct php_image_handler svg_image_handler = { | ||
"image/svg+xml", | ||
".svg", | ||
PHP_IMAGE_CONST_NAME("SVG"), | ||
php_libxml_svg_image_identify, | ||
php_libxml_svg_image_get_info, | ||
}; | ||
|
||
void php_libxml_register_image_svg_handler(void) | ||
{ | ||
svg_image_type_id = php_image_register_handler(&svg_image_handler); | ||
} | ||
|
||
zend_result php_libxml_unregister_image_svg_handler(void) | ||
{ | ||
return php_image_unregister_handler(svg_image_type_id); | ||
} | ||
|
||
#endif |
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,25 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#ifndef LIBXML_IMAGE_SVG | ||
#define LIBXML_IMAGE_SVG | ||
|
||
#include "zend.h" | ||
|
||
void php_libxml_register_image_svg_handler(void); | ||
zend_result php_libxml_unregister_image_svg_handler(void); | ||
|
||
#endif |
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
Oops, something went wrong.
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.
Maybe add a note about the new array element
width_unit
andheight_unit
(for the doc people).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.
Of course