|
| 1 | +/* |
| 2 | +Copyright 2026 Red Hat |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package secret |
| 18 | + |
| 19 | +import ( |
| 20 | + . "github.com/onsi/gomega" // nolint:revive |
| 21 | + "regexp" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +const ErrMsg string = "password does not meet the requirements" |
| 26 | + |
| 27 | +var testRequirements []Rule = []Rule{ |
| 28 | + { |
| 29 | + description: "Must contain at least one digit", |
| 30 | + pattern: *regexp.MustCompile(`.*\d.*`), |
| 31 | + }, |
| 32 | + { |
| 33 | + description: "Must contain at least one lowercase letter", |
| 34 | + pattern: *regexp.MustCompile(`.*[a-z].*`), |
| 35 | + }, |
| 36 | + { |
| 37 | + description: "Must contain at least one uppercase letter", |
| 38 | + pattern: *regexp.MustCompile(`.*[A-Z].*`), |
| 39 | + }, |
| 40 | + { |
| 41 | + description: "Must be at least 8 characters long", |
| 42 | + pattern: *regexp.MustCompile(`^.{8,}$`), |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +var testRejects []Rule = []Rule{ |
| 47 | + { |
| 48 | + description: "Password contains shell expansion patterns ($variable, ${variable}, $(command), `command`)", |
| 49 | + pattern: *regexp.MustCompile(`\$[A-Za-z_][A-Za-z0-9_]*|\$\{[^}]*\}|\$\([^)]*\)|` + "`[^`]*`"), |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +func TestValidatePassword(t *testing.T) { |
| 54 | + // Save original values |
| 55 | + originalRequirements := requirements |
| 56 | + originalRejects := rejects |
| 57 | + |
| 58 | + // Validate testRequirements and testRejects rules |
| 59 | + requirements = testRequirements |
| 60 | + rejects = testRejects |
| 61 | + |
| 62 | + // Restore after test |
| 63 | + defer func() { |
| 64 | + requirements = originalRequirements |
| 65 | + rejects = originalRejects |
| 66 | + }() |
| 67 | + |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + password string |
| 71 | + wantErr bool |
| 72 | + errMsg string |
| 73 | + }{ |
| 74 | + // Valid password scenarios |
| 75 | + { |
| 76 | + name: "valid password with all requirements", |
| 77 | + password: "Password123", |
| 78 | + wantErr: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "valid password with minimum length", |
| 82 | + password: "Abcdef12", |
| 83 | + wantErr: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "valid password with longer length", |
| 87 | + password: "MySecurePassword123", |
| 88 | + wantErr: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "valid password with allowed special characters", |
| 92 | + password: "Password123!@+=._-", |
| 93 | + wantErr: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "valid password with semicolon", |
| 97 | + password: "Password123;allowed", |
| 98 | + wantErr: false, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "valid password with angle brackets", |
| 102 | + password: "Password123<valid>", |
| 103 | + wantErr: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "valid password with caret character", |
| 107 | + password: "Password123^valid", |
| 108 | + wantErr: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "valid password with percent character", |
| 112 | + password: "Password123%valid", |
| 113 | + wantErr: false, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "valid password with safe metacharacters", |
| 117 | + password: "Password123*?{}[]|&~#'\"", |
| 118 | + wantErr: false, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "valid password with isolated dollar and number", |
| 122 | + password: "Password123$123", |
| 123 | + wantErr: false, |
| 124 | + }, |
| 125 | + |
| 126 | + // Invalid password scenarios - shell expansion patterns |
| 127 | + { |
| 128 | + name: "password made by all numbers", |
| 129 | + password: "12345678", |
| 130 | + wantErr: true, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "empty password", |
| 134 | + password: "", |
| 135 | + wantErr: true, |
| 136 | + errMsg: "empty password not allowed", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "password without uppercase", |
| 140 | + password: "password123", |
| 141 | + wantErr: true, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "password without lowercase", |
| 145 | + password: "PASSWORD123", |
| 146 | + wantErr: true, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "password without digit", |
| 150 | + password: "PasswordABC", |
| 151 | + wantErr: true, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "password too short", |
| 155 | + password: "Pass12", |
| 156 | + wantErr: true, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "password with variable expansion", |
| 160 | + password: "Password123$HOME", |
| 161 | + wantErr: true, |
| 162 | + errMsg: ErrMsg, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "password with variable expansion underscore", |
| 166 | + password: "Password123$USER_NAME", |
| 167 | + wantErr: true, |
| 168 | + errMsg: ErrMsg, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "password with braced variable expansion", |
| 172 | + password: "Password123${HOME}", |
| 173 | + wantErr: true, |
| 174 | + errMsg: ErrMsg, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "password with command substitution", |
| 178 | + password: "Password123$(echo bad)", |
| 179 | + wantErr: true, |
| 180 | + errMsg: ErrMsg, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "password with backtick command substitution", |
| 184 | + password: "Password123`echo bad`", |
| 185 | + wantErr: true, |
| 186 | + errMsg: ErrMsg, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "password with complex variable expansion", |
| 190 | + password: "Password123${VARIABLE_NAME}", |
| 191 | + wantErr: true, |
| 192 | + errMsg: ErrMsg, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "shell expansion attack example", |
| 196 | + password: "c^sometext02%text%text02$someText&", |
| 197 | + wantErr: true, |
| 198 | + errMsg: ErrMsg, |
| 199 | + }, |
| 200 | + } |
| 201 | + for _, tt := range tests { |
| 202 | + t.Run(tt.name, func(t *testing.T) { |
| 203 | + g := NewWithT(t) |
| 204 | + |
| 205 | + err := ValidatePassword(tt.password) |
| 206 | + |
| 207 | + if tt.wantErr { |
| 208 | + g.Expect(err).To(HaveOccurred()) |
| 209 | + g.Expect(err.Error()).To(ContainSubstring(tt.errMsg)) |
| 210 | + } else { |
| 211 | + g.Expect(err).ToNot(HaveOccurred()) |
| 212 | + } |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments