From ed0d0db62bfd94d74a2ca83c6195826ff023cf01 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 20 Jul 2007 22:10:49 +0000 Subject: [PATCH] Implemented the form validation for valid CVS tags. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3755 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/scm/CVSSCM.java | 80 +++++++++++++++++-- .../hudson/scm/CVSSCM/TagAction/tagForm.jelly | 6 +- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/hudson/scm/CVSSCM.java b/core/src/main/java/hudson/scm/CVSSCM.java index e3ab9ad74e..650c28c4b0 100644 --- a/core/src/main/java/hudson/scm/CVSSCM.java +++ b/core/src/main/java/hudson/scm/CVSSCM.java @@ -1094,6 +1094,23 @@ public class CVSSCM extends SCM implements Serializable { return tagName.split(" "); } + /** + * Checks if the value is a valid CVS tag name. + */ + public synchronized void doCheckTag(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + new FormFieldValidator(req,rsp,false) { + protected void check() throws IOException, ServletException { + String tag = fixNull(request.getParameter("value")).trim(); + if(tag.length()==0) {// nothing entered yet + ok(); + return; + } + + error(isInvalidTag(tag)); + } + }.check(); + } + /** * Invoked to actually tag the workspace. */ @@ -1103,9 +1120,10 @@ public class CVSSCM extends SCM implements Serializable { Map tagSet = new HashMap(); - String name = req.getParameter("name"); - if(isInvalidTag(name)) { - sendError("No valid tag name given",req,rsp); + String name = fixNull(req.getParameter("name")).trim(); + String reason = isInvalidTag(name); + if(reason!=null) { + sendError(reason,req,rsp); return; } @@ -1121,9 +1139,10 @@ public class CVSSCM extends SCM implements Serializable { if(!upName.startsWith("upstream.")) continue; - String tag = req.getParameter(upName); - if(isInvalidTag(tag)) { - sendError("No valid tag name given for "+upName,req,rsp); + String tag = fixNull(req.getParameter(upName)).trim(); + reason = isInvalidTag(tag); + if(reason!=null) { + sendError("No valid tag name given for "+upName+" : "+reason,req,rsp); return; } @@ -1140,8 +1159,53 @@ public class CVSSCM extends SCM implements Serializable { doIndex(req,rsp); } - private boolean isInvalidTag(String name) { - return name==null || name.length()==0; + /** + * Checks if the given value is a valid CVS tag. + * + * If it's invalid, this method gives you the reason as string. + */ + private String isInvalidTag(String name) { + // source code from CVS rcs.c + //void + //RCS_check_tag (tag) + // const char *tag; + //{ + // char *invalid = "$,.:;@"; /* invalid RCS tag characters */ + // const char *cp; + // + // /* + // * The first character must be an alphabetic letter. The remaining + // * characters cannot be non-visible graphic characters, and must not be + // * in the set of "invalid" RCS identifier characters. + // */ + // if (isalpha ((unsigned char) *tag)) + // { + // for (cp = tag; *cp; cp++) + // { + // if (!isgraph ((unsigned char) *cp)) + // error (1, 0, "tag `%s' has non-visible graphic characters", + // tag); + // if (strchr (invalid, *cp)) + // error (1, 0, "tag `%s' must not contain the characters `%s'", + // tag, invalid); + // } + // } + // else + // error (1, 0, "tag `%s' must start with a letter", tag); + //} + if(name==null || name.length()==0) + return "Tag is empty"; + + char ch = name.charAt(0); + if(!(('A'<=ch && ch<='Z') || ('a'<=ch && ch<='z'))) + return "Tag needs to start with alphabet"; + + for( char invalid : "$,.:;@".toCharArray() ) { + if(name.indexOf(invalid)>=0) + return "Tag contains illegal '"+invalid+"' character"; + } + + return null; } /** diff --git a/core/src/main/resources/hudson/scm/CVSSCM/TagAction/tagForm.jelly b/core/src/main/resources/hudson/scm/CVSSCM/TagAction/tagForm.jelly index 85adce2b4d..8ea675fcdf 100644 --- a/core/src/main/resources/hudson/scm/CVSSCM/TagAction/tagForm.jelly +++ b/core/src/main/resources/hudson/scm/CVSSCM/TagAction/tagForm.jelly @@ -11,7 +11,8 @@ Choose the CVS tag name for this build: - + @@ -19,7 +20,8 @@ - +