Skip to content

Commit bc9627a

Browse files
olevittfcomte
andauthored
Sanitize helm names and namespace (#542)
Co-authored-by: fcomte <frederic.comte@insee.fr>
1 parent fa3cbee commit bc9627a

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ public HelmInstaller installChart(
175175
}
176176
command.append(chart + " ");
177177
command.append("-n ");
178+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
179+
throw new IllegalArgumentException(
180+
"Invalid namespace "
181+
+ namespace
182+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
183+
}
178184
safeConcat(command, namespace);
179185
if (StringUtils.isNotBlank(version)) {
180186
if (!semverPattern.matcher(version).matches()) {
@@ -205,6 +211,18 @@ public HelmInstaller installChart(
205211

206212
public int uninstaller(HelmConfiguration configuration, String name, String namespace)
207213
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
214+
if (name.length() > 53 || !rfc1123Pattern.matcher(name).matches()) {
215+
throw new IllegalArgumentException(
216+
"Invalid release "
217+
+ name
218+
+ ". Must be 53 or fewer characters and be a valid RFC 1123 string.");
219+
}
220+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
221+
throw new IllegalArgumentException(
222+
"Invalid namespace "
223+
+ namespace
224+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
225+
}
208226
StringBuilder command = new StringBuilder("helm uninstall ");
209227
safeConcat(command, name);
210228
command.append(" -n ");
@@ -215,6 +233,12 @@ public int uninstaller(HelmConfiguration configuration, String name, String name
215233
public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespace)
216234
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
217235
StringBuilder command = new StringBuilder("helm ls -a");
236+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
237+
throw new IllegalArgumentException(
238+
"Invalid namespace "
239+
+ namespace
240+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
241+
}
218242
if (namespace != null) {
219243
command.append(" -n ");
220244
safeConcat(command, namespace);
@@ -241,6 +265,18 @@ public String getNotes(HelmConfiguration configuration, String id, String namesp
241265

242266
public HelmReleaseInfo getAll(HelmConfiguration configuration, String id, String namespace) {
243267
StringBuilder command = new StringBuilder("helm get all ");
268+
if (id.length() > 53 || !rfc1123Pattern.matcher(id).matches()) {
269+
throw new IllegalArgumentException(
270+
"Invalid release "
271+
+ id
272+
+ ". Must be 53 or fewer characters and be a valid RFC 1123 string.");
273+
}
274+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
275+
throw new IllegalArgumentException(
276+
"Invalid namespace "
277+
+ namespace
278+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
279+
}
244280
safeConcat(command, id);
245281
command.append(" --namespace ");
246282
safeConcat(command, namespace);
@@ -260,6 +296,18 @@ private String getReleaseInfo(
260296
throw new IllegalArgumentException(
261297
"Invalid info type " + infoType + ", should be manifest, notes or values");
262298
}
299+
if (id.length() > 53 || !rfc1123Pattern.matcher(id).matches()) {
300+
throw new IllegalArgumentException(
301+
"Invalid release "
302+
+ id
303+
+ ". Must be 53 or fewer characters and be a valid RFC 1123 string.");
304+
}
305+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
306+
throw new IllegalArgumentException(
307+
"Invalid namespace "
308+
+ namespace
309+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
310+
}
263311
StringBuilder command = new StringBuilder("helm get " + infoType + " ");
264312
try {
265313
safeConcat(command, id);
@@ -306,7 +354,12 @@ public HelmLs getAppById(HelmConfiguration configuration, String appId, String n
306354
+ appId
307355
+ ". Must be 53 or fewer characters and be a valid RFC 1123 string.");
308356
}
309-
357+
if (namespace.length() > 63 || !rfc1123Pattern.matcher(namespace).matches()) {
358+
throw new IllegalArgumentException(
359+
"Invalid namespace "
360+
+ namespace
361+
+ ". Must be 63 or fewer characters and be a valid RFC 1123 string.");
362+
}
310363
StringBuilder command = new StringBuilder("helm list --filter ");
311364
safeConcat(command, appId);
312365
command.append(" -n ");

0 commit comments

Comments
 (0)