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