Refuse names that are not names at the store boundary
DELETE /flows/%2e%2e reached FlowStore.delete_flow(".."), which removed
the data directory, and /flows/_lib removed every shared node. Every
path the flow and dashboard stores build from a flow, node, library or
dashboard name now goes through checked(), which raises InvalidName
(400) for anything _validate_name refuses. _validate_name uses
fullmatch, so a trailing newline is refused too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013GAeg4Jo6xjZzjwy9QDWMQ
This commit is contained in:
@@ -23,7 +23,7 @@ from collections.abc import Iterable
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fluksio.flow.schemas import FlowDef
|
||||
from fluksio.flow.schemas import FlowDef, _validate_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,6 +38,33 @@ def process():
|
||||
'''
|
||||
|
||||
|
||||
class InvalidName(ValueError):
|
||||
"""A name that cannot be a file or directory of the store."""
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
super().__init__(name)
|
||||
self.name = name
|
||||
|
||||
def __str__(self) -> str:
|
||||
return (
|
||||
f"'{self.name}' is not a valid name: use lowercase letters, digits "
|
||||
"and underscores, starting with a letter"
|
||||
)
|
||||
|
||||
|
||||
def checked(name: str) -> str:
|
||||
"""*name*, once it is certain to be one path segment inside the store.
|
||||
|
||||
Every path the stores build starts from a name the API was handed,
|
||||
percent-decoded on the way in, so without this `..` or `_lib` reached the
|
||||
filesystem as written: `delete_flow("..")` removed the data directory.
|
||||
"""
|
||||
try:
|
||||
return _validate_name(name)
|
||||
except ValueError:
|
||||
raise InvalidName(name) from None
|
||||
|
||||
|
||||
class FlowNotFound(KeyError):
|
||||
def __init__(self, name: str) -> None:
|
||||
super().__init__(name)
|
||||
@@ -206,8 +233,11 @@ class FlowStore:
|
||||
# Paths
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
# Every path below starts in one of these three, and each name in it has
|
||||
# been through `checked`.
|
||||
|
||||
def _flow_dir(self, name: str) -> Path:
|
||||
return self.root / name
|
||||
return self.root / checked(name)
|
||||
|
||||
def _flow_file(self, name: str) -> Path:
|
||||
return self._flow_dir(name) / "flow.json"
|
||||
@@ -216,19 +246,19 @@ class FlowStore:
|
||||
return self._flow_dir(name) / "flow.draft.json"
|
||||
|
||||
def _node_file(self, flow: str, node_id: str) -> Path:
|
||||
return self._flow_dir(flow) / "nodes" / f"{node_id}.py"
|
||||
return self._flow_dir(flow) / "nodes" / f"{checked(node_id)}.py"
|
||||
|
||||
def _draft_nodes_dir(self, flow: str) -> Path:
|
||||
return self._flow_dir(flow) / "nodes.draft"
|
||||
|
||||
def _draft_node_file(self, flow: str, node_id: str) -> Path:
|
||||
return self._draft_nodes_dir(flow) / f"{node_id}.py"
|
||||
return self._draft_nodes_dir(flow) / f"{checked(node_id)}.py"
|
||||
|
||||
def _runtime_file(self, name: str) -> Path:
|
||||
return self._flow_dir(name) / "runtime.json"
|
||||
|
||||
def _lib_file(self, name: str) -> Path:
|
||||
return self.root / LIB_DIR / f"{name}.py"
|
||||
return self.root / LIB_DIR / f"{checked(name)}.py"
|
||||
|
||||
def head(self) -> str:
|
||||
"""The commit the store is at, so a result can name the code it ran."""
|
||||
|
||||
Reference in New Issue
Block a user