dotdot.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "dotdot.h"
  25. #include "curl_memory.h"
  26. /* The last #include file should be: */
  27. #include "memdebug.h"
  28. /*
  29. * "Remove Dot Segments"
  30. * https://tools.ietf.org/html/rfc3986#section-5.2.4
  31. */
  32. /*
  33. * Curl_dedotdotify()
  34. * @unittest: 1395
  35. *
  36. * This function gets a zero-terminated path with dot and dotdot sequences
  37. * passed in and strips them off according to the rules in RFC 3986 section
  38. * 5.2.4.
  39. *
  40. * The function handles a query part ('?' + stuff) appended but it expects
  41. * that fragments ('#' + stuff) have already been cut off.
  42. *
  43. * RETURNS
  44. *
  45. * an allocated dedotdotified output string
  46. */
  47. char *Curl_dedotdotify(const char *input)
  48. {
  49. size_t inlen = strlen(input);
  50. char *clone;
  51. size_t clen = inlen; /* the length of the cloned input */
  52. char *out = malloc(inlen+1);
  53. char *outptr;
  54. char *orgclone;
  55. char *queryp;
  56. if(!out)
  57. return NULL; /* out of memory */
  58. /* get a cloned copy of the input */
  59. clone = strdup(input);
  60. if(!clone) {
  61. free(out);
  62. return NULL;
  63. }
  64. orgclone = clone;
  65. outptr = out;
  66. if(!*clone) {
  67. /* zero length string, return that */
  68. free(out);
  69. return clone;
  70. }
  71. /*
  72. * To handle query-parts properly, we must find it and remove it during the
  73. * dotdot-operation and then append it again at the end to the output
  74. * string.
  75. */
  76. queryp = strchr(clone, '?');
  77. if(queryp)
  78. *queryp = 0;
  79. do {
  80. /* A. If the input buffer begins with a prefix of "../" or "./", then
  81. remove that prefix from the input buffer; otherwise, */
  82. if(!strncmp("./", clone, 2)) {
  83. clone+=2;
  84. clen-=2;
  85. }
  86. else if(!strncmp("../", clone, 3)) {
  87. clone+=3;
  88. clen-=3;
  89. }
  90. /* B. if the input buffer begins with a prefix of "/./" or "/.", where
  91. "." is a complete path segment, then replace that prefix with "/" in
  92. the input buffer; otherwise, */
  93. else if(!strncmp("/./", clone, 3)) {
  94. clone+=2;
  95. clen-=2;
  96. }
  97. else if(!strcmp("/.", clone)) {
  98. clone[1]='/';
  99. clone++;
  100. clen-=1;
  101. }
  102. /* C. if the input buffer begins with a prefix of "/../" or "/..", where
  103. ".." is a complete path segment, then replace that prefix with "/" in
  104. the input buffer and remove the last segment and its preceding "/" (if
  105. any) from the output buffer; otherwise, */
  106. else if(!strncmp("/../", clone, 4)) {
  107. clone+=3;
  108. clen-=3;
  109. /* remove the last segment from the output buffer */
  110. while(outptr > out) {
  111. outptr--;
  112. if(*outptr == '/')
  113. break;
  114. }
  115. *outptr = 0; /* zero-terminate where it stops */
  116. }
  117. else if(!strcmp("/..", clone)) {
  118. clone[2]='/';
  119. clone+=2;
  120. clen-=2;
  121. /* remove the last segment from the output buffer */
  122. while(outptr > out) {
  123. outptr--;
  124. if(*outptr == '/')
  125. break;
  126. }
  127. *outptr = 0; /* zero-terminate where it stops */
  128. }
  129. /* D. if the input buffer consists only of "." or "..", then remove
  130. that from the input buffer; otherwise, */
  131. else if(!strcmp(".", clone) || !strcmp("..", clone)) {
  132. *clone=0;
  133. *out=0;
  134. }
  135. else {
  136. /* E. move the first path segment in the input buffer to the end of
  137. the output buffer, including the initial "/" character (if any) and
  138. any subsequent characters up to, but not including, the next "/"
  139. character or the end of the input buffer. */
  140. do {
  141. *outptr++ = *clone++;
  142. clen--;
  143. } while(*clone && (*clone != '/'));
  144. *outptr = 0;
  145. }
  146. } while(*clone);
  147. if(queryp) {
  148. size_t qlen;
  149. /* There was a query part, append that to the output. The 'clone' string
  150. may now have been altered so we copy from the original input string
  151. from the correct index. */
  152. size_t oindex = queryp - orgclone;
  153. qlen = strlen(&input[oindex]);
  154. memcpy(outptr, &input[oindex], qlen+1); /* include the ending zero byte */
  155. }
  156. free(orgclone);
  157. return out;
  158. }