[JENKINS-76158][JENKINS-75977] Fix stale cache validation failures for newly created branches or tags #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: JENKINS-76158, JENKINS-75977
Problem Description
When a new branch or tag is created and pushed to a remote repository, then immediately used to trigger a downstream Jenkins build using the git-parameter-plugin, the build fails with:
This occurs when:
v1.2.3buildstep, API call, or other trigger) with parameterGIT_TAG=v1.2.3Root Cause Analysis
Background: SECURITY-3419
SECURITY-3419 added parameter validation to prevent arbitrary values from being injected. The validation uses a cached set of allowed values (
allowedValuesfield) that is populated by fetching tags/branches from the remote git repository.Stale cache problem
The original implementation only refreshed the cache when it was
null, but not when a value was not found in an existing cache.Cache clearing behavior observed:
createValue(CLICommand)explicitly setsallowedValues = nullto force cache refreshcreateValue(StaplerRequest)does not appear to clear the cacheUnderstanding the cache population for Stapler requests:
The original comment stated: "Cache does not need to be cleared when invoked through Stapler because generateParamList is called to fill the cache"
Based on my investigation, this appears to be misleading:
generateParamList()is called bygenerateContents()(which populates the cache)generateContents()is called bydoFillValueItems()- which appears to be the UI form rendering endpointdoFillValueItems()may not be calledcreateValue(StaplerRequest)does not appear to callgenerateParamList()to populate the cacheNote
I may be misunderstanding the flow here, so feedback is welcome!
The problem scenario:
[v1.2.2]v1.2.3GIT_TAG=v1.2.3createValue(StaplerRequest)is called → cache appears not to be clearedisValid()is called → cache is NOT null, so it's not refreshedallowedValues.contains("v1.2.3")returnsfalse→ validation failsThe Fix
Modified
isValid()to implement a two-tier validation approach that refreshes the cache when a value is not found.Key improvements:
Alternative considered:
Clearing the cache for Stapler requests (like CLI does with
allowedValues = null) was considered, but the cache refresh on miss approach was chosen to avoid unnecessary git fetches on every build. This way, cache hits remain fast while cache misses automatically refresh.Also updated the comment in
createValue(CLICommand)to clarify thatisValid()handles cache refresh when needed.Compatibility
Escape Hatch
Users experiencing issues can disable validation using the system property:
Testing done
Manual Testing with Local Jenkins Instance
Tested with
mvn hpi:runusing a realistic CI/CD scenario where one job creates a git tag and immediately triggers a downstream build.Test Repository: https://github.com/Mutix/jenkins-git-param-testing
This repository contains:
README.md- Complete step-by-step testing guideJobA-CreateTag.jenkinsfile- Pipeline that creates new tags and triggers JobBJobB-UseTag.jenkinsfile- Pipeline with git parameter that validates tagsFAILING_SCENARIO_OLD_CODE.md- Documentation of the bug with console outputSUCCESS_SCENARIO_WITH_FIX.md- Documentation of the fix working with console outputTest Scenario (Reproduces the Bug):
Results:
hudson.AbortException: Invalid parameter value: (StringParameterValue) GIT_TAG='test-tag-1762473037224'- cache is stale and doesn't contain the newly created tagFAILING_SCENARIO_OLD_CODE.mdfor complete console outputSUCCESS_SCENARIO_WITH_FIX.mdfor complete console outputmvn clean testTo reproduce the test:
README.mdAutomated Testing
Existing tests continue to pass:
mvn clean testNote on adding new tests:
Attempted to add unit tests using reflection to simulate a stale cache scenario, but discovered that the test infrastructure triggers the CLI code path (
createValue(CLICommand)), which already clears the cache before validation (line 183). The bug specifically affects the Stapler request flow (createValue(StaplerRequest2)) used by downstream builds and API triggers, which is more complex to test in isolation. Open to suggestions for testing the Stapler request flow if maintainers have preferred patterns for this.Submitter checklist