Skip to content

Commit 0634ce1

Browse files
[JENKINS-67456] add check that pattern is valid (#178)
* [JENKINS-67456] add check that pattern is valid * Update src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java Co-authored-by: Abhyudaya Sharma <[email protected]> * do not validate pattern in javascript javascript has different rules so it might falsely allow or deny a pattern Co-authored-by: Abhyudaya Sharma <[email protected]>
1 parent 4b1263a commit 0634ce1

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.util.logging.Level;
7575
import java.util.logging.Logger;
7676
import java.util.regex.Pattern;
77+
import java.util.regex.PatternSyntaxException;
7778

7879
import hudson.util.FormValidation;
7980
import jenkins.model.Jenkins;
@@ -940,6 +941,17 @@ public boolean showPermission(String type, Permission p) {
940941
}
941942
}
942943

944+
@RequirePOST
945+
@Restricted(NoExternalUse.class)
946+
public FormValidation doCheckPattern(@QueryParameter String value) {
947+
try {
948+
Pattern.compile(value);
949+
} catch (PatternSyntaxException pse) {
950+
return FormValidation.error(pse.getMessage());
951+
}
952+
return FormValidation.ok();
953+
}
954+
943955
@RequirePOST
944956
public FormValidation doCheckName(@QueryParameter String value) {
945957
final String v = value.substring(1,value.length()-1);

src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-project-roles.jelly

+3-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
<f:textbox type="text" id="${id}text" />
119119
</f:entry>
120120
<f:entry help="${rootURL}/plugin/role-strategy/help/help-pattern.html" title="${%Pattern}">
121-
<f:textbox type="text" id="${id}pattern" />
121+
<f:textbox type="text" id="${id}pattern" checkUrl="'${descriptorPath}/checkPattern?value='+escape(this.value)"/>
122122
</f:entry>
123123
<f:entry>
124124
<input type="button" value="${%Add}" id="${id}button"/>
@@ -148,6 +148,7 @@
148148
alert("Please enter a pattern");
149149
return;
150150
}
151+
151152
if(findElementsBySelector(table,"TR").find(function(n){return n.getAttribute("name")=='['+name+']';})!=null) {
152153
alert("Entry for '"+name+"' already exists");
153154
return;
@@ -258,7 +259,7 @@
258259
this.innerHTML = '<input type="text" name="[pattern]" value="' + this.childNodes[1].value + '" size="' + (this.childNodes[1].value.length+10) + '"/>';
259260
}
260261
else {
261-
this.innerHTML = this.childNodes[0].value.escapeHTML() + '<input type="hidden" name="[pattern]" value="' + this.childNodes[0].value + '"/>';
262+
this.innerHTML = '<a href="#" class="patternAnchor">&quot;' + this.childNodes[0].value.escapeHTML() + '&quot;</a><input type="hidden" name="[pattern]" value="' + this.childNodes[0].value + '"/>';
262263
}
263264
return false;
264265
}

src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-roles.jelly

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
<j:set var="projectGroups" value="${it.strategy.descriptor.getGroups(it.strategy.PROJECT)}"/>
4848
<j:set var="slaveGroups" value="${it.strategy.descriptor.getGroups(it.strategy.SLAVE)}"/>
4949

50+
<j:if test="${empty(descriptorPath)}">
51+
<j:set var="descriptorPath" value="${rootURL}/descriptor/${it.strategy.descriptor.clazz.name}"/>
52+
</j:if>
53+
5054
<d:taglib uri="local">
5155
<d:tag name="roleRow">
5256
<td class="start">

src/main/resources/com/michelin/cio/hudson/plugins/rolestrategy/RoleStrategyConfig/manage-slave-roles.jelly

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<f:textbox type="text" id="${id}text" />
122122
</f:entry>
123123
<f:entry help="${rootURL}/plugin/role-strategy/help/help-pattern.html" title="${%Pattern}">
124-
<f:textbox type="text" id="${id}pattern" />
124+
<f:textbox type="text" id="${id}pattern" checkUrl="'${descriptorPath}/checkPattern?value='+escape(this.value)"/>
125125
</f:entry>
126126
<f:entry>
127127
<input type="button" value="${%Add}" id="${id}button"/>
@@ -152,6 +152,7 @@
152152
alert("Please enter a pattern");
153153
return;
154154
}
155+
155156
if(findElementsBySelector(table,"TR").find(function(n){return n.getAttribute("name")=='['+name+']';})!=null) {
156157
alert("Entry for '"+name+"' already exists");
157158
return;

0 commit comments

Comments
 (0)