|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/projectdiscovery/nuclei/v3/pkg/types" |
| 10 | +) |
| 11 | + |
| 12 | +func TestProcessInlineSecretsFromProfile(t *testing.T) { |
| 13 | + t.Run("profile with inline secrets", func(t *testing.T) { |
| 14 | + profileContent := ` |
| 15 | +secrets: |
| 16 | + static: |
| 17 | + - type: header |
| 18 | + domains: |
| 19 | + - api.example.com |
| 20 | + headers: |
| 21 | + - key: x-api-key |
| 22 | + value: test-key-123 |
| 23 | +` |
| 24 | + tmpFile, err := os.CreateTemp(t.TempDir(), "test-profile-*.yaml") |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("could not create temp file: %v", err) |
| 27 | + } |
| 28 | + if _, err := tmpFile.WriteString(profileContent); err != nil { |
| 29 | + t.Fatalf("could not write profile: %v", err) |
| 30 | + } |
| 31 | + _ = tmpFile.Close() |
| 32 | + |
| 33 | + opts := &types.Options{} |
| 34 | + tempPath, err := processInlineSecretsFromProfile(tmpFile.Name(), opts) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("processInlineSecretsFromProfile failed: %v", err) |
| 37 | + } |
| 38 | + defer func() { |
| 39 | + _ = os.Remove(tempPath) |
| 40 | + }() |
| 41 | + |
| 42 | + if len(opts.SecretsFile) != 1 { |
| 43 | + t.Fatalf("expected 1 secrets file, got %d", len(opts.SecretsFile)) |
| 44 | + } |
| 45 | + |
| 46 | + secretsPath := opts.SecretsFile[0] |
| 47 | + if !strings.Contains(secretsPath, "inline-secrets-") { |
| 48 | + t.Errorf("secrets file path should contain 'inline-secrets-', got %s", secretsPath) |
| 49 | + } |
| 50 | + |
| 51 | + data, err := os.ReadFile(secretsPath) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("could not read generated secrets file: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + content := string(data) |
| 57 | + if !strings.Contains(content, "x-api-key") { |
| 58 | + t.Errorf("secrets file should contain header key, got:\n%s", content) |
| 59 | + } |
| 60 | + if !strings.Contains(content, "test-key-123") { |
| 61 | + t.Errorf("secrets file should contain header value, got:\n%s", content) |
| 62 | + } |
| 63 | + if !strings.Contains(content, "api.example.com") { |
| 64 | + t.Errorf("secrets file should contain domain, got:\n%s", content) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("profile without secrets", func(t *testing.T) { |
| 69 | + profileContent := ` |
| 70 | +severity: |
| 71 | + - critical |
| 72 | + - high |
| 73 | +` |
| 74 | + tmpFile, err := os.CreateTemp(t.TempDir(), "test-profile-*.yaml") |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("could not create temp file: %v", err) |
| 77 | + } |
| 78 | + if _, err := tmpFile.WriteString(profileContent); err != nil { |
| 79 | + t.Fatalf("could not write profile: %v", err) |
| 80 | + } |
| 81 | + if err := tmpFile.Close(); err != nil { |
| 82 | + t.Fatalf("could not close temp file: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + opts := &types.Options{} |
| 86 | + tempPath, err := processInlineSecretsFromProfile(tmpFile.Name(), opts) |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("processInlineSecretsFromProfile should not fail: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + if tempPath != "" { |
| 92 | + t.Errorf("expected empty temp path for profile without secrets, got %s", tempPath) |
| 93 | + } |
| 94 | + |
| 95 | + if len(opts.SecretsFile) != 0 { |
| 96 | + t.Errorf("expected 0 secrets files for profile without secrets, got %d", len(opts.SecretsFile)) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("nonexistent profile", func(t *testing.T) { |
| 101 | + opts := &types.Options{} |
| 102 | + _, err := processInlineSecretsFromProfile(filepath.Join(t.TempDir(), "nonexistent.yaml"), opts) |
| 103 | + if err == nil { |
| 104 | + t.Error("expected error for nonexistent file") |
| 105 | + } |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func TestInlineTargetsParsing(t *testing.T) { |
| 110 | + t.Run("multiline list key treated as inline targets", func(t *testing.T) { |
| 111 | + opts := &types.Options{ |
| 112 | + TargetsFilePath: "example.com\ntest.com\nscanme.sh\n", |
| 113 | + } |
| 114 | + |
| 115 | + if strings.Contains(opts.TargetsFilePath, "\n") { |
| 116 | + inlineTargets := strings.Split(strings.TrimSpace(opts.TargetsFilePath), "\n") |
| 117 | + for _, target := range inlineTargets { |
| 118 | + target = strings.TrimSpace(target) |
| 119 | + if target != "" && !strings.HasPrefix(target, "#") { |
| 120 | + opts.Targets = append(opts.Targets, target) |
| 121 | + } |
| 122 | + } |
| 123 | + opts.TargetsFilePath = "" |
| 124 | + } |
| 125 | + |
| 126 | + if len(opts.Targets) != 3 { |
| 127 | + t.Fatalf("expected 3 targets, got %d: %v", len(opts.Targets), opts.Targets) |
| 128 | + } |
| 129 | + if opts.Targets[0] != "example.com" { |
| 130 | + t.Errorf("expected first target 'example.com', got '%s'", opts.Targets[0]) |
| 131 | + } |
| 132 | + if opts.Targets[1] != "test.com" { |
| 133 | + t.Errorf("expected second target 'test.com', got '%s'", opts.Targets[1]) |
| 134 | + } |
| 135 | + if opts.Targets[2] != "scanme.sh" { |
| 136 | + t.Errorf("expected third target 'scanme.sh', got '%s'", opts.Targets[2]) |
| 137 | + } |
| 138 | + if opts.TargetsFilePath != "" { |
| 139 | + t.Errorf("TargetsFilePath should be cleared, got '%s'", opts.TargetsFilePath) |
| 140 | + } |
| 141 | + }) |
| 142 | + |
| 143 | + t.Run("single line list key remains as file path", func(t *testing.T) { |
| 144 | + opts := &types.Options{ |
| 145 | + TargetsFilePath: "/path/to/targets.txt", |
| 146 | + } |
| 147 | + |
| 148 | + if strings.Contains(opts.TargetsFilePath, "\n") { |
| 149 | + t.Error("single-line path should not be treated as inline targets") |
| 150 | + } |
| 151 | + |
| 152 | + if opts.TargetsFilePath != "/path/to/targets.txt" { |
| 153 | + t.Errorf("file path should remain unchanged, got '%s'", opts.TargetsFilePath) |
| 154 | + } |
| 155 | + }) |
| 156 | + |
| 157 | + t.Run("inline targets with comments and blank lines", func(t *testing.T) { |
| 158 | + opts := &types.Options{ |
| 159 | + TargetsFilePath: "example.com\n# this is a comment\n\ntest.com\n", |
| 160 | + } |
| 161 | + |
| 162 | + if strings.Contains(opts.TargetsFilePath, "\n") { |
| 163 | + inlineTargets := strings.Split(strings.TrimSpace(opts.TargetsFilePath), "\n") |
| 164 | + for _, target := range inlineTargets { |
| 165 | + target = strings.TrimSpace(target) |
| 166 | + if target != "" && !strings.HasPrefix(target, "#") { |
| 167 | + opts.Targets = append(opts.Targets, target) |
| 168 | + } |
| 169 | + } |
| 170 | + opts.TargetsFilePath = "" |
| 171 | + } |
| 172 | + |
| 173 | + if len(opts.Targets) != 2 { |
| 174 | + t.Fatalf("expected 2 targets (comments/blanks filtered), got %d: %v", len(opts.Targets), opts.Targets) |
| 175 | + } |
| 176 | + }) |
| 177 | + |
| 178 | + t.Run("targets-inline key", func(t *testing.T) { |
| 179 | + opts := &types.Options{ |
| 180 | + InlineTargetsList: "host1.com\nhost2.com\nhost3.com", |
| 181 | + } |
| 182 | + |
| 183 | + if opts.InlineTargetsList != "" { |
| 184 | + inlineTargets := strings.Split(strings.TrimSpace(opts.InlineTargetsList), "\n") |
| 185 | + for _, target := range inlineTargets { |
| 186 | + target = strings.TrimSpace(target) |
| 187 | + if target != "" && !strings.HasPrefix(target, "#") { |
| 188 | + opts.Targets = append(opts.Targets, target) |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + if len(opts.Targets) != 3 { |
| 194 | + t.Fatalf("expected 3 targets from targets-inline, got %d", len(opts.Targets)) |
| 195 | + } |
| 196 | + }) |
| 197 | +} |
| 198 | + |
| 199 | +func TestInlineSecretsFileFormat(t *testing.T) { |
| 200 | + // Verify the generated secrets file has the correct YAML structure |
| 201 | + // that matches what authx.GetAuthDataFromYAML expects |
| 202 | + profileContent := ` |
| 203 | +secrets: |
| 204 | + static: |
| 205 | + - type: header |
| 206 | + domains: |
| 207 | + - api.example.com |
| 208 | + headers: |
| 209 | + - key: Authorization |
| 210 | + value: Bearer test-token |
| 211 | + dynamic: |
| 212 | + - template: oauth-flow.yaml |
| 213 | + variables: |
| 214 | + - key: username |
| 215 | + value: testuser |
| 216 | + type: cookie |
| 217 | + domains: |
| 218 | + - auth.example.com |
| 219 | +` |
| 220 | + tmpFile, err := os.CreateTemp(t.TempDir(), "test-secrets-*.yaml") |
| 221 | + if err != nil { |
| 222 | + t.Fatalf("could not create temp file: %v", err) |
| 223 | + } |
| 224 | + if _, err := tmpFile.WriteString(profileContent); err != nil { |
| 225 | + t.Fatalf("could not write profile: %v", err) |
| 226 | + } |
| 227 | + if err := tmpFile.Close(); err != nil { |
| 228 | + t.Fatalf("could not close temp file: %v", err) |
| 229 | + } |
| 230 | + |
| 231 | + opts := &types.Options{} |
| 232 | + tempPath, err := processInlineSecretsFromProfile(tmpFile.Name(), opts) |
| 233 | + if err != nil { |
| 234 | + t.Fatalf("processInlineSecretsFromProfile failed: %v", err) |
| 235 | + } |
| 236 | + defer func() { |
| 237 | + _ = os.Remove(tempPath) |
| 238 | + }() |
| 239 | + |
| 240 | + data, err := os.ReadFile(opts.SecretsFile[0]) |
| 241 | + if err != nil { |
| 242 | + t.Fatalf("could not read secrets file: %v", err) |
| 243 | + } |
| 244 | + |
| 245 | + content := string(data) |
| 246 | + // The secrets section should contain static and dynamic at root level, |
| 247 | + // matching the Authx struct yaml tags |
| 248 | + if !strings.Contains(content, "static:") { |
| 249 | + t.Errorf("secrets file should have 'static:' key for Authx compatibility, got:\n%s", content) |
| 250 | + } |
| 251 | + if !strings.Contains(content, "dynamic:") { |
| 252 | + t.Errorf("secrets file should have 'dynamic:' key for Authx compatibility, got:\n%s", content) |
| 253 | + } |
| 254 | +} |
0 commit comments