Skip to content

Frontend: makeUrl incorrectly stringifies undefined query parameters, causing K8s API errors #7313

Description

@Kakumanu-Harshitha

Describe the bug

In the makeUrl utility function (frontend/src/lib/k8s/api/v2/makeUrl.ts and similarly in v1/formatUrl.ts), the query parameters object is passed directly to new URLSearchParams(query).toString().
If a query parameter is explicitly passed as undefined (e.g., { watch: '1', fieldSelector: undefined }), URLSearchParams converts the undefined value into the literal string "undefined". The resulting query string becomes ?watch=1&fieldSelector=undefined. When this request is sent to the Kubernetes API, it attempts to parse "undefined" as a valid field selector, resulting in API rejection or unexpected behavior.

Steps to Reproduce

  1. Make a request to the Kubernetes API using a client helper that conditionally passes undefined for optional parameters (like fieldSelector or labelSelector).
  2. Inspect the outgoing network request URL.
  3. Notice that the URL query string contains &fieldSelector=undefined instead of omitting the key entirely.
  4. Observe the Kubernetes API rejecting the request with an error about invalid selector syntax.

Expected behavior

The makeUrl function should filter out undefined or null values from the query object before converting them into a query string, effectively omitting the keys so the URL remains clean and valid.

Suggested Fix

In both frontend/src/lib/k8s/api/v2/makeUrl.ts (around line 39) and frontend/src/lib/k8s/api/v1/formatUrl.ts (around line 68), filter the query object before passing it to URLSearchParams:

  const cleanQuery = Object.fromEntries(
    Object.entries(query).filter(([, v]) => v !== undefined && v !== null)
  );
  const queryString = new URLSearchParams(cleanQuery).toString();

Metadata

Metadata

Labels

kind/bugCategorizes issue or PR as related to a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions