SQL 注入和 Postgres“CREATE SCHEMA”

发布于 2024-11-06 12:00:29 字数 219 浏览 0 评论 0原文

在 postgres 中使用 CREATE SCHEMA schema_name 时,如果需要用户输入 schema_name 参数,如何防止 SQL 注入?

schema_name 不能是带引号的值,因此 ActiveRecord::Base.sanitize() 将不起作用。 (如果您使用导轨)。

When using CREATE SCHEMA schema_name in postgres, how do you prevent SQL injection of the schema_name parameter, if it needs to be user-input?

schema_name can't be a quoted value, so for example ActiveRecord::Base.sanitize() won't work. (if your using rails).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-11-13 12:00:29

允许用户创建模式(通常是执行 DDL)是相当不寻常的(甚至是可怕的)。您确定要这样做吗?

在任何情况下,您都可以做通常的事情:要么清理您的输入(我猜如果您允许用户创建模式,至少您可以限制允许的名称 - 例如仅字母数字等),和/或使用准备好的语句,绑定架构名称(这取决于您的语言/环境)。

It's rather unusual (even scary) to allow users to create schemas (in general to do DDL). Are you sure you want to do that?

In any case, you can do the usual things: either sanitize you input (and I guess that if you allow the user to create schames at least you can restrict the allowed names - eg only alphanumeric, etc), and/or use a prepared statement, binding the schema name (this depends on your language/environment).

若水般的淡然安静女子 2024-11-13 12:00:29

用户永远不应该拥有 DDL 权限,他们不需要它,而且它给了他们太多的权力。

您可以使用带有 quote_ident 的 SQL 函数来创建模式并避免 SQL 注入:

    CREATE OR REPLACE FUNCTION new_schema(IN _schemaname TEXT) 
    RETURNS bool 
    LANGUAGE plpgsql 
    AS
    $
    BEGIN;
      EXECUTE 'CREATE SCHEMA ' || quote_ident(_schemaname);

      RETURN true;
    END;
    $
    SECURITY DEFINER;

-- test:
SELECT new_schema('Frank; drop database template1');

Users should never have DDL permissions, they don't need it and it gives them too much power.

You could use a SQL function using quote_ident, to create a schema and avoid SQL injection:

    CREATE OR REPLACE FUNCTION new_schema(IN _schemaname TEXT) 
    RETURNS bool 
    LANGUAGE plpgsql 
    AS
    $
    BEGIN;
      EXECUTE 'CREATE SCHEMA ' || quote_ident(_schemaname);

      RETURN true;
    END;
    $
    SECURITY DEFINER;

-- test:
SELECT new_schema('Frank; drop database template1');
奢华的一滴泪 2024-11-13 12:00:29

我知道这可能不是您正在寻找的答案,但是如果您的设计让用户决定您的架构名称,但您不信任您的用户,那么您确实需要重新设计。让用户决定架构名称类似于要求用户决定类和函数名称。

如果你真的真的想这样做,我会使用正则表达式来限制为仅 16 个字母数字字符,以字母字符开头,在强制小写后 (/^[az][a-z0-9]{ 1,15}$/)。您还需要显式过滤掉“public”作为模式名称、information_schema(如果也允许使用下划线)和其他内置模式名称。

再次,我愿意打赌您真的不想这样做,并且有更好的方法来解决您的根本问题。

I know this probably isn't the answer you are looking for, but if your design has users deciding your schema names, but you don't trust your users, you really need to do a re-design. Having your users decide on a schema name is akin to asking users to decide on class and function names.

If you really, really want to do this, I would use a regular expression to limit to something like 16 alphanumeric characters only, starting with an alphabetic character, after being forced lowercase (/^[a-z][a-z0-9]{1,15}$/). You will also need to explicitly filter out "public" as a schema name, information_schema (if you allow underscores as well), and other built-in schema names.

Again, I'll be willing to bet that you really don't want to do this, and that there's a better way to solve your underlying problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文