Repository: traefik/traefik
File: pkg/middlewares/customerrors/custom_errors.go (Go)

Code before:
	// check the recorder code against the configured http status code ranges
	code := catcher.getCode()

	originalCode := code

	// Check if we need to rewrite the status code
	for _, rsc := range c.statusRewrites {
		if rsc.fromCodes.Contains(code) {
			code = rsc.toCode
			break
		}
	}

	if code != originalCode {
		logger.Debug().Msgf("Caught HTTP Status Code %d (rewritten to %d), returning error page", originalCode, code)
	} else {
		logger.Debug().Msgf("Caught HTTP Status Code %d, returning error page", code)
	}

	var query string

	scheme := "http"
	if req.TLS != nil {
		scheme = "https"
	}
	originalURL := scheme + "://" + req.Host + req.URL.RequestURI()

	if len(c.backendQuery) > 0 {
		query = "/" + strings.TrimPrefix(c.backendQuery, "/")
		query = strings.ReplaceAll(query, "{status}", strconv.Itoa(code))
		query = strings.ReplaceAll(query, "{originalStatus}", strconv.Itoa(originalCode))
		query = strings.ReplaceAll(query, "{url}", url.QueryEscape(originalURL))
	}

	pageReq, err := newRequest("http://" + req.Host + query)
	if err != nil {
		logger.Error().Msgf("Unable to create error page request: %v", err)
		observability.SetStatusErrorf(req.Context(), "Unable to create error page request: %v", err)
		http.Error(rw, http.StatusText(code), code)
		return
	}

	utils.CopyHeaders(pageReq.Header, req.Header)
	c.backendHandler.ServeHTTP(newCodeModifier(rw, code),
		pageReq.WithContext(req.Context()))
}

func newRequest(baseURL string) (*http.Request, error) {
	u, err := url.Parse(baseURL)
	if err != nil {
		return nil, fmt.Errorf("error pages: error when parse URL: %w", err)

Review comment (suggestion):
`req.Host` is **not** the HTTP `Host:` requested by the originating request in this case, as clearly seen via [line 140](https://github.com/traefik/traefik/pull/11876/commits/36eecc25784eada7b96d96680bea8baa6778b714#diff-7f1f4cc79f1369ceb5228465b95bb882b8bbfde24d5ab6b34b77dca6e5ef759cR140). It is the host defined in the `service:` block of the yaml spec (config) for this middleware (ref: [this comment](https://github.com/traefik/traefik/issues/11829#issuecomment-2977393630))

Unfortunately, this isn't a quick-fix for an AI chatbot (I tried that)

Code after:
	code := catcher.getCode()

	originalCode := code

	// Check if we need to rewrite the status code
	for _, rsc := range c.statusRewrites {
		if rsc.fromCodes.Contains(code) {
			code = rsc.toCode
			break
		}
	}

	if code != originalCode {
		logger.Debug().Msgf("Caught HTTP Status Code %d (rewritten to %d), returning error page", originalCode, code)
	} else {
		logger.Debug().Msgf("Caught HTTP Status Code %d, returning error page", code)
	}

	var query string

	scheme := "http"
	if req.TLS != nil {
		scheme = "https"
	}
	orig := &url.URL{Scheme: scheme, Host: req.Host, Path: req.URL.Path, RawPath: req.URL.RawPath, RawQuery: req.URL.RawQuery, Fragment: req.URL.Fragment}

	if len(c.backendQuery) > 0 {
		query = "/" + strings.TrimPrefix(c.backendQuery, "/")
		query = strings.ReplaceAll(query, "{status}", strconv.Itoa(code))
		query = strings.ReplaceAll(query, "{originalStatus}", strconv.Itoa(originalCode))
		query = strings.ReplaceAll(query, "{url}", url.QueryEscape(orig.String()))
	}

	pageReq, err := newRequest("http://" + req.Host + query)
	if err != nil {
		logger.Error().Msgf("Unable to create error page request: %v", err)
		observability.SetStatusErrorf(req.Context(), "Unable to create error page request: %v", err)
		http.Error(rw, http.StatusText(code), code)
		return
	}

	utils.CopyHeaders(pageReq.Header, req.Header)
	c.backendHandler.ServeHTTP(newCodeModifier(rw, code),
		pageReq.WithContext(req.Context()))
}

func newRequest(baseURL string) (*http.Request, error) {
	u, err := url.Parse(baseURL)
	if err != nil {
		return nil, fmt.Errorf("error pages: error when parse URL: %w", err)
	}