CVE-2022-40604: Apache Airflow: Format String Vulnerability
A format string vulnerability in Apache Airflow's log file handler allowed an attacker to inject format specifiers into a URL path, causing sensitive configuration data to be exposed. An attacker could retrieve the secret key and database connection string, potentially forging identities and accessing the database.
- Fixed in 9 days
- Paid in 9 days
- Disclosed after 2.3 years
Summary by the program
##Description:
In Apache Airflow 2.3.0 through 2.3.4, part of a url was unnecessarily formatted, allowing for possible information extraction.
##Credit:
The Apache Airflow PMC would like to thank L3yx of Syclover Security Team for reporting this issue.
##References:
https://github.com/apache/airflow/pull/26337
The report
There is a Format String Vulnerability in src/airflow/utils/log/file_task_handler.py
url = os.path.join("http://{ti.hostname}:{worker_log_server_port}/log", log_relative_path).format( ti=ti, worker_log_server_port=conf.get('logging', 'WORKER_LOG_SERVER_PORT') )
In the above code, I can control some part of the log_relative_path, because log_relative_path is made up of run_id and other things.
Attack steps:
- Enter the DAGs menu, Choose any DAG, select
Trigger DAG w/ config. - Set the run_id to
{ti.task.__class__.__init__.__globals__[conf].__dict__}and trigger it. - Enter the
/xcom/list/page, click to enter the corresponding task page. - Click the
Logoption and capture the packet, you will get a request similar to the following:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=1&metadata=null
5. Modify try_number to a nonexistent value, such as 9999, such as:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=9999&metadata=null
6. Paste the modified url into the browser for access
Impact
Attacker can get a lot of sensitive information through this vulnerability, such as secret_key, database connection string, various configurations.
Can forge identity by the secret_key, can get the database password by database connection string, etc. Moreover, this vulnerability can be triggered by any DAG and exists in the production environment, so I think it's critical.
Context
Apache Airflow is a workflow orchestration platform. Users define DAGs (directed acyclic graphs) of tasks, trigger runs, and inspect the logs each task produces. The web interface exposes a log viewer that fetches task logs from the worker that ran the task, through an endpoint called /get_logs_with_metadata.
The component involved here is src/airflow/utils/log/file_task_handler.py, the handler that builds the URL used to retrieve a task's log from the worker's log server. The report covers Apache Airflow 2.3.0 through 2.3.4, and the weakness is classified as use of an externally-controlled format string.
Setup
The report does not describe any special account, role or tooling. The precondition is simply access to the Airflow web interface with the ability to trigger a DAG with a configuration, which is the normal workflow for anyone operating the instance. The vulnerable versions are 2.3.0 through 2.3.4.
Finding it
The researcher looked at how the log URL is assembled in file_task_handler.py and found this code:
url = os.path.join("http://{ti.hostname}:{worker_log_server_port}/log", log_relative_path).format(
ti=ti, worker_log_server_port=conf.get('logging', 'WORKER_LOG_SERVER_PORT')
)
The .format() call is applied to the whole joined string, not just to the template portion. That means any brace-delimited placeholder that ends up inside log_relative_path is also interpreted by Python's string formatter. The researcher notes that log_relative_path is made up of run_id and other things, so part of it is attacker-controlled: the run_id of a DAG run flows into the string that gets formatted.
That is the whole bug: user input is placed into a format string, so the formatter evaluates it as a template rather than treating it as data.
Exploitation
Step 1: Open the DAG trigger form
Enter the DAGs menu, choose any DAG, and select Trigger DAG w/ config. This gives control over the run configuration, including the run_id.
Step 2: Set a malicious run_id
Set the run_id to the following payload and trigger the run:
{ti.task.__class__.__init__.__globals__[conf].__dict__}
This is a Python format-string field. ti is the task instance object passed into .format(). From it the payload walks to ti.task, then to the task's class, then to the class's __init__ function, then to that function's __globals__ dictionary, and indexes it with conf. The result is the global conf object, and .__dict__ renders its attributes. Because the run_id becomes part of log_relative_path, the formatter resolves this expression when the log URL is built.
Step 3: Reach the task page
Enter the /xcom/list/ page and click through to the corresponding task page.
Step 4: Capture the log request
Click the Log option and capture the packet. The request looks similar to:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=1&metadata=null
The parameters identify the DAG (dag_id), the task (task_id), the map index, the execution date and the try number. The try_number is what selects which attempt's log file is fetched.
Step 5: Point try_number at a log that does not exist
Modify try_number to a value that does not exist, such as 9999:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=9999&metadata=null
The report does not explain why a non-existent attempt is needed; it simply changes try_number to 9999.
Step 6: Send the request
Paste the modified URL into the browser and access it.
Root cause
The mistake is calling .format() on a string that already contains user-controlled data. The template portion (http://{ti.hostname}:{worker_log_server_port}/log) needs formatting, but log_relative_path, which includes the run_id, is concatenated in before the call. Python's formatter then treats braces in the run_id as field expressions and evaluates them against the objects supplied as arguments (ti and the worker log server port).
Existing defences did not stop it because the value is not passed as a format argument; it is part of the format string itself, so nothing escapes or validates it as data.
Impact
The researcher states that an attacker can obtain a lot of sensitive information through this vulnerability, such as secret_key, the database connection string and various configurations. With the secret_key an attacker can forge identity, and with the database connection string they can get the database password. The researcher also notes that the vulnerability can be triggered by any DAG and exists in the production environment, which is why it is rated critical.
The Apache Airflow PMC credited L3yx of Syclover Security Team for reporting the issue.
Fix
The report references the upstream change at https://github.com/apache/airflow/pull/26337.
Variants to test
- Other fields that flow into
log_relative_pathbesidesrun_id. A positive result is any of them resolving a{...}expression when the log URL is built. - Other places in the codebase where
.format()is applied to a string that already contains request or user data. A positive result is a rendered attribute of an object reachable from the format arguments appearing in a response. - Different attribute chains from
ti, for example reaching configuration, environment or connection objects. A positive result is any sensitive value appearing in the log response.
Hunting notes
This class of bug hides where a template string and user input are concatenated before a .format() call, especially in code that builds URLs, paths or messages from identifiers such as run IDs, task IDs or file names. During recon, look for endpoints that echo back a value the user supplied at creation time, and for parameters like try_number that select a resource which may not exist, since error paths often rebuild strings from stored identifiers. The researcher states that the secret_key and database connection string can be obtained, and that any DAG can trigger it in production.
What happened, in plain words
Apache Airflow is a tool that runs scheduled jobs for you. You describe a job as a set of steps, and Airflow runs those steps, keeps a record of each attempt, and lets you read the output (the log) of every step in a web page. The bug in this report is that one of the pieces of text Airflow builds to fetch those logs was assembled in a way that let a user sneak a small instruction into it. When the page later asked for a log that did not exist, Airflow ran that instruction, which could expose its own private settings, such as the secret key and database connection string.
The weakness is classified as use of an externally-controlled format string. That sounds technical, but the idea is simple: some programming languages have a feature where a piece of text can contain blanks to be filled in, and the filling-in step can also do small calculations. If you let a stranger write part of that text, the stranger can put a calculation in it. That is what happened here.
The product and the feature
Airflow organises work as a DAG, short for directed acyclic graph. In everyday terms, a DAG is a recipe: a list of steps with arrows showing which step must finish before the next one starts. Each step is called a task. Every time you start a DAG, that is a run, and each run gets an identifier called a run_id. If a task fails and is retried, each attempt is a try, numbered from 1 upwards.
When a task runs, it writes a log file. The Airflow web interface has a log viewer that fetches that file from the machine that ran the task, through a web address ending in /get_logs_with_metadata. That address is an API endpoint: a URL that a web page calls behind the scenes to get data, rather than a page a person reads directly. Think of it as a service counter: you hand over a form with a few fields filled in, and you get back a document.
The file involved is src/airflow/utils/log/file_task_handler.py. It is the part of Airflow that works out the address of a task's log file on the worker machine. The affected versions are Apache Airflow 2.3.0 through 2.3.4.
Setup
The report describes no special account, role or tool. All you need is access to the Airflow web interface and the ability to start a DAG with a configuration, which is the ordinary way anyone uses the instance. The vulnerable versions are 2.3.0 through 2.3.4.
Finding it
The researcher read the code that builds the log address and found this:
url = os.path.join("http://{ti.hostname}:{worker_log_server_port}/log", log_relative_path).format(
ti=ti, worker_log_server_port=conf.get('logging', 'WORKER_LOG_SERVER_PORT')
)
Line by line:
os.path.join(...)glues two pieces of text together with the right separator for the operating system. The first piece is a template:http://{ti.hostname}:{worker_log_server_port}/log. The curly braces mark blanks to be filled in later. The second piece islog_relative_path, the path of the log file relative to the log folder..format(...)is Python's fill-in-the-blanks feature. It walks through the text, finds every{...}and replaces it with a value.ti=tisupplies the task instance object under the nameti, so{ti.hostname}becomes the hostname of the machine that ran the task.worker_log_server_port=conf.get('logging', 'WORKER_LOG_SERVER_PORT')supplies the port number the log server listens on, read from Airflow's configuration object, which is calledconf.
The important detail is that .format() is applied to the whole joined string, not just to the template part. So any curly braces that end up inside log_relative_path are also treated as blanks to fill in. The researcher notes that log_relative_path is built from run_id and other things, so part of it is under the attacker's control: the run_id of a run flows into the string that gets formatted.
That is the whole bug. User input is placed inside a format string, so the formatter reads it as a template instead of treating it as plain data.
Exploitation
Step 1: Open the DAG trigger form
Go to the DAGs menu, choose any DAG, and select Trigger DAG w/ config. This opens a form where you can supply a configuration for the run, including the run_id. The reason for starting here is that this form is the one place where an ordinary user can choose the run_id themselves.
Step 2: Set a malicious run_id
Set the run_id to the following payload and trigger the run:
{ti.task.__class__.__init__.__globals__[conf].__dict__}
This is a Python format-string field, meaning a blank to be filled in, written in the formatter's own mini-language. Read it as a chain of small steps, each one reaching a little further into the running program:
tiis the task instance object that was passed into.format().ti.taskis the task that instance belongs to..__class__is the blueprint that the task was made from, in other words its type..__init__is the function that runs when such an object is created..__globals__is the dictionary of global names that function can see, a kind of shared noticeboard for that part of the program.[conf]picks the entry namedconffrom that noticeboard, which is Airflow's global configuration object..__dict__lists all the settings stored on that object.
Because the run_id becomes part of log_relative_path, the formatter resolves this expression when it builds the log address. In plain terms, you have written a request for the program's own settings into a field the program later reads as instructions.
Step 3: Reach the task page
Go to the /xcom/list/ page and click through to the corresponding task page. XCom is Airflow's mechanism for passing small values between tasks; the list page shows those values, and from there you can open the task that produced them. This step is just navigation: you need to be on the task page so that its Log button is available.
Step 4: Capture the log request
Click the Log option and capture the packet. The request looks similar to:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=1&metadata=null
This is an HTTP request line: the address the browser asks for, followed by a question mark and a list of name=value pairs separated by &. Each pair is a parameter, a named field the server uses to decide what to return. Here:
dag_id=example_xcomnames the DAG.task_id=push_by_returningnames the task.map_index=-1is the position within a set of dynamically created task copies;-1means this task is not one of those.execution_date=2022-08-29T13%3A25%3A11%2B00%3A00identifies the run by its start time. The%3Aand%2Bare encoded characters:%3Ais a colon and%2Bis a plus sign, because a URL cannot contain those characters literally.try_number=1selects which attempt's log file to fetch.metadata=nullasks for the log without the extra metadata.
Capturing the packet means recording this request, for example with the browser's developer tools, so you can copy it and change one field by hand.
Step 5: Point try_number at a log that does not exist
Change try_number to a value that does not exist, such as 9999:
/get_logs_with_metadata?dag_id=example_xcom&task_id=push_by_returning&map_index=-1&execution_date=2022-08-29T13%3A25%3A11%2B00%3A00&try_number=9999&metadata=null
The report does not explain why a non-existent attempt is needed; it simply changes try_number to 9999. In other words, the missing log is the trigger: the program has to work out where that log would be, and that is the moment it fills in the blanks.
Step 6: Send the request
Paste the modified URL into the browser and open it. The report does not describe the response returned by the modified request.
Root cause
The mistake is calling .format() on a string that already contains user-controlled data. The template part, http://{ti.hostname}:{worker_log_server_port}/log, does need formatting, but log_relative_path, which includes the run_id, is joined in before the call. Python's formatter then treats braces in the run_id as field expressions and evaluates them against the objects supplied as arguments, namely ti and the worker log server port.
Existing defences did not stop it because the value is not passed as a format argument. It is part of the format string itself, so nothing escapes it or checks it as data.
Impact
The researcher states that an attacker can obtain a lot of sensitive information through this vulnerability, such as secret_key, the database connection string and various configurations. The secret_key is the value Airflow uses to sign session cookies, the small pieces of data that keep you logged in; with it, an attacker can forge identity, meaning they can create a cookie that the server accepts as a valid login for someone else. The database connection string is the text that says where the database is and which username and password to use; with it, an attacker can get the database password. The researcher also notes that the vulnerability can be triggered by any DAG and exists in the production environment, which is why it is rated critical.
The Apache Airflow PMC credited L3yx of Syclover Security Team for reporting the issue.
Fix
The report references the upstream change at https://github.com/apache/airflow/pull/26337.
Variants to test
- Other fields that flow into
log_relative_pathbesidesrun_id. A positive result is any of them resolving a{...}expression when the log address is built. - Other places in the codebase where
.format()is applied to a string that already contains request or user data. A positive result is a rendered attribute of an object reachable from the format arguments appearing in a response. - Different attribute chains from
ti, for example reaching configuration, environment or connection objects. A positive result is any sensitive value appearing in the log response.
Hunting notes
This class of bug hides where a template string and user input are joined before a .format() call, especially in code that builds URLs, paths or messages from identifiers such as run IDs, task IDs or file names. During recon, look for endpoints that echo back a value the user supplied at creation time, and for parameters like try_number that select a resource which may not exist, since error paths often rebuild strings from stored identifiers. The researcher states that the secret_key and database connection string can be obtained, and that any DAG can trigger it in production.
Glossary
- API: a URL that a web page or program calls behind the scenes to get or send data, rather than a page a person reads directly.
- Apache Airflow: a tool for scheduling and running workflows made of many steps.
- Configuration object (
conf): the in-memory object that holds Airflow's settings, such as the secret key and database address. - Cookie: a small piece of data a website stores in your browser so it can recognise you on later requests.
- Critical: the highest severity rating, used when a bug can expose highly sensitive data or take over a system.
- DAG: directed acyclic graph; in Airflow, a recipe of steps with arrows showing which must finish before the next.
- Database connection string: the text that says where a database is and which username and password to use.
- Endpoint: a specific address on a server that accepts requests, such as
/get_logs_with_metadata. - Execution date: the timestamp that identifies a particular run of a DAG.
- Format string: a piece of text containing blanks, written
{...}, that a program fills in with values. .format(): Python's feature for filling in those blanks, which can also evaluate small expressions written inside them.log_relative_path: the path of a task's log file relative to the log folder, built partly from the run_id.- Log: the recorded output a task writes while it runs.
- Map index: the position of a task within a set of dynamically created copies;
-1means the task is not one of those. - Parameter: a named field in a URL after the question mark, written
name=valueand separated by&. - Payload: the crafted input an attacker sends to trigger a bug.
- Production environment: the live system real users rely on, as opposed to a test setup.
- Run: one execution of a DAG.
run_id: the identifier given to a single run.- Secret key: the value Airflow uses to sign session cookies; whoever knows it can forge a valid login.
- Session: the server's record of you being logged in, usually tied to a cookie.
- Task: one step inside a DAG.
- Task instance (
ti): the object representing one task in one run, passed into the formatter here. - Try number: which attempt of a task to fetch the log for, counting from 1.
- URL encoding: writing characters that a URL cannot contain as
%followed by a code, such as%3Afor a colon. - Use of an externally-controlled format string: a weakness where user input ends up inside a format string, so the program evaluates it as a template instead of treating it as data.
- Worker: the machine that actually runs a task and writes its log.
- XCom: Airflow's mechanism for passing small values between tasks.