Skip to content

Commit c972e53

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 3e0d3b3 commit c972e53

File tree

7 files changed

+258
-4
lines changed

7 files changed

+258
-4
lines changed

ext/libxml/config.w32

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

15-
EXTENSION("libxml", "libxml.c mime_sniff.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
15+
EXTENSION("libxml", "libxml.c mime_sniff.c image_svg.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
1616
AC_DEFINE("HAVE_LIBXML", 1, "Define to 1 if the PHP extension 'libxml' is available.");
1717
ADD_FLAG("CFLAGS_LIBXML", "/D LIBXML_STATIC /D LIBXML_STATIC_FOR_DLL /D HAVE_WIN32_THREADS ");
1818
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: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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-9a-zA-Z] */
41+
static bool php_libxml_valid_dimension(const xmlChar *input)
42+
{
43+
while (*input) {
44+
if (!((*input >= '0' && *input <= '9') || (*input >= 'a' && *input <= 'z') || (*input >= 'A' && *input <= 'Z'))) {
45+
return false;
46+
}
47+
input++;
48+
}
49+
return true;
50+
}
51+
52+
zend_result php_libxml_svg_image_handle(php_stream *stream, struct php_gfxinfo **result)
53+
{
54+
if (php_stream_rewind(stream)) {
55+
return FAILURE;
56+
}
57+
58+
/* Early check before doing more expensive work */
59+
if (php_stream_getc(stream) != '<') {
60+
return FAILURE;
61+
}
62+
63+
if (php_stream_rewind(stream)) {
64+
return FAILURE;
65+
}
66+
67+
PHP_LIBXML_SANITIZE_GLOBALS(reader_for_stream);
68+
xmlTextReaderPtr reader = xmlReaderForIO(
69+
php_libxml_svg_stream_read,
70+
NULL,
71+
stream,
72+
NULL,
73+
NULL,
74+
XML_PARSE_NOWARNING | XML_PARSE_NOERROR | XML_PARSE_NONET
75+
);
76+
PHP_LIBXML_RESTORE_GLOBALS(reader_for_stream);
77+
78+
if (!reader) {
79+
return FAILURE;
80+
}
81+
82+
bool is_svg = false;
83+
while (xmlTextReaderRead(reader) == 1) {
84+
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
85+
/* Root must be an svg element */
86+
const xmlChar *name = xmlTextReaderConstLocalName(reader);
87+
if (!name || strcasecmp((const char *) name, "svg") != 0) {
88+
break;
89+
}
90+
91+
xmlChar *width = xmlTextReaderGetAttribute(reader, BAD_CAST "width");
92+
xmlChar *height = xmlTextReaderGetAttribute(reader, BAD_CAST "height");
93+
if (!width || !height || !php_libxml_valid_dimension(width) || !php_libxml_valid_dimension(height)) {
94+
xmlFree(width);
95+
xmlFree(height);
96+
break;
97+
}
98+
99+
is_svg = true;
100+
if (result) {
101+
*result = ecalloc(1, sizeof(**result));
102+
(*result)->width_str = zend_string_init((const char *) width, xmlStrlen(width), false);
103+
(*result)->height_str = zend_string_init((const char *) height, xmlStrlen(height), false);
104+
}
105+
106+
xmlFree(width);
107+
xmlFree(height);
108+
break;
109+
}
110+
}
111+
112+
xmlFreeTextReader(reader);
113+
114+
return is_svg ? SUCCESS : FAILURE;
115+
}
116+
117+
zend_result php_libxml_svg_image_identify(php_stream *stream)
118+
{
119+
return php_libxml_svg_image_handle(stream, NULL);
120+
}
121+
122+
struct php_gfxinfo *php_libxml_svg_image_get_info(php_stream *stream)
123+
{
124+
struct php_gfxinfo *result = NULL;
125+
zend_result status = php_libxml_svg_image_handle(stream, &result);
126+
ZEND_ASSERT((status == SUCCESS) == (result != NULL));
127+
return result;
128+
}
129+
130+
static const struct php_image_handler svg_image_handler = {
131+
"image/svg+xml",
132+
".svg",
133+
PHP_IMAGE_CONST_NAME("SVG"),
134+
php_libxml_svg_image_identify,
135+
php_libxml_svg_image_get_info,
136+
};
137+
138+
void php_libxml_register_image_svg_handler(void)
139+
{
140+
svg_image_type_id = php_image_register_handler(&svg_image_handler);
141+
}
142+
143+
zend_result php_libxml_unregister_image_svg_handler(void)
144+
{
145+
return php_image_unregister_handler(svg_image_type_id);
146+
}
147+
148+
#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

@@ -88,8 +89,14 @@ static zend_long php_libxml_default_dump_doc_to_file(const char *filename, xmlDo
8889

8990
/* }}} */
9091

92+
static const zend_module_dep libxml_deps[] = {
93+
ZEND_MOD_REQUIRED("standard")
94+
ZEND_MOD_END
95+
};
96+
9197
zend_module_entry libxml_module_entry = {
92-
STANDARD_MODULE_HEADER,
98+
STANDARD_MODULE_HEADER_EX, NULL,
99+
libxml_deps,
93100
"libxml", /* extension name */
94101
ext_functions, /* extension function list */
95102
PHP_MINIT(libxml), /* extension-wide startup function */
@@ -967,6 +974,8 @@ static PHP_MINIT_FUNCTION(libxml)
967974
xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
968975
}
969976

977+
php_libxml_register_image_svg_handler();
978+
970979
return SUCCESS;
971980
}
972981

@@ -1008,7 +1017,7 @@ static PHP_MSHUTDOWN_FUNCTION(libxml)
10081017
}
10091018
php_libxml_shutdown();
10101019

1011-
return SUCCESS;
1020+
return php_libxml_unregister_image_svg_handler();
10121021
}
10131022

10141023
static zend_result php_libxml_post_deactivate(void)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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=\"é\" height='1'/>",
18+
"<foo width='1' height=\"1\"/>",
19+
"<foo foo='1' height=\"1\"/>",
20+
];
21+
22+
foreach ($inputs as $input) {
23+
var_dump(getimagesizefromstring($input));
24+
}
25+
26+
?>
27+
--EXPECTF--
28+
bool(false)
29+
bool(false)
30+
array(5) {
31+
[0]=>
32+
string(3) "4cm"
33+
[1]=>
34+
string(3) "8cm"
35+
[2]=>
36+
int(%d)
37+
[3]=>
38+
string(24) "width="4cm" height="8cm""
39+
["mime"]=>
40+
string(13) "image/svg+xml"
41+
}
42+
array(5) {
43+
[0]=>
44+
string(1) "1"
45+
[1]=>
46+
string(1) "1"
47+
[2]=>
48+
int(%d)
49+
[3]=>
50+
string(20) "width="1" height="1""
51+
["mime"]=>
52+
string(13) "image/svg+xml"
53+
}
54+
bool(false)
55+
bool(false)
56+
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)