Skip to content

Replacer's addHTTPVarsToReplacer should return false on unset dynamic placeholder keys #7373

@bluebox-steven

Description

@bluebox-steven

Issue Details

I wrote a simple string manipulation plugin to alter placeholders with lower/upper case by appending a .lower or .upper suffix. It works by adding a custom Replacer provider mapping function. Testing showed it worked fine with static placeholders, however, it wasn't triggering on dynamic placeholders such as http.request.header.*, http.request.uri.query.*, etc. After further debugging, the issue stems from the implementation of dynamic placeholders in the addHTTPVarsToReplacer function. The dynamic placeholders are replaced whether it finds a set key or not (returning empty string if not found). However, this means that custom providers further down the chain which may want to hook into a dynamic placeholder will never be called.

ie. http.request.header.x-custom-header.upper will always be stopped in the addHTTPVarsToReplacer provider chain and return empty even though it doesn't exist.

I propose that all Replacer logic handling dynamic placeholders return false if there isn't a key match, thereby allowing other providers to apply their own logic. ie.

			// request header fields
			if strings.HasPrefix(key, reqHeaderReplPrefix) {
				field := key[len(reqHeaderReplPrefix):]
				vals := req.Header[textproto.CanonicalMIMEHeaderKey(field)]
				// always return true, since the header field might
				// be present only in some requests
				return strings.Join(vals, ","), true
			}

becomes:

			// request header fields
			if strings.HasPrefix(key, reqHeaderReplPrefix) {
				field := key[len(reqHeaderReplPrefix):]
				vals := req.Header[textproto.CanonicalMIMEHeaderKey(field)]
				if len(vals) == 0 {
					return "", false
				}
				return strings.Join(vals, ","), true
			}

Based on my testing I see no adverse effects and resolves the chaining issue, but rather than adding a pull request I thought it's best to check what the feedback on a change like this would be, and whether it's something that would be accepted.

Thanks.

Assistance Disclosure

AI not used

If AI was used, describe the extent to which it was used.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions