Skip to content

Commit 684aa51

Browse files
committed
added node name sanitization and unit test
1 parent 84d816a commit 684aa51

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,12 @@ public int run() throws Exception {
457457
//create temporary file for private key
458458
File tempHostPkFile;
459459
try {
460-
tempHostPkFile = AnsibleUtil.createTemporaryFile("","id_rsa_node_"+nodeName, privateKey,customTmpDirPath);
460+
// Sanitize node name for filesystem use (replace unsafe characters with underscores)
461+
String safeNodeName = nodeName.replaceAll("[^a-zA-Z0-9._-]", "_");
462+
if(debug && !nodeName.equals(safeNodeName)) {
463+
System.err.println("DEBUG: Sanitized node name '" + nodeName + "' to '" + safeNodeName + "' for temp file");
464+
}
465+
tempHostPkFile = AnsibleUtil.createTemporaryFile("","id_rsa_node_"+safeNodeName, privateKey,customTmpDirPath);
461466

462467
// Only the owner can read and write
463468
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();

src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerSpec.groovy

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,4 +1002,61 @@ class AnsibleRunnerSpec extends Specification{
10021002
2 * ansibleVault.encryptVariable(_, _) >> "!vault | value"
10031003
}
10041004

1005+
def "node name sanitization: should sanitize node names with forward slashes for temp files"() {
1006+
given:
1007+
// Test case from real issue: node name with forward slashes
1008+
String nodeName = "/docker-runner-ansible-ssh-node-b-3"
1009+
String sanitized = nodeName.replaceAll("[^a-zA-Z0-9._-]", "_")
1010+
1011+
expect:
1012+
sanitized == "_docker-runner-ansible-ssh-node-b-3"
1013+
// Verify sanitized name is filesystem-safe
1014+
!sanitized.contains("/")
1015+
}
1016+
1017+
def "node name sanitization: should sanitize various special characters"() {
1018+
expect:
1019+
nodeName.replaceAll("[^a-zA-Z0-9._-]", "_") == expected
1020+
1021+
where:
1022+
nodeName || expected
1023+
"simple-node" || "simple-node"
1024+
"node.with.dots" || "node.with.dots"
1025+
"node_with_underscores" || "node_with_underscores"
1026+
"/docker-runner-ansible" || "_docker-runner-ansible"
1027+
"node:with:colons" || "node_with_colons"
1028+
"node with spaces" || "node_with_spaces"
1029+
"node\\with\\backslashes" || "node_with_backslashes"
1030+
"node*with?wildcards" || "node_with_wildcards"
1031+
"node|with|pipes" || "node_with_pipes"
1032+
"node<with>brackets" || "node_with_brackets"
1033+
"node\"with'quotes" || "node_with_quotes"
1034+
'node@with#special$chars%' || "node_with_special_chars_"
1035+
"/path/to/node-123" || "_path_to_node-123"
1036+
}
1037+
1038+
def "node name sanitization: should preserve safe alphanumeric and allowed characters"() {
1039+
given:
1040+
String safeName = "my-node_123.server-A"
1041+
String sanitized = safeName.replaceAll("[^a-zA-Z0-9._-]", "_")
1042+
1043+
expect:
1044+
sanitized == safeName // Should not be changed
1045+
}
1046+
1047+
def "node name sanitization: should handle empty and edge case node names"() {
1048+
expect:
1049+
nodeName.replaceAll("[^a-zA-Z0-9._-]", "_") == expected
1050+
1051+
where:
1052+
nodeName || expected
1053+
"" || ""
1054+
"123" || "123"
1055+
"..." || "..."
1056+
"---" || "---"
1057+
"___" || "___"
1058+
"a" || "a"
1059+
"/////" || "_____"
1060+
}
1061+
10051062
}

0 commit comments

Comments
 (0)