Skip to content

Commit bd0d0cf

Browse files
committed
Explicitly forbid the EXECUTOR_NUMBER field
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
1 parent a6ecf27 commit bd0d0cf

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/main/java/hudson/plugins/perforce/PerforceSCM.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,11 @@ public FormValidation doValidatePerforceLogin(StaplerRequest req) {
20132013

20142014
/**
20152015
* Checks to see if the specified workspace is valid.
2016+
* The method also checks forbidden variables in the client name.
2017+
* (see <a href="https://wiki.jenkins-ci.org/display/JENKINS/Perforce+Plugin">
2018+
* Perforce Plugin Wiki page</a>)
2019+
* to get the clarification of forbidden variables.
2020+
* An improper usage of the variable may corrupt Perforce workspaces in project builds.
20162021
*/
20172022
public FormValidation doValidateP4Client(StaplerRequest req) {
20182023
String workspace = Util.fixEmptyAndTrim(req.getParameter("client"));
@@ -2025,6 +2030,14 @@ public FormValidation doValidateP4Client(StaplerRequest req) {
20252030
return FormValidation.error("Client name doesn't meet global pattern: "+getP4ClientPattern());
20262031
}
20272032

2033+
// Check forbidden variables
2034+
for (String variableName : P4CLIENT_FORBIDDEN_VARIABLES) {
2035+
if (MacroStringHelper.containsVariable(workspace, variableName)) {
2036+
return FormValidation.error(hudson.plugins.perforce.Messages.
2037+
PerforceSCM_doValidateP4Client_forbiddenVariableError(variableName));
2038+
}
2039+
}
2040+
20282041
// Then, check depot
20292042
Depot depot = getDepotFromRequest(req);
20302043
if (depot == null) {
@@ -2364,7 +2377,10 @@ public void onRenamed(Item item, String oldName, String newName) {
23642377
Pattern.compile("^\\s*([+-]?//\\S+?/\\S+)\\s+\"//\\S+?(/[^\"]+)\"\\s*$");
23652378
private static final Pattern QUOTED_DEPOT_AND_WORKSPACE =
23662379
Pattern.compile("^\\s*\"([+-]?//\\S+?/[^\"]+)\"\\s+//\\S+?(/\\S+)$\\s*");
2367-
2380+
private static final String[] P4CLIENT_FORBIDDEN_VARIABLES=
2381+
{"EXECUTOR_NUMBER"};
2382+
2383+
23682384
/**
23692385
* Parses the projectPath into a list of pairs of strings representing the depot and client
23702386
* paths. Even items are depot and odd items are client.

src/main/java/hudson/plugins/perforce/utils/MacroStringHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ public static boolean containsMacro(@CheckForNull String str) {
192192
return str != null && str.matches(".*\\$\\{.*\\}.*");
193193
}
194194

195+
/**
196+
* Check if the input string contains the specified variable reference.
197+
* @param str String to be checked
198+
* @param variableName Variable name
199+
* @return true if the string contains the specified variable
200+
*/
201+
public static boolean containsVariable(@CheckForNull String str, @Nonnull String variableName) {
202+
return str != null && str.matches(".*\\$\\{" + variableName + "\\}.*");
203+
}
204+
195205
/**
196206
* Substitute parameters and validate contents of the resulting string.
197207
* This is a keystone method of {@link MacroStringHelper}.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
PerforceToolInstallation.onLoaded=Checking p4 executable migration
1+
PerforceToolInstallation.onLoaded=Checking p4 executable migration
2+
3+
PerforceSCM.doValidateP4Client.forbiddenVariableError= \
4+
The {0} variable is forbidden for the client (see the plugin's Wiki page). \
5+
An improper usage of the variable may corrupt Perforce workspaces in your build. \
6+
Use the variable on your own risk.

0 commit comments

Comments
 (0)