1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.internal.submodule;
44
45 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
46 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL;
47 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION;
48 import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_NAME;
49 import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_PARSE;
50 import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_PATH;
51 import static org.eclipse.jgit.lib.ObjectChecker.ErrorType.GITMODULES_URL;
52
53 import java.text.MessageFormat;
54
55 import org.eclipse.jgit.errors.ConfigInvalidException;
56 import org.eclipse.jgit.internal.JGitText;
57 import org.eclipse.jgit.lib.Config;
58 import org.eclipse.jgit.lib.ObjectChecker;
59
60
61
62
63
64
65
66 public class SubmoduleValidator {
67
68
69
70
71 public static class SubmoduleValidationException extends Exception {
72
73 private static final long serialVersionUID = 1L;
74
75 private final ObjectChecker.ErrorType fsckMessageId;
76
77
78
79
80
81
82
83
84 SubmoduleValidationException(String message,
85 ObjectChecker.ErrorType fsckMessageId) {
86 super(message);
87 this.fsckMessageId = fsckMessageId;
88 }
89
90
91
92
93
94 public ObjectChecker.ErrorType getFsckMessageId() {
95 return fsckMessageId;
96 }
97 }
98
99
100
101
102
103
104
105
106
107 public static void assertValidSubmoduleName(String name)
108 throws SubmoduleValidationException {
109 if (name.contains("/../") || name.contains("\\..\\")
110 || name.startsWith("../") || name.startsWith("..\\")
111 || name.endsWith("/..") || name.endsWith("\\..")) {
112
113
114
115
116
117
118
119
120
121 throw new SubmoduleValidationException(MessageFormat
122 .format(JGitText.get().invalidNameContainsDotDot, name),
123 GITMODULES_NAME);
124 }
125
126 if (name.startsWith("-")) {
127 throw new SubmoduleValidationException(
128 MessageFormat.format(
129 JGitText.get().submoduleNameInvalid, name),
130 GITMODULES_NAME);
131 }
132 }
133
134
135
136
137
138
139
140
141
142 public static void assertValidSubmoduleUri(String uri)
143 throws SubmoduleValidationException {
144 if (uri.startsWith("-")) {
145 throw new SubmoduleValidationException(
146 MessageFormat.format(
147 JGitText.get().submoduleUrlInvalid, uri),
148 GITMODULES_URL);
149 }
150 }
151
152
153
154
155
156
157
158
159
160 public static void assertValidSubmodulePath(String path)
161 throws SubmoduleValidationException {
162 if (path.startsWith("-")) {
163 throw new SubmoduleValidationException(
164 MessageFormat.format(
165 JGitText.get().submodulePathInvalid, path),
166 GITMODULES_PATH);
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179 public static void assertValidGitModulesFile(String gitModulesContents)
180 throws SubmoduleValidationException {
181 Config c = new Config();
182 try {
183 c.fromText(gitModulesContents);
184 for (String subsection :
185 c.getSubsections(CONFIG_SUBMODULE_SECTION)) {
186 assertValidSubmoduleName(subsection);
187
188 String url = c.getString(
189 CONFIG_SUBMODULE_SECTION, subsection, CONFIG_KEY_URL);
190 if (url != null) {
191 assertValidSubmoduleUri(url);
192 }
193
194 String path = c.getString(
195 CONFIG_SUBMODULE_SECTION, subsection, CONFIG_KEY_PATH);
196 if (path != null) {
197 assertValidSubmodulePath(path);
198 }
199 }
200 } catch (ConfigInvalidException e) {
201 throw new SubmoduleValidationException(
202 JGitText.get().invalidGitModules,
203 GITMODULES_PARSE);
204 }
205 }
206 }