package authservice import ( "strings" "github.com/google/shlex" ) // Used to find the name of the command to be executed from a command string which // can be passed to bash. Ignores all arguments passed to the command and any parameters // defined in the command string func extractCommandName(command string) (string, error) { words, err := shlex.Split(command) if err != nil { return "", err } for _, word := range words { if isParameter(word) { continue } return word, nil } return "", nil } // Reports whether word is a valid name for a parameter in bash // Valid names are composed of letters, numbers or an underscore, and must start // with a letter or an underscore func isParameter(word string) bool { // Parameter must have a name and value separated by an '=' if !strings.Contains(word, "=") { return false } // Verify name of parameter follows the rules name := strings.SplitN(word, "=", 2)[0] // All characters must be either letter, number or underscore for _, char := range name { if isNumber(char) || isLetter(char) || char == '_' { continue } return false } // First character must be a letter or underscore var firstRune rune // Extract first char of parameter name for _, char := range name { firstRune = char break } if isLetter(firstRune) || firstRune == '_' { return true } return false } func isLetter(char rune) bool { return ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') } func isNumber(char rune) bool { return ('0' <= char && char <= '9') }