Skip to content

Commit

Permalink
Guard against : in template resource paths, not output paths
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-aws committed Oct 24, 2024
1 parent a70ba35 commit 28f32c8
Showing 1 changed file with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public static void writeTokenTreesIntoDir(
/**
* Evaluate a simple template as a resource under "/templates/<templatePath>"
* and then write it to "<rootPath>/<templateOutputPath>".
* The template output path is also evaluated as in {@link #safeEvalPathTemplate(String, Map)}
* The template output path is also evaluated as in {@link #evalTemplate(String, Map)}
* so the path can be customized by parameter values as well.
*
* @see #safeEvalPathTemplate(String, Map)
* @see #evalTemplate(String, Map)
*/
public static void writeTemplatedFile(
Expand All @@ -84,7 +83,7 @@ public static void writeTemplatedFile(
) {
final String content = evalTemplate(klass, templatePath, parameters);
final Path outputPath = rootPath.resolve(
safeEvalPathTemplate(templateOutputPath, parameters)
evalTemplate(templateOutputPath, parameters)
);

try {
Expand All @@ -98,37 +97,29 @@ public static void writeTemplatedFile(
}

/**
* Evaluate a template string representing a file path.
* Note that ':' can't be used in file paths on Windows,
* Evaluate a simple template from a resource file using a {@link SimpleCodeWriter}.
* See {@link software.amazon.smithy.utils.AbstractCodeWriter} for documentation
* on the templating syntax.
*
* Note that ':' can't be used in resource paths on Windows,
* so we use ';' instead and replace it with ':' before evaluating the templated path.
* We also explicitly reject ':' in paths in case someone accidentally
* uses that and doesn't test on Windows (purely hypothetically :)
*/
public static String safeEvalPathTemplate(
final String pathTemplate,
final Map<String, String> parameters
public static String evalTemplate(
Class<?> klass,
String templatePath,
Map<String, String> context
) {
if (pathTemplate.contains(":")) {
if (templatePath.contains(":")) {
throw new IllegalArgumentException(
"':' cannot be used in template paths since they are not allowed on Windows. Use ';' instead."
);
}
return evalTemplate(pathTemplate.replace(';', ':'), parameters);
}

/**
* Evaluate a simple template from a resource file using a {@link SimpleCodeWriter}.
* See {@link software.amazon.smithy.utils.AbstractCodeWriter} for documentation
* on the templating syntax.
*/
public static String evalTemplate(
Class<?> klass,
String templatePath,
Map<String, String> context
) {
final String template = IoUtils.readUtf8Resource(
klass,
"/templates/" + templatePath
"/templates/" + templatePath.replace(';', ':')
);
return evalTemplate(template, context);
}
Expand Down

0 comments on commit 28f32c8

Please sign in to comment.