title: Apply Principle of Least Privilege
impact: MEDIUM
impactDescription: Reduced attack surface, better audit trail
tags: privileges, security, roles, permissions
遵循最小权限原则
只授予所需的最低权限。绝不要使用超级用户执行应用程序查询。
错误做法(权限过于宽泛):
-- 应用程序使用超级用户连接-- 或向应用程序角色授予 ALL 权限grantallprivilegesonalltablesinschemapublictoapp_user;grantallprivilegesonallsequencesinschemapublictoapp_user;-- 任何 SQL 注入都会造成灾难性后果-- drop table users; 会级联影响一切正确做法(最小化、精确的授权):
-- 创建没有任何默认权限的角色createrole app_readonly nologin;-- 只对特定表授予 SELECTgrantusageonschemapublictoapp_readonly;grantselectonpublic.products,public.categoriestoapp_readonly;-- 创建写入角色,限定范围createrole app_writer nologin;grantusageonschemapublictoapp_writer;grantselect,insert,updateonpublic.orderstoapp_writer;grantusageonsequence orders_id_seqtoapp_writer;-- 无 DELETE 权限-- 登录角色继承这些角色createrole app_user login password'xxx';grantapp_writertoapp_user;撤销 public 默认权限:
-- 撤销 public 的默认访问权限revokeallonschemapublicfrompublic;revokeallonalltablesinschemapublicfrompublic;参考:角色与权限