...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/authservice/command.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/authservice

     1  package authservice
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/google/shlex"
     7  )
     8  
     9  // Used to find the name of the command to be executed from a command string which
    10  // can be passed to bash. Ignores all arguments passed to the command and any parameters
    11  // defined in the command string
    12  func extractCommandName(command string) (string, error) {
    13  	words, err := shlex.Split(command)
    14  	if err != nil {
    15  		return "", err
    16  	}
    17  	for _, word := range words {
    18  		if isParameter(word) {
    19  			continue
    20  		}
    21  
    22  		return word, nil
    23  	}
    24  	return "", nil
    25  }
    26  
    27  // Reports whether word is a valid name for a parameter in bash
    28  // Valid names are composed of letters, numbers or an underscore, and must start
    29  // with a letter or an underscore
    30  func isParameter(word string) bool {
    31  	// Parameter must have a name and value separated by an '='
    32  	if !strings.Contains(word, "=") {
    33  		return false
    34  	}
    35  
    36  	// Verify name of parameter follows the rules
    37  	name := strings.SplitN(word, "=", 2)[0]
    38  	// All characters must be either letter, number or underscore
    39  	for _, char := range name {
    40  		if isNumber(char) || isLetter(char) || char == '_' {
    41  			continue
    42  		}
    43  		return false
    44  	}
    45  
    46  	// First character must be a letter or underscore
    47  	var firstRune rune
    48  	// Extract first char of parameter name
    49  	for _, char := range name {
    50  		firstRune = char
    51  		break
    52  	}
    53  	if isLetter(firstRune) || firstRune == '_' {
    54  		return true
    55  	}
    56  
    57  	return false
    58  }
    59  
    60  func isLetter(char rune) bool {
    61  	return ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z')
    62  }
    63  
    64  func isNumber(char rune) bool {
    65  	return ('0' <= char && char <= '9')
    66  }
    67  

View as plain text