This is an automated email from the ASF dual-hosted git repository.

dabla pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new deebac0c085 Fail fast when common-io state store path has no url 
scheme (#77435)
deebac0c085 is described below

commit deebac0c085fe082f7d38c85ad384acf8fc8571d
Author: Amogh Desai <[email protected]>
AuthorDate: Tue Nov 24 12:47:35 2026 +0530

    Fail fast when common-io state store path has no url scheme (#77435)
    
    * Fail fast when common-io state store path has no url scheme
---
 providers/common/io/docs/changelog.rst              |  6 ++++++
 providers/common/io/provider.yaml                   |  1 +
 .../providers/common/io/get_provider_info.py        |  2 +-
 .../providers/common/io/state_store/backend.py      | 12 ++++++++++--
 .../unit/common/io/state_store/test_backend.py      | 21 +++++++++++++++++++++
 5 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/providers/common/io/docs/changelog.rst 
b/providers/common/io/docs/changelog.rst
index 1799e1ee642..f2ba6e05961 100644
--- a/providers/common/io/docs/changelog.rst
+++ b/providers/common/io/docs/changelog.rst
@@ -26,6 +26,12 @@ Changelog
 ---------
 
 .. note::
+    ``StateStoreObjectStorageBackend`` now raises ``ValueError`` when 
``[common.io] state_store_objectstorage_path``
+    has no URL scheme (for example ``/tmp/task-state``). Before, large values 
were written but reading them back
+    failed with ``JSONDecodeError``. Plain ``get``, ``set``, ``delete`` and 
``clear`` calls worked with such a
+    path and now raise too. Set the path to a URL such as 
``s3://conn_id@bucket/task-state``, or
+    ``file:///path/to/task-state`` for local storage.
+
     ``XComObjectStorageBackend`` now raises ``ValueError`` when it has to 
write a value to object storage
     and ``[common.io] xcom_objectstorage_path`` has no URL scheme (for example 
``""`` or ``/tmp/xcom``).
     Before, the write succeeded but reads returned the file path instead of 
the value. Set the path to a
diff --git a/providers/common/io/provider.yaml 
b/providers/common/io/provider.yaml
index d6294d423bf..77a8d190efa 100644
--- a/providers/common/io/provider.yaml
+++ b/providers/common/io/provider.yaml
@@ -128,6 +128,7 @@ config:
           When set, StateStoreObjectStorageBackend will persist task and asset 
state under this
           prefix, organized as 
<dag_id>/<run_id>/[region=<region_id>/]<task_id>/<region_index>/<key>
           for tasks and assets/<asset_identifier>/<key> for assets.
+          Must include a URL scheme; use ``file:///path/to/task-state`` for 
local storage.
         version_added: 1.8.0
         type: string
         example: "s3://conn_id@bucket/task-state/"
diff --git 
a/providers/common/io/src/airflow/providers/common/io/get_provider_info.py 
b/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
index 79cd8dc86ae..5685b02f03a 100644
--- a/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
+++ b/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
@@ -85,7 +85,7 @@ def get_provider_info():
                         "default": "",
                     },
                     "state_store_objectstorage_path": {
-                        "description": "Base path on object storage for the 
task/asset state store backend, in URL format.\nWhen set, 
StateStoreObjectStorageBackend will persist task and asset state under 
this\nprefix, organized as 
<dag_id>/<run_id>/[region=<region_id>/]<task_id>/<region_index>/<key>\nfor 
tasks and assets/<asset_identifier>/<key> for assets.\n",
+                        "description": "Base path on object storage for the 
task/asset state store backend, in URL format.\nWhen set, 
StateStoreObjectStorageBackend will persist task and asset state under 
this\nprefix, organized as 
<dag_id>/<run_id>/[region=<region_id>/]<task_id>/<region_index>/<key>\nfor 
tasks and assets/<asset_identifier>/<key> for assets.\nMust include a URL 
scheme; use ``file:///path/to/task-state`` for local storage.\n",
                         "version_added": "1.8.0",
                         "type": "string",
                         "example": "s3://conn_id@bucket/task-state/",
diff --git 
a/providers/common/io/src/airflow/providers/common/io/state_store/backend.py 
b/providers/common/io/src/airflow/providers/common/io/state_store/backend.py
index 7496ad1af2a..7bce63e03a2 100644
--- a/providers/common/io/src/airflow/providers/common/io/state_store/backend.py
+++ b/providers/common/io/src/airflow/providers/common/io/state_store/backend.py
@@ -47,7 +47,14 @@ _WRITE_CHUNK_CHARS = 1024 * 1024
 
 @cache
 def _get_base_path() -> ObjectStoragePath:
-    return ObjectStoragePath(conf.get_mandatory_value(SECTION, 
"state_store_objectstorage_path"))
+    path = conf.get_mandatory_value(SECTION, "state_store_objectstorage_path")
+    # _is_storage_ref only recognises URLs, so a schemeless ref would be 
parsed as JSON on read.
+    if not urlsplit(path).scheme:
+        raise ValueError(
+            f"[{SECTION}] state_store_objectstorage_path must be a URL with a 
scheme, such as "
+            f"s3://conn_id@bucket/task-state or file:///path/to/task-state; 
got {path!r}."
+        )
+    return ObjectStoragePath(path)
 
 
 @cache
@@ -119,10 +126,11 @@ def _read_from_object_storage(path: ObjectStoragePath) -> 
str | None:
 
 
 def _is_storage_ref(value: str) -> bool:
+    base_path = _get_base_path()
     try:
         if not urlsplit(value).scheme:
             return False
-        return ObjectStoragePath(value).is_relative_to(_get_base_path())
+        return ObjectStoragePath(value).is_relative_to(base_path)
     except Exception:
         return False
 
diff --git 
a/providers/common/io/tests/unit/common/io/state_store/test_backend.py 
b/providers/common/io/tests/unit/common/io/state_store/test_backend.py
index 976c2f3b2a3..fb05556ab4c 100644
--- a/providers/common/io/tests/unit/common/io/state_store/test_backend.py
+++ b/providers/common/io/tests/unit/common/io/state_store/test_backend.py
@@ -389,6 +389,27 @@ class TestStateStoreObjectStorageBackend:
             with pytest.raises(ValueError, match="must be non-negative"):
                 backend._get_threshold()
 
+    @pytest.mark.parametrize("path", ["", "relative/state", "/abs/state"])
+    def test_serialize_rejects_a_path_without_a_scheme(self, task_scope, path):
+        with conf_vars({("common.io", "state_store_objectstorage_path"): 
path}):
+            with pytest.raises(
+                ValueError, match="state_store_objectstorage_path must be a 
URL with a scheme"
+            ):
+                
StateStoreObjectStorageBackend().serialize_task_state_store_to_ref(
+                    value={"k": "v"}, key="key", scope=task_scope
+                )
+
+    @pytest.mark.parametrize(
+        "method", ["deserialize_task_state_store_from_ref", 
"deserialize_asset_state_store_from_ref"]
+    )
+    @pytest.mark.parametrize("path", ["", "/abs/state"])
+    def test_deserialize_rejects_a_path_without_a_scheme(self, base_path, 
method, path):
+        with conf_vars({("common.io", "state_store_objectstorage_path"): 
path}):
+            with pytest.raises(
+                ValueError, match="state_store_objectstorage_path must be a 
URL with a scheme"
+            ):
+                getattr(StateStoreObjectStorageBackend(), 
method)(f"{base_path}/dag/run/task/-1/key")
+
     @pytest.mark.asyncio
     async def test_aset_and_aget_task(self, store, task_scope):
         await store.aset(task_scope, "k", "hello")

Reply via email to