Validate Variable key is a non-empty string in get() and set()#69606
Open
bramhanandlingala wants to merge 1 commit into
Open
Validate Variable key is a non-empty string in get() and set()#69606bramhanandlingala wants to merge 1 commit into
bramhanandlingala wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
While looking into #69595, I checked
Variable.set()andVariable.get()inairflow-core/src/airflow/models/variable.py, since they're two of the most commonly used public APIs in Airflow — called directly in DAG code all the time.Root cause:
Neither function validated the
keyargument before doing anything with it. InVariable.set(), the key flowed straight through to a database write. Thekeycolumn is declared NOT NULL with nonullable=True, soVariable.set(None, "x")reached the database before failing, surfacing a rawsqlalchemy.exc.IntegrityErrorwith a stack trace that doesn't say what actually went wrong.Variable.set("", "x")was worse — an empty string satisfies NOT NULL, so it silently succeeded and created a variable with an unusable empty-string key.Variable.get()had a milder version of the same gap.Variable.get(None)doesn't crash, but it queries the database for a variable literally namedNone, finds nothing, and raisesKeyError("Variable None does not exist.")— a message that reads like there's a real variable you're missing, not that the key itself was invalid.Fix:
Added the same check to the top of both methods, before any database or Task-SDK redirect logic runs:
This catches all three cases the issue describes —
None, empty string, and non-string values — with one consistent, descriptive error instead of a database exception or a misleadingKeyError.Didn't need to touch
setdefault()separately since it callsget()internally as its first line, so it inherits the same validation automatically. Added a test to confirm that rather than just assuming it.related: #69595