有没有办法防止 PostgreSQL 中用户的泛洪插入?有某种速率限制吗?

Is there a way to prevent flood insert from user in PosgreSQL? Is there some kind rate limit?

作为 SQL 注入预防的一部分,我已经撤销了使用该连接的用户的删除和更新权限。这样,即使错误代码允许 SQL 注入,攻击者也无法破坏数据的完整性。

现在只剩下INSERT了。例如。攻击者可以通过洪水插入特定的 table,创建一个脏数据库或使用洪水插入将其关闭,可能会关闭 HDD 和 PostgreSQL 所在的服务器 运行。该用户的所有 DDL 和 DCL 都已撤销。

所以,我的问题是:是否有可能防止泛滥插入、限制特定连接/会话/执行的速率、在上述期间每 5-10 秒尝试插入超过 1 行。

泛洪插入的意思是这样的:

insert into cars (id, name)
select id, name from cars

-- or if we imagine that id is auto-number.
insert into cars (name)
select name from cars

其中用户可以用一行简单的代码执行整个 table 的复制,每次下一次执行时将其加倍。

在多个地方,我已阅读作为保护授予 function/procedure 的权利,但要获得 INSERT/DELETE/UPDATE 的权利,但我不知道该怎么做,只要你撤销INSERT 上的权限,该过程将无法运行,因为它是由同一用户 运行。

所以我不太确定你是如何阻止 Script/Query 执行,但你授予 function/procedure 执行?

如果可以的话,有人能给我一个实际的例子吗?你会怎么做这样的事情?

“限制插入的可用 RAM”行中的答案不接受table。

谢谢,

您的评论之间存在一些矛盾的要求:

I need number of rows inserted in single statement limit

你的问题:

rate-limiting specific connection / session / execution attempting insert of more than 1 row per 5-10 seconds

“速率限制”没有外部工具无法完成,但“in single statement limit”部分可以通过语句级触发器实现。

该函数检查插入的行数:

create function limit_insert()
 returns trigger
as
$$
declare
  l_count bigint;
begin
  select count(*)
    into l_count
  from inserted;
  if l_count > 10 then
    raise 'Too many rows inserted!';
  end if;
  return null;
end;
$$
language plpgsql;

触发器定义为:

create trigger limit_insert_rows_trigger
  after insert on the_table
  referencing new table as inserted
  for each statement execute function limit_insert();

请注意,触发器 函数 可用于任何 table。

如何使用 user_id 或会话 ID、event_name 和 event_time

保留 table user_event?

select max(event_time) 从 user_event 其中 user_id = :user_id 和 event_name = 'insert_car';

if event_time <现在 错误“你做得太快了”

很明显这是伪代码