Repository: prometheus/prometheus
File: config/config.go (Go)

Code before:
	// Escaping scheme is based on the validation scheme if left blank.
	switch globalConfig.MetricNameEscapingScheme {
	case "":
		if globalConfig.MetricNameValidationScheme == model.LegacyValidation {
			globalConfig.MetricNameEscapingScheme = model.EscapeUnderscores
		} else {
			globalConfig.MetricNameEscapingScheme = model.AllowUTF8
		}
	case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
	default:
		return fmt.Errorf("unknown global name escaping method specified, must be one of '%s', '%s', '%s', or '%s', got %q", model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues, globalConfig.MetricNameEscapingScheme)
	}

	// Similarly, if ScrapeConfig escaping scheme is blank, infer it from the
	// ScrapeConfig validation scheme if that was set, or the Global validation
	// scheme if the ScrapeConfig validation scheme was also not set. This ensures
	// that local ScrapeConfigs that only specify Legacy validation do not inherit
	// the global AllowUTF8 escaping setting, which is an error.
	if c.MetricNameEscapingScheme == "" {
		if localValidationUnset {
			c.MetricNameEscapingScheme = globalConfig.MetricNameEscapingScheme
		} else {
			if c.MetricNameValidationScheme == model.LegacyValidation {
				c.MetricNameEscapingScheme = model.EscapeUnderscores
			} else {
				c.MetricNameEscapingScheme = model.AllowUTF8
			}
		}
	}

	switch c.MetricNameEscapingScheme {
	case model.AllowUTF8:
		if c.MetricNameValidationScheme != model.UTF8Validation {
			return errors.New("utf8 metric names requested but validation scheme is not set to UTF8")
		}
	case model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
	default:
		return fmt.Errorf("unknown scrape config name escaping method specified, must be one of '%s', '%s', '%s', or '%s', got %q", model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues, c.MetricNameEscapingScheme)
	}

	if c.ConvertClassicHistogramsToNHCB == nil {
		global := globalConfig.ConvertClassicHistogramsToNHCB
		c.ConvertClassicHistogramsToNHCB = &global
	}

	if c.AlwaysScrapeClassicHistograms == nil {
		global := globalConfig.AlwaysScrapeClassicHistograms
		c.AlwaysScrapeClassicHistograms = &global
	}

	return nil

Review comment (security):
```suggestion
    if localValidationUnset {
        c.MetricNameEscapingScheme = globalConfig.MetricNameEscapingScheme
    } else if c.MetricNameValidationScheme == model.LegacyValidation {
        c.MetricNameEscapingScheme = model.EscapeUnderscores
    } else {
        c.MetricNameEscapingScheme = model.AllowUTF8
    }
```

Code after:
	// Escaping scheme is based on the validation scheme if left blank.
	switch globalConfig.MetricNameEscapingScheme {
	case "":
		if globalConfig.MetricNameValidationScheme == model.LegacyValidation {
			globalConfig.MetricNameEscapingScheme = model.EscapeUnderscores
		} else {
			globalConfig.MetricNameEscapingScheme = model.AllowUTF8
		}
	case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
	default:
		return fmt.Errorf("unknown global name escaping method specified, must be one of '%s', '%s', '%s', or '%s', got %q", model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues, globalConfig.MetricNameEscapingScheme)
	}

	// Similarly, if ScrapeConfig escaping scheme is blank, infer it from the
	// ScrapeConfig validation scheme if that was set, or the Global validation
	// scheme if the ScrapeConfig validation scheme was also not set. This ensures
	// that local ScrapeConfigs that only specify Legacy validation do not inherit
	// the global AllowUTF8 escaping setting, which is an error.
	if c.MetricNameEscapingScheme == "" {
		//nolint:gocritic
		if localValidationUnset {
			c.MetricNameEscapingScheme = globalConfig.MetricNameEscapingScheme
		} else if c.MetricNameValidationScheme == model.LegacyValidation {
			c.MetricNameEscapingScheme = model.EscapeUnderscores
		} else {
			c.MetricNameEscapingScheme = model.AllowUTF8
		}
	}

	switch c.MetricNameEscapingScheme {
	case model.AllowUTF8:
		if c.MetricNameValidationScheme != model.UTF8Validation {
			return errors.New("utf8 metric names requested but validation scheme is not set to UTF8")
		}
	case model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
	default:
		return fmt.Errorf("unknown scrape config name escaping method specified, must be one of '%s', '%s', '%s', or '%s', got %q", model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues, c.MetricNameEscapingScheme)
	}

	if c.ConvertClassicHistogramsToNHCB == nil {
		global := globalConfig.ConvertClassicHistogramsToNHCB
		c.ConvertClassicHistogramsToNHCB = &global
	}

	if c.AlwaysScrapeClassicHistograms == nil {
		global := globalConfig.AlwaysScrapeClassicHistograms
		c.AlwaysScrapeClassicHistograms = &global
	}

	return nil
}