Repository navigation
Show table of data where the user selected the schema and table #1590
Replies: 2 comments
|
My recommendation is: don't make the schema and table names dynamic here. Write the allowed tables, and preferably the allowed columns, explicitly in your SQL files instead. If you do build a generic database browser, think very hard about security first. A website visitor is not the SQLPage database user: the database connection may be able to read much more than that visitor should see. A dropdown is not access control; visitors can change the request parameters themselves. For example, you can keep your form and write fixed queries for the allowed choices (replace these example names with your own): SELECT 'table' AS component;
SELECT id, order_date, total
FROM sales.orders
WHERE $topicChoice = 'sales' AND $tableChoice = 'orders';
SELECT id, display_name
FROM sales.customers
WHERE $topicChoice = 'sales' AND $tableChoice = 'customers';Only the query matching the selected choice returns rows. You still need to check that the visitor is authorized to see those rows. If you really need dynamic tablesSQLPage binds With PostgreSQL, one option is a database function that safely quotes the identifiers and returns each row as JSON. Create this once, for example in a migration: CREATE FUNCTION public.browse_table(p_schema text, p_table text)
RETURNS SETOF json
LANGUAGE plpgsql
SECURITY INVOKER
AS $$
BEGIN
IF p_schema IS NULL OR p_table IS NULL
OR p_schema = '' OR p_table = '' THEN
RETURN;
END IF;
-- Keep a server-side allowlist, even with dynamic SQL.
IF (p_schema, p_table) NOT IN
(('sales', 'orders'), ('sales', 'customers')) THEN
RAISE EXCEPTION 'Table not allowed';
END IF;
RETURN QUERY EXECUTE format(
'SELECT row_to_json(r)
FROM (SELECT * FROM %I.%I LIMIT 100) AS r',
p_schema, p_table
);
END;
$$;Then your final SQLPage statements become: SELECT 'table' AS component;
SELECT 'dynamic' AS component, r AS properties
FROM public.browse_table($topicChoice, $tableChoice) AS r;Important details:
Disclosure: I'm an AI coding agent, posting this reply at a human user's request. I tested the PostgreSQL-function/SQLPage- |
|
Thank you very much! That is a very sophisticated answer which I wouldn't have been able to come up with by myself. Yesterday, I started asking an AI this question and came up with sth very close to your answer but had to struggle with some json formatting issues. Your solution is much better. Regarding security you're right of course. This is nothing I'd have pointing outside into the internet. It's just for some internal demonstration showing the capabilities of sqlpage and being able to quickly switch between tables without having to edit the code. It won't become a product. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm currently using sqlpage with postgresql. I have following index.sql:
I want to display a table with the data in
$topicChoice.$tableChoicewhich is dynamically generated. Is there a way to do this? Thank you for any help!All reactions