You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
10KB

  1. <?php
  2. /**
  3. * HTML Purifier's internal representation of a URI.
  4. * @note
  5. * Internal data-structures are completely escaped. If the data needs
  6. * to be used in a non-URI context (which is very unlikely), be sure
  7. * to decode it first. The URI may not necessarily be well-formed until
  8. * validate() is called.
  9. */
  10. class HTMLPurifier_URI
  11. {
  12. /**
  13. * @type string
  14. */
  15. public $scheme;
  16. /**
  17. * @type string
  18. */
  19. public $userinfo;
  20. /**
  21. * @type string
  22. */
  23. public $host;
  24. /**
  25. * @type int
  26. */
  27. public $port;
  28. /**
  29. * @type string
  30. */
  31. public $path;
  32. /**
  33. * @type string
  34. */
  35. public $query;
  36. /**
  37. * @type string
  38. */
  39. public $fragment;
  40. /**
  41. * @param string $scheme
  42. * @param string $userinfo
  43. * @param string $host
  44. * @param int $port
  45. * @param string $path
  46. * @param string $query
  47. * @param string $fragment
  48. * @note Automatically normalizes scheme and port
  49. */
  50. public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment)
  51. {
  52. $this->scheme = is_null($scheme) || ctype_lower($scheme) ? $scheme : strtolower($scheme);
  53. $this->userinfo = $userinfo;
  54. $this->host = $host;
  55. $this->port = is_null($port) ? $port : (int)$port;
  56. $this->path = $path;
  57. $this->query = $query;
  58. $this->fragment = $fragment;
  59. }
  60. /**
  61. * Retrieves a scheme object corresponding to the URI's scheme/default
  62. * @param HTMLPurifier_Config $config
  63. * @param HTMLPurifier_Context $context
  64. * @return HTMLPurifier_URIScheme Scheme object appropriate for validating this URI
  65. */
  66. public function getSchemeObj($config, $context)
  67. {
  68. $registry = HTMLPurifier_URISchemeRegistry::instance();
  69. if ($this->scheme !== null) {
  70. $scheme_obj = $registry->getScheme($this->scheme, $config, $context);
  71. if (!$scheme_obj) {
  72. return false;
  73. } // invalid scheme, clean it out
  74. } else {
  75. // no scheme: retrieve the default one
  76. $def = $config->getDefinition('URI');
  77. $scheme_obj = $def->getDefaultScheme($config, $context);
  78. if (!$scheme_obj) {
  79. // something funky happened to the default scheme object
  80. trigger_error(
  81. 'Default scheme object "' . $def->defaultScheme . '" was not readable',
  82. E_USER_WARNING
  83. );
  84. return false;
  85. }
  86. }
  87. return $scheme_obj;
  88. }
  89. /**
  90. * Generic validation method applicable for all schemes. May modify
  91. * this URI in order to get it into a compliant form.
  92. * @param HTMLPurifier_Config $config
  93. * @param HTMLPurifier_Context $context
  94. * @return bool True if validation/filtering succeeds, false if failure
  95. */
  96. public function validate($config, $context)
  97. {
  98. // ABNF definitions from RFC 3986
  99. $chars_sub_delims = '!$&\'()*+,;=';
  100. $chars_gen_delims = ':/?#[]@';
  101. $chars_pchar = $chars_sub_delims . ':@';
  102. // validate host
  103. if (!is_null($this->host)) {
  104. $host_def = new HTMLPurifier_AttrDef_URI_Host();
  105. $this->host = $host_def->validate($this->host, $config, $context);
  106. if ($this->host === false) {
  107. $this->host = null;
  108. }
  109. }
  110. // validate scheme
  111. // NOTE: It's not appropriate to check whether or not this
  112. // scheme is in our registry, since a URIFilter may convert a
  113. // URI that we don't allow into one we do. So instead, we just
  114. // check if the scheme can be dropped because there is no host
  115. // and it is our default scheme.
  116. if (!is_null($this->scheme) && is_null($this->host) || $this->host === '') {
  117. // support for relative paths is pretty abysmal when the
  118. // scheme is present, so axe it when possible
  119. $def = $config->getDefinition('URI');
  120. if ($def->defaultScheme === $this->scheme) {
  121. $this->scheme = null;
  122. }
  123. }
  124. // validate username
  125. if (!is_null($this->userinfo)) {
  126. $encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . ':');
  127. $this->userinfo = $encoder->encode($this->userinfo);
  128. }
  129. // validate port
  130. if (!is_null($this->port)) {
  131. if ($this->port < 1 || $this->port > 65535) {
  132. $this->port = null;
  133. }
  134. }
  135. // validate path
  136. $segments_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/');
  137. if (!is_null($this->host)) { // this catches $this->host === ''
  138. // path-abempty (hier and relative)
  139. // http://www.example.com/my/path
  140. // //www.example.com/my/path (looks odd, but works, and
  141. // recognized by most browsers)
  142. // (this set is valid or invalid on a scheme by scheme
  143. // basis, so we'll deal with it later)
  144. // file:///my/path
  145. // ///my/path
  146. $this->path = $segments_encoder->encode($this->path);
  147. } elseif ($this->path !== '') {
  148. if ($this->path[0] === '/') {
  149. // path-absolute (hier and relative)
  150. // http:/my/path
  151. // /my/path
  152. if (strlen($this->path) >= 2 && $this->path[1] === '/') {
  153. // This could happen if both the host gets stripped
  154. // out
  155. // http://my/path
  156. // //my/path
  157. $this->path = '';
  158. } else {
  159. $this->path = $segments_encoder->encode($this->path);
  160. }
  161. } elseif (!is_null($this->scheme)) {
  162. // path-rootless (hier)
  163. // http:my/path
  164. // Short circuit evaluation means we don't need to check nz
  165. $this->path = $segments_encoder->encode($this->path);
  166. } else {
  167. // path-noscheme (relative)
  168. // my/path
  169. // (once again, not checking nz)
  170. $segment_nc_encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . '@');
  171. $c = strpos($this->path, '/');
  172. if ($c !== false) {
  173. $this->path =
  174. $segment_nc_encoder->encode(substr($this->path, 0, $c)) .
  175. $segments_encoder->encode(substr($this->path, $c));
  176. } else {
  177. $this->path = $segment_nc_encoder->encode($this->path);
  178. }
  179. }
  180. } else {
  181. // path-empty (hier and relative)
  182. $this->path = ''; // just to be safe
  183. }
  184. // qf = query and fragment
  185. $qf_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/?');
  186. if (!is_null($this->query)) {
  187. $this->query = $qf_encoder->encode($this->query);
  188. }
  189. if (!is_null($this->fragment)) {
  190. $this->fragment = $qf_encoder->encode($this->fragment);
  191. }
  192. return true;
  193. }
  194. /**
  195. * Convert URI back to string
  196. * @return string URI appropriate for output
  197. */
  198. public function toString()
  199. {
  200. // reconstruct authority
  201. $authority = null;
  202. // there is a rendering difference between a null authority
  203. // (http:foo-bar) and an empty string authority
  204. // (http:///foo-bar).
  205. if (!is_null($this->host)) {
  206. $authority = '';
  207. if (!is_null($this->userinfo)) {
  208. $authority .= $this->userinfo . '@';
  209. }
  210. $authority .= $this->host;
  211. if (!is_null($this->port)) {
  212. $authority .= ':' . $this->port;
  213. }
  214. }
  215. // Reconstruct the result
  216. // One might wonder about parsing quirks from browsers after
  217. // this reconstruction. Unfortunately, parsing behavior depends
  218. // on what *scheme* was employed (file:///foo is handled *very*
  219. // differently than http:///foo), so unfortunately we have to
  220. // defer to the schemes to do the right thing.
  221. $result = '';
  222. if (!is_null($this->scheme)) {
  223. $result .= $this->scheme . ':';
  224. }
  225. if (!is_null($authority)) {
  226. $result .= '//' . $authority;
  227. }
  228. $result .= $this->path;
  229. if (!is_null($this->query)) {
  230. $result .= '?' . $this->query;
  231. }
  232. if (!is_null($this->fragment)) {
  233. $result .= '#' . $this->fragment;
  234. }
  235. return $result;
  236. }
  237. /**
  238. * Returns true if this URL might be considered a 'local' URL given
  239. * the current context. This is true when the host is null, or
  240. * when it matches the host supplied to the configuration.
  241. *
  242. * Note that this does not do any scheme checking, so it is mostly
  243. * only appropriate for metadata that doesn't care about protocol
  244. * security. isBenign is probably what you actually want.
  245. * @param HTMLPurifier_Config $config
  246. * @param HTMLPurifier_Context $context
  247. * @return bool
  248. */
  249. public function isLocal($config, $context)
  250. {
  251. if ($this->host === null) {
  252. return true;
  253. }
  254. $uri_def = $config->getDefinition('URI');
  255. if ($uri_def->host === $this->host) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Returns true if this URL should be considered a 'benign' URL,
  262. * that is:
  263. *
  264. * - It is a local URL (isLocal), and
  265. * - It has a equal or better level of security
  266. * @param HTMLPurifier_Config $config
  267. * @param HTMLPurifier_Context $context
  268. * @return bool
  269. */
  270. public function isBenign($config, $context)
  271. {
  272. if (!$this->isLocal($config, $context)) {
  273. return false;
  274. }
  275. $scheme_obj = $this->getSchemeObj($config, $context);
  276. if (!$scheme_obj) {
  277. return false;
  278. } // conservative approach
  279. $current_scheme_obj = $config->getDefinition('URI')->getDefaultScheme($config, $context);
  280. if ($current_scheme_obj->secure) {
  281. if (!$scheme_obj->secure) {
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. }
  288. // vim: et sw=4 sts=4