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