|
| 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 | + | license@php.net so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | Authors: Niels Dossche <nielsdos@php.net> | |
| 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 |
0 commit comments