Re: [RFH/PATCH] prefix_path(): disallow absolute paths
- From
Johannes Sixt <j.sixt@viscovery.net>
- Date
- Jan 29, 2008, 07:20 UTC
- Message-ID
- <479ED3AE.5000403@viscovery.net>
- In-Reply-To
- <7vwspts9vj.fsf@gitster.siamese.dyndns.org>
Junio C Hamano schrieb:
Show 10 quoted lines
> +static int sanitary_path_copy(char *dst, const char *src)
> {
> - const char *orig = path;
> + char *dst0 = dst;
> +
> + if (*src == '/') {
> + *dst++ = '/';
> + while (*src == '/')
> + src++;
> + }Advance notice: In this function, tests of the kind *src == '/' need to be turned into is_dir_sep(*src) when we port to Windows.
Show 9 quoted lines
> + /* copy up to the next '/', and eat all '/' */
> + while ((c = *src++) != '\0' && c != '/')
> + *dst++ = c;
> if (c == '/') {
> - path += 2;
> - continue;
> - }
> - if (c != '.')
> + *dst++ = c;*dst++ = '/';
will be needed on Windows to sanitize all is_dir_sep(c) to '/'.
Show 5 quoted lines
> + while (c == '/') > + c = *src++; > + src--; > + } else if (!c) > break;
...
Show 5 quoted lines
> +const char *prefix_path(const char *prefix, int len, const char *path)
> +{
> + const char *orig = path;
> + char *sanitized = xmalloc(len + strlen(path) + 1);
> + if (*orig == '/')if (is_absolute_path(*orig))
Show 10 quoted lines
> + strcpy(sanitized, path);
> + else {
> + if (len)
> + memcpy(sanitized, prefix, len);
> + strcpy(sanitized + len, path);
> }
> - return path;
> + if (sanitary_path_copy(sanitized, sanitized))
> + goto error_out;
> + if (*orig == '/') {Ditto.
Show 12 quoted lines
> + const char *work_tree = get_git_work_tree();
> + size_t len = strlen(work_tree);
> + if (strncmp(sanitized, work_tree, len) ||
> + (sanitized[len] != '\0' && sanitized[len] != '/')) {
> + error_out:
> + error("'%s' is outside repository", orig);
> + free(sanitized);
> + return NULL;
> + }
> + }
> + return sanitized;
> }I appreciate this new sanitary_copy_path() because I expect that we will need at least one less #ifdef __MINGW32__/#endif compared to our current Windows port.
-- Hannes