Skip to content

Commit

Permalink
Fix headers with special characters in config
Browse files Browse the repository at this point in the history
This adjusts the regular expression for headers to allow all characters
in the value. It also adds the same validation to the header option and
merges headers from config and options.

(cherry picked from commit 8f38ce3)
  • Loading branch information
mdellweg authored and patchback[bot] committed Mar 10, 2025
1 parent c6955bf commit 9737247
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES/1139.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug when header values weren't allowed to contain other than alphanumeric characters.
Also combine the headers from setting and the commandline.
19 changes: 17 additions & 2 deletions pulp_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@
"verbose",
"plugins",
]
HEADER_REGEX = r"^[-a-zA-Z0-9_]+:.+$"


def headers_callback(
ctx: click.Context, param: click.Parameter, value: t.Iterable[str]
) -> t.Iterable[str]:
failed_headers = ", ".join((item for item in value if not re.match(HEADER_REGEX, item)))
if failed_headers:
raise click.BadParameter(f"format must be <header-name>:<value> \n{failed_headers}")

default_map = ctx.default_map or {}
headers: t.List[str] = default_map.get("headers", [])
headers.extend(value)
return headers


CONFIG_OPTIONS = [
click.option("--base-url", default="https://localhost", help=_("API base url")),
Expand All @@ -65,6 +80,7 @@
"Name and value are colon separated. "
"Can be specified multiple times."
),
callback=headers_callback,
),
click.option("--username", default=None, help=_("Username on pulp server")),
click.option("--password", default=None, help=_("Password on pulp server")),
Expand Down Expand Up @@ -149,8 +165,7 @@ def validate_config(config: t.Dict[str, t.Any], strict: bool = False) -> None:
if "headers" in config:
if not isinstance(config["headers"], list) or not all(
(
isinstance(header, str)
and re.match(r"^[-a-zA-Z0-9_]+\s*:\s*[-a-zA-Z0-9_]+$", header)
isinstance(header, str) and re.match(HEADER_REGEX, header)
for header in config["headers"]
)
):
Expand Down
4 changes: 3 additions & 1 deletion tests/scripts/test_debug_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
. "$(dirname "$(realpath "$0")")"/config.source

expect_succ pulp -v status

echo "${ERROUTPUT}" | grep -q "^status_read : get https\?://\w.*/api/v3/status/$"

expect_succ pulp -vv --header "test-header:Value with space" status
echo "${ERROUTPUT}" | grep -q "^ test-header: Value with space$"

0 comments on commit 9737247

Please sign in to comment.