THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
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.

608 lines
18KB

  1. #include "cache.h"
  2. #include "urlmatch.h"
  3. #define URL_ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  4. #define URL_DIGIT "0123456789"
  5. #define URL_ALPHADIGIT URL_ALPHA URL_DIGIT
  6. #define URL_SCHEME_CHARS URL_ALPHADIGIT "+.-"
  7. #define URL_HOST_CHARS URL_ALPHADIGIT ".-[:]" /* IPv6 literals need [:] */
  8. #define URL_UNSAFE_CHARS " <>\"%{}|\\^`" /* plus 0x00-0x1F,0x7F-0xFF */
  9. #define URL_GEN_RESERVED ":/?#[]@"
  10. #define URL_SUB_RESERVED "!$&'()*+,;="
  11. #define URL_RESERVED URL_GEN_RESERVED URL_SUB_RESERVED /* only allowed delims */
  12. static int append_normalized_escapes(struct strbuf *buf,
  13. const char *from,
  14. size_t from_len,
  15. const char *esc_extra,
  16. const char *esc_ok)
  17. {
  18. /*
  19. * Append to strbuf 'buf' characters from string 'from' with length
  20. * 'from_len' while unescaping characters that do not need to be escaped
  21. * and escaping characters that do. The set of characters to escape
  22. * (the complement of which is unescaped) starts out as the RFC 3986
  23. * unsafe characters (0x00-0x1F,0x7F-0xFF," <>\"#%{}|\\^`"). If
  24. * 'esc_extra' is not NULL, those additional characters will also always
  25. * be escaped. If 'esc_ok' is not NULL, those characters will be left
  26. * escaped if found that way, but will not be unescaped otherwise (used
  27. * for delimiters). If a %-escape sequence is encountered that is not
  28. * followed by 2 hexadecimal digits, the sequence is invalid and
  29. * false (0) will be returned. Otherwise true (1) will be returned for
  30. * success.
  31. *
  32. * Note that all %-escape sequences will be normalized to UPPERCASE
  33. * as indicated in RFC 3986. Unless included in esc_extra or esc_ok
  34. * alphanumerics and "-._~" will always be unescaped as per RFC 3986.
  35. */
  36. while (from_len) {
  37. int ch = *from++;
  38. int was_esc = 0;
  39. from_len--;
  40. if (ch == '%') {
  41. if (from_len < 2)
  42. return 0;
  43. ch = hex2chr(from);
  44. if (ch < 0)
  45. return 0;
  46. from += 2;
  47. from_len -= 2;
  48. was_esc = 1;
  49. }
  50. if ((unsigned char)ch <= 0x1F || (unsigned char)ch >= 0x7F ||
  51. strchr(URL_UNSAFE_CHARS, ch) ||
  52. (esc_extra && strchr(esc_extra, ch)) ||
  53. (was_esc && strchr(esc_ok, ch)))
  54. strbuf_addf(buf, "%%%02X", (unsigned char)ch);
  55. else
  56. strbuf_addch(buf, ch);
  57. }
  58. return 1;
  59. }
  60. static const char *end_of_token(const char *s, int c, size_t n)
  61. {
  62. const char *next = memchr(s, c, n);
  63. if (!next)
  64. next = s + n;
  65. return next;
  66. }
  67. static int match_host(const struct url_info *url_info,
  68. const struct url_info *pattern_info)
  69. {
  70. const char *url = url_info->url + url_info->host_off;
  71. const char *pat = pattern_info->url + pattern_info->host_off;
  72. int url_len = url_info->host_len;
  73. int pat_len = pattern_info->host_len;
  74. while (url_len && pat_len) {
  75. const char *url_next = end_of_token(url, '.', url_len);
  76. const char *pat_next = end_of_token(pat, '.', pat_len);
  77. if (pat_next == pat + 1 && pat[0] == '*')
  78. /* wildcard matches anything */
  79. ;
  80. else if ((pat_next - pat) == (url_next - url) &&
  81. !memcmp(url, pat, url_next - url))
  82. /* the components are the same */
  83. ;
  84. else
  85. return 0; /* found an unmatch */
  86. if (url_next < url + url_len)
  87. url_next++;
  88. url_len -= url_next - url;
  89. url = url_next;
  90. if (pat_next < pat + pat_len)
  91. pat_next++;
  92. pat_len -= pat_next - pat;
  93. pat = pat_next;
  94. }
  95. return (!url_len && !pat_len);
  96. }
  97. static char *url_normalize_1(const char *url, struct url_info *out_info, char allow_globs)
  98. {
  99. /*
  100. * Normalize NUL-terminated url using the following rules:
  101. *
  102. * 1. Case-insensitive parts of url will be converted to lower case
  103. * 2. %-encoded characters that do not need to be will be unencoded
  104. * 3. Characters that are not %-encoded and must be will be encoded
  105. * 4. All %-encodings will be converted to upper case hexadecimal
  106. * 5. Leading 0s are removed from port numbers
  107. * 6. If the default port for the scheme is given it will be removed
  108. * 7. A path part (including empty) not starting with '/' has one added
  109. * 8. Any dot segments (. or ..) in the path are resolved and removed
  110. * 9. IPv6 host literals are allowed (but not normalized or validated)
  111. *
  112. * The rules are based on information in RFC 3986.
  113. *
  114. * Please note this function requires a full URL including a scheme
  115. * and host part (except for file: URLs which may have an empty host).
  116. *
  117. * The return value is a newly allocated string that must be freed
  118. * or NULL if the url is not valid.
  119. *
  120. * If out_info is non-NULL, the url and err fields therein will always
  121. * be set. If a non-NULL value is returned, it will be stored in
  122. * out_info->url as well, out_info->err will be set to NULL and the
  123. * other fields of *out_info will also be filled in. If a NULL value
  124. * is returned, NULL will be stored in out_info->url and out_info->err
  125. * will be set to a brief, translated, error message, but no other
  126. * fields will be filled in.
  127. *
  128. * This is NOT a URL validation function. Full URL validation is NOT
  129. * performed. Some invalid host names are passed through this function
  130. * undetected. However, most all other problems that make a URL invalid
  131. * will be detected (including a missing host for non file: URLs).
  132. */
  133. size_t url_len = strlen(url);
  134. struct strbuf norm;
  135. size_t spanned;
  136. size_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0;
  137. size_t host_off=0, host_len=0, port_off=0, port_len=0, path_off, path_len, result_len;
  138. const char *slash_ptr, *at_ptr, *colon_ptr, *path_start;
  139. char *result;
  140. /*
  141. * Copy lowercased scheme and :// suffix, %-escapes are not allowed
  142. * First character of scheme must be URL_ALPHA
  143. */
  144. spanned = strspn(url, URL_SCHEME_CHARS);
  145. if (!spanned || !isalpha(url[0]) || spanned + 3 > url_len ||
  146. url[spanned] != ':' || url[spanned+1] != '/' || url[spanned+2] != '/') {
  147. if (out_info) {
  148. out_info->url = NULL;
  149. out_info->err = _("invalid URL scheme name or missing '://' suffix");
  150. }
  151. return NULL; /* Bad scheme and/or missing "://" part */
  152. }
  153. strbuf_init(&norm, url_len);
  154. scheme_len = spanned;
  155. spanned += 3;
  156. url_len -= spanned;
  157. while (spanned--)
  158. strbuf_addch(&norm, tolower(*url++));
  159. /*
  160. * Copy any username:password if present normalizing %-escapes
  161. */
  162. at_ptr = strchr(url, '@');
  163. slash_ptr = url + strcspn(url, "/?#");
  164. if (at_ptr && at_ptr < slash_ptr) {
  165. user_off = norm.len;
  166. if (at_ptr > url) {
  167. if (!append_normalized_escapes(&norm, url, at_ptr - url,
  168. "", URL_RESERVED)) {
  169. if (out_info) {
  170. out_info->url = NULL;
  171. out_info->err = _("invalid %XX escape sequence");
  172. }
  173. strbuf_release(&norm);
  174. return NULL;
  175. }
  176. colon_ptr = strchr(norm.buf + scheme_len + 3, ':');
  177. if (colon_ptr) {
  178. passwd_off = (colon_ptr + 1) - norm.buf;
  179. passwd_len = norm.len - passwd_off;
  180. user_len = (passwd_off - 1) - (scheme_len + 3);
  181. } else {
  182. user_len = norm.len - (scheme_len + 3);
  183. }
  184. }
  185. strbuf_addch(&norm, '@');
  186. url_len -= (++at_ptr - url);
  187. url = at_ptr;
  188. }
  189. /*
  190. * Copy the host part excluding any port part, no %-escapes allowed
  191. */
  192. if (!url_len || strchr(":/?#", *url)) {
  193. /* Missing host invalid for all URL schemes except file */
  194. if (strncmp(norm.buf, "file:", 5)) {
  195. if (out_info) {
  196. out_info->url = NULL;
  197. out_info->err = _("missing host and scheme is not 'file:'");
  198. }
  199. strbuf_release(&norm);
  200. return NULL;
  201. }
  202. } else {
  203. host_off = norm.len;
  204. }
  205. colon_ptr = slash_ptr - 1;
  206. while (colon_ptr > url && *colon_ptr != ':' && *colon_ptr != ']')
  207. colon_ptr--;
  208. if (*colon_ptr != ':') {
  209. colon_ptr = slash_ptr;
  210. } else if (!host_off && colon_ptr < slash_ptr && colon_ptr + 1 != slash_ptr) {
  211. /* file: URLs may not have a port number */
  212. if (out_info) {
  213. out_info->url = NULL;
  214. out_info->err = _("a 'file:' URL may not have a port number");
  215. }
  216. strbuf_release(&norm);
  217. return NULL;
  218. }
  219. if (allow_globs)
  220. spanned = strspn(url, URL_HOST_CHARS "*");
  221. else
  222. spanned = strspn(url, URL_HOST_CHARS);
  223. if (spanned < colon_ptr - url) {
  224. /* Host name has invalid characters */
  225. if (out_info) {
  226. out_info->url = NULL;
  227. out_info->err = _("invalid characters in host name");
  228. }
  229. strbuf_release(&norm);
  230. return NULL;
  231. }
  232. while (url < colon_ptr) {
  233. strbuf_addch(&norm, tolower(*url++));
  234. url_len--;
  235. }
  236. /*
  237. * Check the port part and copy if not the default (after removing any
  238. * leading 0s); no %-escapes allowed
  239. */
  240. if (colon_ptr < slash_ptr) {
  241. /* skip the ':' and leading 0s but not the last one if all 0s */
  242. url++;
  243. url += strspn(url, "0");
  244. if (url == slash_ptr && url[-1] == '0')
  245. url--;
  246. if (url == slash_ptr) {
  247. /* Skip ":" port with no number, it's same as default */
  248. } else if (slash_ptr - url == 2 &&
  249. !strncmp(norm.buf, "http:", 5) &&
  250. !strncmp(url, "80", 2)) {
  251. /* Skip http :80 as it's the default */
  252. } else if (slash_ptr - url == 3 &&
  253. !strncmp(norm.buf, "https:", 6) &&
  254. !strncmp(url, "443", 3)) {
  255. /* Skip https :443 as it's the default */
  256. } else {
  257. /*
  258. * Port number must be all digits with leading 0s removed
  259. * and since all the protocols we deal with have a 16-bit
  260. * port number it must also be in the range 1..65535
  261. * 0 is not allowed because that means "next available"
  262. * on just about every system and therefore cannot be used
  263. */
  264. unsigned long pnum = 0;
  265. spanned = strspn(url, URL_DIGIT);
  266. if (spanned < slash_ptr - url) {
  267. /* port number has invalid characters */
  268. if (out_info) {
  269. out_info->url = NULL;
  270. out_info->err = _("invalid port number");
  271. }
  272. strbuf_release(&norm);
  273. return NULL;
  274. }
  275. if (slash_ptr - url <= 5)
  276. pnum = strtoul(url, NULL, 10);
  277. if (pnum == 0 || pnum > 65535) {
  278. /* port number not in range 1..65535 */
  279. if (out_info) {
  280. out_info->url = NULL;
  281. out_info->err = _("invalid port number");
  282. }
  283. strbuf_release(&norm);
  284. return NULL;
  285. }
  286. strbuf_addch(&norm, ':');
  287. port_off = norm.len;
  288. strbuf_add(&norm, url, slash_ptr - url);
  289. port_len = slash_ptr - url;
  290. }
  291. url_len -= slash_ptr - colon_ptr;
  292. url = slash_ptr;
  293. }
  294. if (host_off)
  295. host_len = norm.len - host_off - (port_len ? port_len + 1 : 0);
  296. /*
  297. * Now copy the path resolving any . and .. segments being careful not
  298. * to corrupt the URL by unescaping any delimiters, but do add an
  299. * initial '/' if it's missing and do normalize any %-escape sequences.
  300. */
  301. path_off = norm.len;
  302. path_start = norm.buf + path_off;
  303. strbuf_addch(&norm, '/');
  304. if (*url == '/') {
  305. url++;
  306. url_len--;
  307. }
  308. for (;;) {
  309. const char *seg_start;
  310. size_t seg_start_off = norm.len;
  311. const char *next_slash = url + strcspn(url, "/?#");
  312. int skip_add_slash = 0;
  313. /*
  314. * RFC 3689 indicates that any . or .. segments should be
  315. * unescaped before being checked for.
  316. */
  317. if (!append_normalized_escapes(&norm, url, next_slash - url, "",
  318. URL_RESERVED)) {
  319. if (out_info) {
  320. out_info->url = NULL;
  321. out_info->err = _("invalid %XX escape sequence");
  322. }
  323. strbuf_release(&norm);
  324. return NULL;
  325. }
  326. seg_start = norm.buf + seg_start_off;
  327. if (!strcmp(seg_start, ".")) {
  328. /* ignore a . segment; be careful not to remove initial '/' */
  329. if (seg_start == path_start + 1) {
  330. strbuf_setlen(&norm, norm.len - 1);
  331. skip_add_slash = 1;
  332. } else {
  333. strbuf_setlen(&norm, norm.len - 2);
  334. }
  335. } else if (!strcmp(seg_start, "..")) {
  336. /*
  337. * ignore a .. segment and remove the previous segment;
  338. * be careful not to remove initial '/' from path
  339. */
  340. const char *prev_slash = norm.buf + norm.len - 3;
  341. if (prev_slash == path_start) {
  342. /* invalid .. because no previous segment to remove */
  343. if (out_info) {
  344. out_info->url = NULL;
  345. out_info->err = _("invalid '..' path segment");
  346. }
  347. strbuf_release(&norm);
  348. return NULL;
  349. }
  350. while (*--prev_slash != '/') {}
  351. if (prev_slash == path_start) {
  352. strbuf_setlen(&norm, prev_slash - norm.buf + 1);
  353. skip_add_slash = 1;
  354. } else {
  355. strbuf_setlen(&norm, prev_slash - norm.buf);
  356. }
  357. }
  358. url_len -= next_slash - url;
  359. url = next_slash;
  360. /* if the next char is not '/' done with the path */
  361. if (*url != '/')
  362. break;
  363. url++;
  364. url_len--;
  365. if (!skip_add_slash)
  366. strbuf_addch(&norm, '/');
  367. }
  368. path_len = norm.len - path_off;
  369. /*
  370. * Now simply copy the rest, if any, only normalizing %-escapes and
  371. * being careful not to corrupt the URL by unescaping any delimiters.
  372. */
  373. if (*url) {
  374. if (!append_normalized_escapes(&norm, url, url_len, "", URL_RESERVED)) {
  375. if (out_info) {
  376. out_info->url = NULL;
  377. out_info->err = _("invalid %XX escape sequence");
  378. }
  379. strbuf_release(&norm);
  380. return NULL;
  381. }
  382. }
  383. result = strbuf_detach(&norm, &result_len);
  384. if (out_info) {
  385. out_info->url = result;
  386. out_info->err = NULL;
  387. out_info->url_len = result_len;
  388. out_info->scheme_len = scheme_len;
  389. out_info->user_off = user_off;
  390. out_info->user_len = user_len;
  391. out_info->passwd_off = passwd_off;
  392. out_info->passwd_len = passwd_len;
  393. out_info->host_off = host_off;
  394. out_info->host_len = host_len;
  395. out_info->port_off = port_off;
  396. out_info->port_len = port_len;
  397. out_info->path_off = path_off;
  398. out_info->path_len = path_len;
  399. }
  400. return result;
  401. }
  402. char *url_normalize(const char *url, struct url_info *out_info)
  403. {
  404. return url_normalize_1(url, out_info, 0);
  405. }
  406. static size_t url_match_prefix(const char *url,
  407. const char *url_prefix,
  408. size_t url_prefix_len)
  409. {
  410. /*
  411. * url_prefix matches url if url_prefix is an exact match for url or it
  412. * is a prefix of url and the match ends on a path component boundary.
  413. * Both url and url_prefix are considered to have an implicit '/' on the
  414. * end for matching purposes if they do not already.
  415. *
  416. * url must be NUL terminated. url_prefix_len is the length of
  417. * url_prefix which need not be NUL terminated.
  418. *
  419. * The return value is the length of the match in characters (including
  420. * the final '/' even if it's implicit) or 0 for no match.
  421. *
  422. * Passing NULL as url and/or url_prefix will always cause 0 to be
  423. * returned without causing any faults.
  424. */
  425. if (!url || !url_prefix)
  426. return 0;
  427. if (!url_prefix_len || (url_prefix_len == 1 && *url_prefix == '/'))
  428. return (!*url || *url == '/') ? 1 : 0;
  429. if (url_prefix[url_prefix_len - 1] == '/')
  430. url_prefix_len--;
  431. if (strncmp(url, url_prefix, url_prefix_len))
  432. return 0;
  433. if ((strlen(url) == url_prefix_len) || (url[url_prefix_len] == '/'))
  434. return url_prefix_len + 1;
  435. return 0;
  436. }
  437. static int match_urls(const struct url_info *url,
  438. const struct url_info *url_prefix,
  439. struct urlmatch_item *match)
  440. {
  441. /*
  442. * url_prefix matches url if the scheme, host and port of url_prefix
  443. * are the same as those of url and the path portion of url_prefix
  444. * is the same as the path portion of url or it is a prefix that
  445. * matches at a '/' boundary. If url_prefix contains a user name,
  446. * that must also exactly match the user name in url.
  447. *
  448. * If the user, host, port and path match in this fashion, the returned
  449. * value is the length of the path match including any implicit
  450. * final '/'. For example, "http://me@example.com/path" is matched by
  451. * "http://example.com" with a path length of 1.
  452. *
  453. * If there is a match and exactusermatch is not NULL, then
  454. * *exactusermatch will be set to true if both url and url_prefix
  455. * contained a user name or false if url_prefix did not have a
  456. * user name. If there is no match *exactusermatch is left untouched.
  457. */
  458. char usermatched = 0;
  459. size_t pathmatchlen;
  460. if (!url || !url_prefix || !url->url || !url_prefix->url)
  461. return 0;
  462. /* check the scheme */
  463. if (url_prefix->scheme_len != url->scheme_len ||
  464. strncmp(url->url, url_prefix->url, url->scheme_len))
  465. return 0; /* schemes do not match */
  466. /* check the user name if url_prefix has one */
  467. if (url_prefix->user_off) {
  468. if (!url->user_off || url->user_len != url_prefix->user_len ||
  469. strncmp(url->url + url->user_off,
  470. url_prefix->url + url_prefix->user_off,
  471. url->user_len))
  472. return 0; /* url_prefix has a user but it's not a match */
  473. usermatched = 1;
  474. }
  475. /* check the host */
  476. if (!match_host(url, url_prefix))
  477. return 0; /* host names do not match */
  478. /* check the port */
  479. if (url_prefix->port_len != url->port_len ||
  480. strncmp(url->url + url->port_off,
  481. url_prefix->url + url_prefix->port_off, url->port_len))
  482. return 0; /* ports do not match */
  483. /* check the path */
  484. pathmatchlen = url_match_prefix(
  485. url->url + url->path_off,
  486. url_prefix->url + url_prefix->path_off,
  487. url_prefix->url_len - url_prefix->path_off);
  488. if (!pathmatchlen)
  489. return 0; /* paths do not match */
  490. if (match) {
  491. match->hostmatch_len = url_prefix->host_len;
  492. match->pathmatch_len = pathmatchlen;
  493. match->user_matched = usermatched;
  494. }
  495. return 1;
  496. }
  497. static int cmp_matches(const struct urlmatch_item *a,
  498. const struct urlmatch_item *b)
  499. {
  500. if (a->hostmatch_len != b->hostmatch_len)
  501. return a->hostmatch_len < b->hostmatch_len ? -1 : 1;
  502. if (a->pathmatch_len != b->pathmatch_len)
  503. return a->pathmatch_len < b->pathmatch_len ? -1 : 1;
  504. if (a->user_matched != b->user_matched)
  505. return b->user_matched ? -1 : 1;
  506. return 0;
  507. }
  508. int urlmatch_config_entry(const char *var, const char *value, void *cb)
  509. {
  510. struct string_list_item *item;
  511. struct urlmatch_config *collect = cb;
  512. struct urlmatch_item matched = {0};
  513. struct url_info *url = &collect->url;
  514. const char *key, *dot;
  515. struct strbuf synthkey = STRBUF_INIT;
  516. int retval;
  517. if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
  518. if (collect->cascade_fn)
  519. return collect->cascade_fn(var, value, cb);
  520. return 0; /* not interested */
  521. }
  522. dot = strrchr(key, '.');
  523. if (dot) {
  524. char *config_url, *norm_url;
  525. struct url_info norm_info;
  526. config_url = xmemdupz(key, dot - key);
  527. norm_url = url_normalize_1(config_url, &norm_info, 1);
  528. free(config_url);
  529. if (!norm_url)
  530. return 0;
  531. retval = match_urls(url, &norm_info, &matched);
  532. free(norm_url);
  533. if (!retval)
  534. return 0;
  535. key = dot + 1;
  536. }
  537. if (collect->key && strcmp(key, collect->key))
  538. return 0;
  539. item = string_list_insert(&collect->vars, key);
  540. if (!item->util) {
  541. item->util = xcalloc(1, sizeof(matched));
  542. } else {
  543. if (cmp_matches(&matched, item->util) < 0)
  544. /*
  545. * Our match is worse than the old one,
  546. * we cannot use it.
  547. */
  548. return 0;
  549. /* Otherwise, replace it with this one. */
  550. }
  551. memcpy(item->util, &matched, sizeof(matched));
  552. strbuf_addstr(&synthkey, collect->section);
  553. strbuf_addch(&synthkey, '.');
  554. strbuf_addstr(&synthkey, key);
  555. retval = collect->collect_fn(synthkey.buf, value, collect->cb);
  556. strbuf_release(&synthkey);
  557. return retval;
  558. }