Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix: allow schemas with slash in the name (#77928)

Uladzimir Havenchyk3246bebfe400
diff --git a/e2e/test/scenarios/onboarding/home/browse-slash-schema.cy.spec.ts b/e2e/test/scenarios/onboarding/home/browse-slash-schema.cy.spec.ts
new file mode 100644
index 00000000000..b92a96d15e3
--- /dev/null
+++ b/e2e/test/scenarios/onboarding/home/browse-slash-schema.cy.spec.ts
@@ -0,0 +1,69 @@
+const { H } = cy;
+
+import { WRITABLE_DB_ID } from "e2e/support/cypress_data";
+
+const DB_NAME = "Writable Postgres12";
+const SLASH_SCHEMA = "public/transactions";
+const TABLE_NAME = "slash_schema_orders";
+const TABLE_DISPLAY_NAME = "Slash Schema Orders";
+const ANCHOR_TABLE_NAME = "plain_schema_anchor";
+
+const CLEANUP_SQL = `
+ DROP SCHEMA IF EXISTS "${SLASH_SCHEMA}" CASCADE;
+ DROP TABLE IF EXISTS public.${ANCHOR_TABLE_NAME};
+`;
+
+const SETUP_SQL = `
+ ${CLEANUP_SQL}
+ CREATE SCHEMA "${SLASH_SCHEMA}";
+ CREATE TABLE "${SLASH_SCHEMA}".${TABLE_NAME} (
+ id SERIAL PRIMARY KEY,
+ total INTEGER
+ );
+ INSERT INTO "${SLASH_SCHEMA}".${TABLE_NAME} (total) VALUES (10), (20);
+ CREATE TABLE public.${ANCHOR_TABLE_NAME} (id SERIAL PRIMARY KEY);
+`;
+
+describe(
+ "issue 77353 (schema names containing a slash)",
+ { tags: "@external" },
+ () => {
+ beforeEach(() => {
+ H.restore("postgres-writable");
+ cy.signInAsAdmin();
+ H.queryWritableDB(SETUP_SQL, "postgres");
+ H.resyncDatabase({
+ dbId: WRITABLE_DB_ID,
+ tables: [TABLE_NAME, ANCHOR_TABLE_NAME],
+ });
+ });
+
+ after(() => {
+ H.queryWritableDB(CLEANUP_SQL, "postgres");
+ });
+
+ it("should browse and query tables in a schema whose name contains a slash (metabase#77353)", () => {
+ cy.log("browse to the schema from the database page");
+ cy.visit(`/browse/databases/${WRITABLE_DB_ID}`);
+ cy.findByRole("heading", { name: SLASH_SCHEMA }).click();
+
+ cy.location("pathname").should(
+ "eq",
+ `/browse/databases/${WRITABLE_DB_ID}/schema/${encodeURIComponent(SLASH_SCHEMA)}`,
+ );
+ cy.findByRole("heading", { name: TABLE_DISPLAY_NAME }).should(
+ "be.visible",
+ );
+
+ cy.log("pick a table from the slashed schema in the data picker");
+ H.newButton("Question").click();
+ H.miniPicker().within(() => {
+ cy.findByText(DB_NAME).click();
+ cy.findByText(SLASH_SCHEMA).click();
+ cy.findByText(TABLE_DISPLAY_NAME).click();
+ });
+ H.visualize();
+ H.assertQueryBuilderRowCount(2);
+ });
+ },
+);
diff --git a/frontend/src/metabase/api/database.ts b/frontend/src/metabase/api/database.ts
index d294e8f67bb..c18e1474718 100644
--- a/frontend/src/metabase/api/database.ts
+++ b/frontend/src/metabase/api/database.ts
@@ -45,6 +45,14 @@ import {
} from "./tags";
import { handleQueryFulfilled } from "./utils/lifecycle";
+/**
+ * schema names containing slashes, backslashes, or percent signs are rejected
+ * at the HTTP layer when percent-encoded in a URL path, so they must be passed
+ * as a query parameter instead (#77353)
+ */
+export const shouldSchemaBePassedAsQueryParam = (schema: SchemaName) =>
+ /[/\\%]/.test(schema);
+
const toNormalizedSchemas = (dbId: DatabaseId, schemaNames: SchemaName[]) =>
schemaNames.map((schemaName) => ({
id: generateSchemaId(dbId, schemaName),
@@ -168,11 +176,17 @@ export const databaseApi = Api.injectEndpoints({
Table[],
ListDatabaseSchemaTablesRequest
>({
- query: ({ id, schema, ...params }) => ({
- method: "GET",
- url: `/api/database/${id}/schema/${encodeURIComponent(schema)}`,
- params,
- }),
+ query: ({ id, schema, ...params }) => {
+ const isQueryParam = shouldSchemaBePassedAsQueryParam(schema);
+
+ return {
+ method: "GET",
+ url: isQueryParam
+ ? `/api/database/${id}/schema/`
+ : `/api/database/${id}/schema/${encodeURIComponent(schema)}`,
+ params: isQueryParam ? { ...params, schema } : params,
+ };
+ },
providesTags: (tables = []) => [
listTag("table"),
...tables.map((table) => idTag("table", table.id)),
diff --git a/frontend/test/__support__/server-mocks/database.ts b/frontend/test/__support__/server-mocks/database.ts
index b10c5d7174a..07c54d5b012 100644
--- a/frontend/test/__support__/server-mocks/database.ts
+++ b/frontend/test/__support__/server-mocks/database.ts
@@ -1,6 +1,7 @@
import fetchMock from "fetch-mock";
import _ from "underscore";
+import { shouldSchemaBePassedAsQueryParam } from "metabase/api";
import { SAVED_QUESTIONS_DATABASE } from "metabase/databases/constants";
import { isTypePK } from "metabase-lib/v1/types/utils/isa";
import type { Database, DatabaseUsageInfo } from "metabase-types/api";
@@ -102,11 +103,15 @@ export const setupSchemaEndpoints = (db: Database) => {
fetchMock.get(`path:/api/database/${db.id}/syncable_schemas`, schemaNames);
schemaNames.forEach((schema) => {
- fetchMock.get(
- `path:/api/database/${db.id}/schema/${encodeURIComponent(schema)}`,
- schemas[schema],
- { name: `database-${db.id}-schema-${schema}` },
- );
+ const isQueryParam = shouldSchemaBePassedAsQueryParam(schema);
+ fetchMock.get({
+ url: isQueryParam
+ ? `path:/api/database/${db.id}/schema/`
+ : `path:/api/database/${db.id}/schema/${encodeURIComponent(schema)}`,
+ ...(isQueryParam && { query: { schema } }),
+ response: schemas[schema],
+ name: `database-${db.id}-schema-${schema}`,
+ });
});
};
diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.clj
index b79b3e79213..9465034d683 100644
--- a/src/metabase/warehouses_rest/api.clj
+++ b/src/metabase/warehouses_rest/api.clj
@@ -1559,6 +1559,10 @@
(api.macros/defendpoint :get "/:id/schema/:schema"
"Returns a list of Tables for the given Database `id` and `schema`.
+ Schema names containing slashes, backslashes, or percent signs are rejected at the HTTP layer when
+ percent-encoded in the URL path; pass those as the `schema` query parameter of `GET /:id/schema/`
+ instead (#77353).
+
Optional filters:
- `can-query=true` - filter to only tables the user can query
- `can-write-metadata=true` - filter to only tables the user can edit metadata for"
@@ -1589,26 +1593,32 @@
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-query-params-use-kebab-case
:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :get "/:id/schema/"
- "Return a list of Tables for a Database whose `schema` is `nil` or an empty string.
+ "Return a list of Tables for a Database whose schema is the `schema` query parameter, or `nil` or an
+ empty string when no `schema` is given. Unlike the `/:id/schema/:schema` route, the query parameter
+ supports schema names containing slashes, which are rejected at the HTTP layer when percent-encoded in
+ the URL path (#77353).
Optional filters:
- `can-query=true` - filter to only tables the user can query
- `can-write-metadata=true` - filter to only tables the user can edit metadata for"
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]
- {:keys [include_hidden include_editable_data_model can-query can-write-metadata include_measures]} :- [:map
- [:include_hidden {:default false} [:maybe ms/BooleanValue]]
- [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]
- [:can-query {:optional true} [:maybe :boolean]]
- [:can-write-metadata {:optional true} [:maybe :boolean]]
- [:include_measures {:optional true} [:maybe :boolean]]]]
+ {:keys [schema include_hidden include_editable_data_model can-query can-write-metadata include_measures]} :- [:map
+ [:schema {:optional true} [:maybe :string]]
+ [:include_hidden {:default false} [:maybe ms/BooleanValue]]
+ [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]
+ [:can-query {:optional true} [:maybe :boolean]]
+ [:can-write-metadata {:optional true} [:maybe :boolean]]
+ [:include_measures {:optional true} [:maybe :boolean]]]]
(let [opts {:include-hidden? include_hidden
:include-editable-data-model? include_editable_data_model
:can-query? can-query
:can-write-metadata? can-write-metadata
:include-measures? include_measures}]
- (api/check-404 (seq (concat (schema-tables-list id nil opts)
- (schema-tables-list id "" opts))))))
+ (api/check-404 (seq (if (str/blank? schema)
+ (concat (schema-tables-list id nil opts)
+ (schema-tables-list id "" opts))
+ (schema-tables-list id schema opts))))))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 47c8e6d6435..1d67ab52e15 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -2192,6 +2192,34 @@
(is (= ["t1" "t2"]
(map :name (mt/user-http-request :lucky :get 200 (format "database/%d/schema/" db-id)))))))))
+(deftest ^:parallel get-schema-tables-schema-query-param-test
+ (testing "GET /api/database/:id/schema/?schema=:schema"
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table _ {:db_id db-id :schema nil :name "t1"}
+ :model/Table _ {:db_id db-id :schema "" :name "t2"}
+ :model/Table _ {:db_id db-id :schema "public" :name "t3"}]
+ (let [url (format "database/%d/schema/" db-id)]
+ (testing "the schema can be passed as a query parameter"
+ (is (= ["t3"]
+ (map :name (mt/user-http-request :lucky :get 200 url :schema "public")))))
+ (testing "which, unlike the route param, supports schema names containing characters rejected in URL
+ paths at the HTTP layer: slashes (#77353), backslashes, and percent signs"
+ (doseq [schema-name ["public/transactions" "back\\slash" "per%cent"]]
+ (mt/with-temp [:model/Table _ {:db_id db-id :schema schema-name :name (str "table in " schema-name)}]
+ (is (= [(str "table in " schema-name)]
+ (map :name (mt/user-http-request :lucky :get 200 url :schema schema-name)))))))
+ (testing "an empty schema query param behaves like no schema param: `nil` and empty schemas"
+ (is (= ["t1" "t2"]
+ (map :name (mt/user-http-request :lucky :get 200 url :schema "")))))
+ (testing "should return a 404 for a schema that doesn't exist"
+ (is (= "Not found."
+ (mt/user-http-request :lucky :get 404 url :schema "no such schema"))))
+ (testing "the route-param form of such names is rejected at the HTTP layer, which is why the query
+ param exists; if this assertion fails, the query-param workaround may be obsolete (#77353)"
+ (is (= 400 (:status (mt/user-real-request-full-response
+ :lucky :get 400
+ (str url "public%2Ftransactions"))))))))))
+
(deftest ^:parallel get-schema-tables-publishing-test
(testing "GET /api/database/:id/schema/:schema"
(testing "should return is_published as true for published tables"