-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Implement #71517: Implement SVG support for getimagesize() and friends #16670
Open
nielsdos
wants to merge
6
commits into
php:master
Choose a base branch
from
nielsdos:req-71517
base: master
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.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
04f1a90
Expose gfxinfo as php_gfxinfo
nielsdos 2441dab
Add API to register custom image handlers
nielsdos ec8a095
Implement SVG image handler
nielsdos de86b70
UPGRADING and UPGRADING.INTERNALS for SVG and extensible image API
nielsdos 779f6f7
Address review comments
nielsdos 2c6b540
[ci skip] UPGRADING
nielsdos 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 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 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 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 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 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 | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <nielsdos@php.net> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#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 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 | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <nielsdos@php.net> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#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 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 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,98 @@ | ||
--TEST-- | ||
getimagesize() with svg input | ||
--EXTENSIONS-- | ||
libxml | ||
--FILE-- | ||
<?php | ||
|
||
$inputs = [ | ||
"<?xml version=\"1.0\"?>", | ||
"svg width=\"1\" height=\"1\"", | ||
<<<XML | ||
<?xml version="1.0" standalone="yes"?> | ||
<!-- foo <svg width="1" height="1"/> --> | ||
<x:svg width='4cm' height="8cm" xmlns:x="http://www.w3.org/2000/svg"> | ||
XML, | ||
"<SVG width='1' height=\"1\"/>", | ||
"<SVG width='1px' height=\"1\"/>", | ||
"<SVG width='1' height=\"1mm\"/>", | ||
"<SVG width='1' height=\"cm\"/>", | ||
"<SVG width='' height=\"\"/>", | ||
"<SVG width=\"é\" height='1'/>", | ||
"<foo width='1' height=\"1\"/>", | ||
"<foo foo='1' height=\"1\"/>", | ||
]; | ||
|
||
foreach ($inputs as $input) { | ||
var_dump(getimagesizefromstring($input)); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
bool(false) | ||
bool(false) | ||
array(6) { | ||
[0]=> | ||
int(4) | ||
[1]=> | ||
int(8) | ||
[2]=> | ||
int(%d) | ||
["mime"]=> | ||
string(13) "image/svg+xml" | ||
["width_unit"]=> | ||
string(2) "cm" | ||
["height_unit"]=> | ||
string(2) "cm" | ||
} | ||
array(7) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(1) | ||
[2]=> | ||
int(%d) | ||
[3]=> | ||
string(20) "width="1" height="1"" | ||
["mime"]=> | ||
string(13) "image/svg+xml" | ||
["width_unit"]=> | ||
string(2) "px" | ||
["height_unit"]=> | ||
string(2) "px" | ||
} | ||
array(7) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(1) | ||
[2]=> | ||
int(20) | ||
[3]=> | ||
string(20) "width="1" height="1"" | ||
["mime"]=> | ||
string(13) "image/svg+xml" | ||
["width_unit"]=> | ||
string(2) "px" | ||
["height_unit"]=> | ||
string(2) "px" | ||
} | ||
array(6) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(1) | ||
[2]=> | ||
int(20) | ||
["mime"]=> | ||
string(13) "image/svg+xml" | ||
["width_unit"]=> | ||
string(2) "px" | ||
["height_unit"]=> | ||
string(2) "mm" | ||
} | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) |
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