(source store.ExternalDataSource)
| 951 | } |
| 952 | |
| 953 | func (e *StrategyEngine) fetchSingleExternalSource(source store.ExternalDataSource) (interface{}, error) { |
| 954 | // SSRF Protection: Validate URL before making request |
| 955 | if err := security.ValidateURL(source.URL); err != nil { |
| 956 | return nil, fmt.Errorf("external source URL validation failed: %w", err) |
| 957 | } |
| 958 | |
| 959 | timeout := time.Duration(source.RefreshSecs) * time.Second |
| 960 | if timeout == 0 { |
| 961 | timeout = 30 * time.Second |
| 962 | } |
| 963 | |
| 964 | // Use SSRF-safe HTTP client |
| 965 | client := security.SafeHTTPClient(timeout) |
| 966 | |
| 967 | req, err := http.NewRequest(source.Method, source.URL, nil) |
| 968 | if err != nil { |
| 969 | return nil, err |
| 970 | } |
| 971 | |
| 972 | for k, v := range source.Headers { |
| 973 | req.Header.Set(k, v) |
| 974 | } |
| 975 | |
| 976 | resp, err := client.Do(req) |
| 977 | if err != nil { |
| 978 | return nil, err |
| 979 | } |
| 980 | defer resp.Body.Close() |
| 981 | |
| 982 | body, err := io.ReadAll(resp.Body) |
| 983 | if err != nil { |
| 984 | return nil, err |
| 985 | } |
| 986 | |
| 987 | var result interface{} |
| 988 | if err := json.Unmarshal(body, &result); err != nil { |
| 989 | return nil, err |
| 990 | } |
| 991 | |
| 992 | if source.DataPath != "" { |
| 993 | result = extractJSONPath(result, source.DataPath) |
| 994 | } |
| 995 | |
| 996 | return result, nil |
| 997 | } |
| 998 | |
| 999 | func extractJSONPath(data interface{}, path string) interface{} { |
| 1000 | parts := strings.Split(path, ".") |
no test coverage detected