MCPcopy Create free account
hub / github.com/NoFxAiOS/nofx / fetchSingleExternalSource

Method fetchSingleExternalSource

kernel/engine.go:953–997  ·  view source on GitHub ↗
(source store.ExternalDataSource)

Source from the content-addressed store, hash-verified

951}
952
953func (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
999func extractJSONPath(data interface{}, path string) interface{} {
1000 parts := strings.Split(path, ".")

Callers 1

FetchExternalDataMethod · 0.95

Calls 7

ValidateURLFunction · 0.92
SafeHTTPClientFunction · 0.92
extractJSONPathFunction · 0.85
SetMethod · 0.80
DoMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected