gpt4 book ai didi

postgresql - 在没有前缀的 pg_temp 模式中调用函数

转载 作者:行者123 更新时间:2023-11-29 12:03:54 25 4
gpt4 key购买 nike

在 postgres 中,pg_temp 模式默认位于搜索路径上。正如 Tom Lane 所解释的那样 here出于安全原因,pg_temp 架构中的函数在默认情况下无法在没有前缀的情况下调用。

但是他指出,为了在没有前缀的情况下调用 pg_temp 模式中的函数,您必须明确地将临时模式添加到搜索路径中。不幸的是,从 postgresql 9.4 开始,这似乎不再起作用了。

set search_path to pg_temp,public;
-- create function in the temp schema
create function test_fun() returns int as $$ select 1; $$ language sql;
-- results in "function test_fun() does not exist"
select test_fun();
-- works perfectly
select pg_temp.test_fun();

有没有办法在 pg_temp 模式中调用函数而不给它们加上前缀?

这对于开发新功能非常方便。

最佳答案

看起来 Tome Lane 在那个方面并不是 100% 精确——我检查了 9.2 到 9.5,在每一个中你都需要用 pg_temp 来限定函数。设置 search_path 是不够的。

参见 PostgreSQL commit aa27977fe21a7dfa4da4376ad66ae37cb8f0d0b5 :

Support explicit placement of the temporary-table schema within search_path. This is needed to allow a security-definer function to set a truly secure value of search_path. Without it, a malicious user can use temporary objects to execute code with the privileges of the security-definer function. Even pushing the temp schema to the back of the search path is not quite good enough, because a function or operator at the back of the path might still capture control from one nearer the front due to having a more exact datatype match. Hence, disable searching the temp schema altogether for functions and operators.

Security: CVE-2007-2138

请特别查看 FuncnameGetCandidates 中的更改:

@@ -549,12 +586,16 @@ FuncnameGetCandidates(List *names, int nargs)
}
else
{
- /* Consider only procs that are in the search path */
+ /*
+ * Consider only procs that are in the search path and are not
+ * in the temp namespace.
+ */
ListCell *nsp;

foreach(nsp, activeSearchPath)
{
- if (procform->pronamespace == lfirst_oid(nsp))
+ if (procform->pronamespace == lfirst_oid(nsp) &&
+ procform->pronamespace != myTempNamespace)
break;
pathpos++;
}

关于postgresql - 在没有前缀的 pg_temp 模式中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38329932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com