Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate injected tag identifiers #55

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.internal.file.FileOperations;
Expand Down Expand Up @@ -93,6 +94,11 @@ public void injectTags() throws IOException {
for (Map.Entry<String, Object> entry : replacements.entrySet()) {
final Object e = entry.getValue();
final String eType, eJava;
final String identifier = entry.getKey();
if (!isValidJavaIdentifier(identifier)) {
throw new InvalidUserDataException(
"Tag injection identifier " + identifier + "is not a valid Java identifier!");
}
if (e instanceof Integer) {
eType = "int";
eJava = Integer.toString((Integer) e);
Expand All @@ -103,7 +109,7 @@ public void injectTags() throws IOException {
outWriter.append(" /** Auto-generated tag from RetroFuturaGradle */\n public static final ");
outWriter.append(eType);
outWriter.append(' ');
outWriter.append(entry.getKey());
outWriter.append(identifier);
outWriter.append(" = ");
outWriter.append(eJava);
outWriter.append(";\n");
Expand All @@ -112,4 +118,19 @@ public void injectTags() throws IOException {
FileUtils.writeStringToFile(outFile, outWriter.toString(), StandardCharsets.UTF_8);
}
}

private static boolean isValidJavaIdentifier(final String s) {
if (s.isEmpty()) {
return false;
}
if (!Character.isJavaIdentifierStart(s.charAt(0))) {
return false;
}
for (int i = 1; i < s.length(); i++) {
if (!Character.isJavaIdentifierPart(s.charAt(i))) {
return false;
}
}
return true;
}
}
Loading