Skip to content

bug: branch names with URL-significant characters (#, /) are not encoded in _graphql_url #1209

Description

@iddocohen

Bug

InfrahubClient._graphql_url() interpolates the branch name directly into the URL path without percent-encoding it. Branch names containing URL-significant characters (#, /, …) therefore produce malformed URLs.

infrahub_sdk/client.py:

def _graphql_url(
    self,
    branch_name: str | None = None,
    at: str | Timestamp | None = None,
) -> str:
    url = f"{self.config.address}/graphql"
    if branch_name:
        url += f"/{branch_name}"          # <-- not URL-encoded

    url_params = {}
    if at:
        at = Timestamp(at)
        url_params["at"] = at.to_string()
        url += "?" + urlencode(url_params)   # <-- query params ARE encoded

    return url

The at query parameter is passed through urlencode, but the branch_name path segment is not.

Impact / symptoms

  • A branch named e.g. feature#123 builds https://<host>/graphql/feature#123. Everything after # is treated as a URL fragment, so the request path collapses to /graphql/feature → the wrong (or non-existent) branch is queried → URLNotFoundError / HTTP 404. When Infrahub tries to update that branch's commit, the operation fails.
  • A branch named e.g. feature/foo injects an extra path segment (/graphql/feature/foo), which likewise does not resolve.

Reported from the field (a customer hit both # and / cases and correctly suspected a general escaping problem).

Steps to reproduce

  1. Have (or create) an Infrahub branch whose name contains a #, e.g. feature#123.
  2. Use the SDK against that branch (any call that routes through _graphql_url, e.g. client.execute_graphql(branch_name="feature#123")).
  3. Observe the request goes to /graphql/feature (fragment dropped) and returns a 404 / URLNotFoundError instead of hitting the intended branch.

Expected

The branch name should be percent-encoded as a single path segment so any branch name Infrahub accepts also works through the SDK.

Suggested fix

from urllib.parse import quote
...
    if branch_name:
        url += f"/{quote(branch_name, safe='')}"

(Apply the same treatment to the other places branch names are placed into paths, if any.)

Environment

  • infrahub-sdk-python v1.22.0 (code path unchanged on main @ HEAD 5b8047c).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions