Skip to content

Commit 4e0bdaa

Browse files
carlosmnpks-t
authored andcommitted
submodule: add failing test for option-injection protection in url and path
1 parent b95c79a commit 4e0bdaa

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/submodule/inject_option.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "clar_libgit2.h"
2+
#include "posix.h"
3+
#include "path.h"
4+
#include "submodule_helpers.h"
5+
#include "fileops.h"
6+
#include "repository.h"
7+
8+
static git_repository *g_repo = NULL;
9+
10+
void test_submodule_inject_option__initialize(void)
11+
{
12+
g_repo = setup_fixture_submodule_simple();
13+
}
14+
15+
void test_submodule_inject_option__cleanup(void)
16+
{
17+
cl_git_sandbox_cleanup();
18+
}
19+
20+
static int find_naughty(git_submodule *sm, const char *name, void *payload)
21+
{
22+
int *foundit = (int *) payload;
23+
24+
GIT_UNUSED(sm);
25+
26+
if (!git__strcmp("naughty", name))
27+
*foundit = true;
28+
29+
return 0;
30+
}
31+
32+
void test_submodule_inject_option__url(void)
33+
{
34+
int foundit;
35+
git_submodule *sm;
36+
git_buf buf = GIT_BUF_INIT;
37+
38+
cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
39+
cl_git_rewritefile(buf.ptr,
40+
"[submodule \"naughty\"]\n"
41+
" path = testrepo\n"
42+
" url = -u./payload\n");
43+
git_buf_dispose(&buf);
44+
45+
/* We do want to find it, but with the appropriate field empty */
46+
foundit = 0;
47+
cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
48+
cl_assert_equal_i(1, foundit);
49+
50+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
51+
cl_assert_equal_s("testrepo", git_submodule_path(sm));
52+
cl_assert_equal_p(NULL, git_submodule_url(sm));
53+
54+
git_submodule_free(sm);
55+
}
56+
57+
void test_submodule_inject_option__path(void)
58+
{
59+
int foundit;
60+
git_submodule *sm;
61+
git_buf buf = GIT_BUF_INIT;
62+
63+
cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
64+
cl_git_rewritefile(buf.ptr,
65+
"[submodule \"naughty\"]\n"
66+
" path = --something\n"
67+
" url = blah.git\n");
68+
git_buf_dispose(&buf);
69+
70+
/* We do want to find it, but with the appropriate field empty */
71+
foundit = 0;
72+
cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
73+
cl_assert_equal_i(1, foundit);
74+
75+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
76+
cl_assert_equal_s("naughty", git_submodule_path(sm));
77+
cl_assert_equal_s("blah.git", git_submodule_url(sm));
78+
79+
git_submodule_free(sm);
80+
}

0 commit comments

Comments
 (0)