Skip to content

Commit 0962c32

Browse files
committed
Implement SVG image handler
This implements an SVG handler using the libxml reader API. This does not parse the entire document but instead uses a pull parser to locate the root element, check whether it's an svg root, do some extra sanity checks on the attribute, and fill in the php_gfxinfo structure.
1 parent 8db883c commit 0962c32

27 files changed

+565
-65
lines changed

ext/libxml/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (PHP_LIBXML == "yes") {
1111
if (GREP_HEADER("libxml/xmlversion.h", "#define\\s+LIBXML_VERSION\\s+(\\d+)", PHP_PHP_BUILD + "\\include\\libxml2") &&
1212
+RegExp.$1 >= 20904) {
1313

14-
EXTENSION("libxml", "libxml.c mime_sniff.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
14+
EXTENSION("libxml", "libxml.c mime_sniff.c image_svg.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
1515
AC_DEFINE("HAVE_LIBXML", 1, "Define to 1 if the PHP extension 'libxml' is available.");
1616
ADD_FLAG("CFLAGS_LIBXML", "/D LIBXML_STATIC /D LIBXML_STATIC_FOR_DLL /D HAVE_WIN32_THREADS ");
1717
if (!PHP_LIBXML_SHARED) {

ext/libxml/config0.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if test "$PHP_LIBXML" != "no"; then
1212
AC_DEFINE([HAVE_LIBXML], [1],
1313
[Define to 1 if the PHP extension 'libxml' is available.])
1414
PHP_NEW_EXTENSION([libxml],
15-
[libxml.c mime_sniff.c],
15+
[libxml.c mime_sniff.c image_svg.c],
1616
[$ext_shared],,
1717
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
1818
PHP_INSTALL_HEADERS([ext/libxml], [php_libxml.h])

ext/libxml/image_svg.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Niels Dossche <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifdef HAVE_CONFIG_H
18+
#include "config.h"
19+
#endif
20+
21+
#include "php.h"
22+
#include "image_svg.h"
23+
#include "php_libxml.h"
24+
25+
#include "ext/standard/php_image.h"
26+
27+
#include <libxml/xmlreader.h>
28+
29+
#ifdef HAVE_LIBXML
30+
31+
static int svg_image_type_id;
32+
33+
static int php_libxml_svg_stream_read(void *context, char *buffer, int len)
34+
{
35+
return php_stream_read(context, buffer, len);
36+
}
37+
38+
/* Sanity check that the input only contains characters valid for a dimension (numbers with units, e.g. 5cm).
39+
* This also protects the user against injecting XSS.
40+
* Only accept [0-9]+[a-zA-Z]* */
41+
static bool php_libxml_parse_dimension(const xmlChar *input, const xmlChar **unit_position)
42+
{
43+
if (!(*input >= '0' && *input <= '9')) {
44+
return false;
45+
}
46+
47+
input++;
48+
49+
while (*input) {
50+
if (!(*input >= '0' && *input <= '9')) {
51+
if ((*input >= 'a' && *input <= 'z') || (*input >= 'A' && *input <= 'Z')) {
52+
break;
53+
}
54+
return false;
55+
}
56+
input++;
57+
}
58+
59+
*unit_position = input;
60+
61+
while (*input) {
62+
if (!((*input >= 'a' && *input <= 'z') || (*input >= 'A' && *input <= 'Z'))) {
63+
return false;
64+
}
65+
input++;
66+
}
67+
68+
return true;
69+
}
70+
71+
zend_result php_libxml_svg_image_handle(php_stream *stream, struct php_gfxinfo **result)
72+
{
73+
if (php_stream_rewind(stream)) {
74+
return FAILURE;
75+
}
76+
77+
/* Early check before doing more expensive work */
78+
if (php_stream_getc(stream) != '<') {
79+
return FAILURE;
80+
}
81+
82+
if (php_stream_rewind(stream)) {
83+
return FAILURE;
84+
}
85+
86+
PHP_LIBXML_SANITIZE_GLOBALS(reader_for_stream);
87+
xmlTextReaderPtr reader = xmlReaderForIO(
88+
php_libxml_svg_stream_read,
89+
NULL,
90+
stream,
91+
NULL,
92+
NULL,
93+
XML_PARSE_NOWARNING | XML_PARSE_NOERROR | XML_PARSE_NONET
94+
);
95+
PHP_LIBXML_RESTORE_GLOBALS(reader_for_stream);
96+
97+
if (!reader) {
98+
return FAILURE;
99+
}
100+
101+
bool is_svg = false;
102+
while (xmlTextReaderRead(reader) == 1) {
103+
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
104+
/* Root must be an svg element */
105+
const xmlChar *name = xmlTextReaderConstLocalName(reader);
106+
if (!name || strcasecmp((const char *) name, "svg") != 0) {
107+
break;
108+
}
109+
110+
xmlChar *width = xmlTextReaderGetAttribute(reader, BAD_CAST "width");
111+
xmlChar *height = xmlTextReaderGetAttribute(reader, BAD_CAST "height");
112+
const xmlChar *width_unit_position, *height_unit_position;
113+
if (!width || !height
114+
|| !php_libxml_parse_dimension(width, &width_unit_position)
115+
|| !php_libxml_parse_dimension(height, &height_unit_position)) {
116+
xmlFree(width);
117+
xmlFree(height);
118+
break;
119+
}
120+
121+
is_svg = true;
122+
if (result) {
123+
*result = ecalloc(1, sizeof(**result));
124+
(*result)->width = ZEND_STRTOL((const char *) width, NULL, 10);
125+
(*result)->height = ZEND_STRTOL((const char *) height, NULL, 10);
126+
if (*width_unit_position) {
127+
(*result)->width_unit = zend_string_init((const char*) width_unit_position,
128+
xmlStrlen(width_unit_position), false);
129+
}
130+
if (*height_unit_position) {
131+
(*result)->height_unit = zend_string_init((const char*) height_unit_position,
132+
xmlStrlen(height_unit_position), false);
133+
}
134+
}
135+
136+
xmlFree(width);
137+
xmlFree(height);
138+
break;
139+
}
140+
}
141+
142+
xmlFreeTextReader(reader);
143+
144+
return is_svg ? SUCCESS : FAILURE;
145+
}
146+
147+
zend_result php_libxml_svg_image_identify(php_stream *stream)
148+
{
149+
return php_libxml_svg_image_handle(stream, NULL);
150+
}
151+
152+
struct php_gfxinfo *php_libxml_svg_image_get_info(php_stream *stream)
153+
{
154+
struct php_gfxinfo *result = NULL;
155+
zend_result status = php_libxml_svg_image_handle(stream, &result);
156+
ZEND_ASSERT((status == SUCCESS) == (result != NULL));
157+
return result;
158+
}
159+
160+
static const struct php_image_handler svg_image_handler = {
161+
"image/svg+xml",
162+
".svg",
163+
PHP_IMAGE_CONST_NAME("SVG"),
164+
php_libxml_svg_image_identify,
165+
php_libxml_svg_image_get_info,
166+
};
167+
168+
void php_libxml_register_image_svg_handler(void)
169+
{
170+
svg_image_type_id = php_image_register_handler(&svg_image_handler);
171+
}
172+
173+
zend_result php_libxml_unregister_image_svg_handler(void)
174+
{
175+
return php_image_unregister_handler(svg_image_type_id);
176+
}
177+
178+
#endif

ext/libxml/image_svg.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Niels Dossche <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef LIBXML_IMAGE_SVG
18+
#define LIBXML_IMAGE_SVG
19+
20+
#include "zend.h"
21+
22+
void php_libxml_register_image_svg_handler(void);
23+
zend_result php_libxml_unregister_image_svg_handler(void);
24+
25+
#endif

ext/libxml/libxml.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#endif
4444

4545
#include "php_libxml.h"
46+
#include "image_svg.h"
4647

4748
#define PHP_LIBXML_LOADED_VERSION ((char *)xmlParserVersion)
4849

@@ -84,8 +85,14 @@ static zend_long php_libxml_default_dump_doc_to_file(const char *filename, xmlDo
8485

8586
/* }}} */
8687

88+
static const zend_module_dep libxml_deps[] = {
89+
ZEND_MOD_REQUIRED("standard")
90+
ZEND_MOD_END
91+
};
92+
8793
zend_module_entry libxml_module_entry = {
88-
STANDARD_MODULE_HEADER,
94+
STANDARD_MODULE_HEADER_EX, NULL,
95+
libxml_deps,
8996
"libxml", /* extension name */
9097
ext_functions, /* extension function list */
9198
PHP_MINIT(libxml), /* extension-wide startup function */
@@ -969,6 +976,8 @@ static PHP_MINIT_FUNCTION(libxml)
969976
xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
970977
}
971978

979+
php_libxml_register_image_svg_handler();
980+
972981
return SUCCESS;
973982
}
974983

@@ -1010,7 +1019,7 @@ static PHP_MSHUTDOWN_FUNCTION(libxml)
10101019
}
10111020
php_libxml_shutdown();
10121021

1013-
return SUCCESS;
1022+
return php_libxml_unregister_image_svg_handler();
10141023
}
10151024

10161025
static zend_result php_libxml_post_deactivate(void)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
--TEST--
2+
getimagesize() with svg input
3+
--EXTENSIONS--
4+
libxml
5+
--FILE--
6+
<?php
7+
8+
$inputs = [
9+
"<?xml version=\"1.0\"?>",
10+
"svg width=\"1\" height=\"1\"",
11+
<<<XML
12+
<?xml version="1.0" standalone="yes"?>
13+
<!-- foo <svg width="1" height="1"/> -->
14+
<x:svg width='4cm' height="8cm" xmlns:x="http://www.w3.org/2000/svg">
15+
XML,
16+
"<SVG width='1' height=\"1\"/>",
17+
"<SVG width='1px' height=\"1\"/>",
18+
"<SVG width='1' height=\"1mm\"/>",
19+
"<SVG width='1' height=\"cm\"/>",
20+
"<SVG width='' height=\"\"/>",
21+
"<SVG width=\"é\" height='1'/>",
22+
"<foo width='1' height=\"1\"/>",
23+
"<foo foo='1' height=\"1\"/>",
24+
];
25+
26+
foreach ($inputs as $input) {
27+
var_dump(getimagesizefromstring($input));
28+
}
29+
30+
?>
31+
--EXPECTF--
32+
bool(false)
33+
bool(false)
34+
array(6) {
35+
[0]=>
36+
int(4)
37+
[1]=>
38+
int(8)
39+
[2]=>
40+
int(%d)
41+
["mime"]=>
42+
string(13) "image/svg+xml"
43+
["width_unit"]=>
44+
string(2) "cm"
45+
["height_unit"]=>
46+
string(2) "cm"
47+
}
48+
array(7) {
49+
[0]=>
50+
int(1)
51+
[1]=>
52+
int(1)
53+
[2]=>
54+
int(%d)
55+
[3]=>
56+
string(20) "width="1" height="1""
57+
["mime"]=>
58+
string(13) "image/svg+xml"
59+
["width_unit"]=>
60+
string(2) "px"
61+
["height_unit"]=>
62+
string(2) "px"
63+
}
64+
array(7) {
65+
[0]=>
66+
int(1)
67+
[1]=>
68+
int(1)
69+
[2]=>
70+
int(%d)
71+
[3]=>
72+
string(20) "width="1" height="1""
73+
["mime"]=>
74+
string(13) "image/svg+xml"
75+
["width_unit"]=>
76+
string(2) "px"
77+
["height_unit"]=>
78+
string(2) "px"
79+
}
80+
array(6) {
81+
[0]=>
82+
int(1)
83+
[1]=>
84+
int(1)
85+
[2]=>
86+
int(%d)
87+
["mime"]=>
88+
string(13) "image/svg+xml"
89+
["width_unit"]=>
90+
string(2) "px"
91+
["height_unit"]=>
92+
string(2) "mm"
93+
}
94+
bool(false)
95+
bool(false)
96+
bool(false)
97+
bool(false)
98+
bool(false)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
imagetype API with svg extension
3+
--EXTENSIONS--
4+
libxml
5+
--FILE--
6+
<?php
7+
8+
var_dump(IMAGETYPE_COUNT > IMAGETYPE_SVG);
9+
var_dump(image_type_to_extension(IMAGETYPE_SVG));
10+
var_dump(image_type_to_mime_type(IMAGETYPE_SVG));
11+
12+
?>
13+
--EXPECT--
14+
bool(true)
15+
string(4) ".svg"
16+
string(13) "image/svg+xml"

0 commit comments

Comments
 (0)