Sub topic 11
Invoker-Rights Packages with AUTHID CURRENT_USER
This chapter assumes you can write a package specification and body, and that you know what a schema, a privilege, and a role are. It covers how to make a package run its SQL with the caller's identity instead of the owner's.
Definer rights: the default
By default, a PL/SQL package runs with the rights of its owner. This is called definer rights. Oracle resolves every table name in the package's SQL against the owner's schema. It also checks privileges as the owner, not as the caller.
This has a useful consequence. If HR owns emp_api and HR can read employees, you can grant EXECUTE on emp_api to a user who has no access to employees. That user can only reach the data through the package's code. Definer rights is the usual way to expose controlled access to tables.
The same behavior is a problem in other cases. Suppose each user has a private copy of a table with the same name. A definer-rights package always reads the owner's copy, never the caller's.
Invoker rights with AUTHID CURRENT_USER
Add AUTHID CURRENT_USER to the package specification to switch to invoker rights. The SQL in the package then runs with the privileges of the user who calls it, and unqualified table names resolve in that user's schema.
CREATE OR REPLACE PACKAGE emp_api AUTHID CURRENT_USER AS
FUNCTION get_salary(p_employee_id IN employees.employee_id%TYPE)
RETURN employees.salary%TYPE;
END emp_api;
/The clause goes after the package name and before AS or IS. The opposite clause is AUTHID DEFINER, which is also what you get when you write nothing.
Two points about placement:
- You set
AUTHIDonly on the specification. The package body cannot repeat it or change it. - The setting applies to the whole package. You cannot mix rights between subprograms in one package.
What changes at run time
The diagram shows how Oracle resolves a table name in the two modes.
Consider a worked case. HR owns emp_api with AUTHID CURRENT_USER. User SCOTT has his own employees table and has EXECUTE on HR.emp_api.
-- Connected as SCOTT
SELECT hr.emp_api.get_salary(100) FROM dual;The SELECT ... FROM employees inside the body resolves to SCOTT.EMPLOYEES, not HR.EMPLOYEES. If SCOTT has no such table and no synonym or grant that makes employees visible, the call fails at run time with ORA-00942: table or view does not exist.
Two details matter here:
- The package compiles in the owner's schema. The owner needs enough access to
employeesfor the compile to succeed, because Oracle checks static SQL at compile time. The name is then resolved again for each caller at run time. - Under definer rights, a privilege the owner receives through a role is not usable inside the package. Under invoker rights, roles enabled for the caller are used, with the exception described in the next section.
Which parts of the code use invoker rights
Within an invoker-rights package, these items resolve against the caller:
- Static SQL statements such as
SELECT,INSERT,UPDATEandDELETE. - Dynamic SQL run with
EXECUTE IMMEDIATEorDBMS_SQL. - Calls to other subprograms by an unqualified name, when that name is not found in the package itself.
These items still resolve against the owner:
- Names that the package declares itself, such as its own procedures, constants and types.
- Anything the code qualifies with an explicit schema name.
hr.employeesalways meansHR's table, though privilege checks still use the caller. - References in
%TYPEand%ROWTYPEdeclarations, which are resolved when the package is compiled.
That last point deserves care. The specification in this chapter uses employees.salary%TYPE as the return type. The compiler resolves that against the owner's employees table when it compiles the package. If a caller's employees table has a different column type, the declared type does not change.
Invoker rights and roles
Roles behave differently depending on the mode.
| Aspect | Definer rights | Invoker rights |
|---|---|---|
| Name resolution | Owner's schema | Caller's schema |
| Privileges used | Owner's direct grants | Caller's direct grants and enabled roles |
| Roles | Roles are disabled in the package | Caller's enabled roles apply |
| Typical use | Controlled access to owner data | Shared code that works on each caller's data |
Roles in invoker-rights code have one more rule. When an invoker-rights unit is called from a definer-rights unit, the invoker is the definer-rights unit's owner, and roles do not apply. Since Oracle Database 12c, an owner can grant INHERIT PRIVILEGES and INHERIT ANY PRIVILEGES to control which invoker-rights code may use the caller's privileges. This matters for security, which the next section covers.
Security consequences
Invoker rights moves trust to the caller, and it also exposes the caller. If a user with high privileges calls an invoker-rights package written by someone else, that package's SQL runs with the powerful user's privileges. A malicious owner could use that to escalate.
Oracle added a control for this in 12c. A user must grant INHERIT PRIVILEGES on their own account to another user before that user's invoker-rights code can use their privileges. By default, PUBLIC holds that grant on each user, so existing code keeps working.
-- A privileged user restricts who may use their privileges
REVOKE INHERIT PRIVILEGES ON USER app_admin FROM PUBLIC;
GRANT INHERIT PRIVILEGES ON USER app_admin TO hr;After this change, invoker-rights code owned by anyone except HR fails with ORA-06598: insufficient INHERIT PRIVILEGES privilege when APP_ADMIN calls it.
Choosing between the modes
Pick definer rights when the package is a gatekeeper. Callers get a narrow set of operations, and they should not need direct table access. Most business-logic packages fit this pattern.
Pick invoker rights when the package is a utility that should act on the caller's own objects. Typical cases are:
- Generic code that works against whichever schema the caller is in, for example an export or auditing helper.
- Multitenant designs where each tenant has its own schema with identical table names.
- Code that must respect the caller's own privileges, so a call cannot reach data that the caller could not read directly.
An invoker-rights package also has costs to plan for:
- Each caller needs direct access to every object the package touches. You must grant this to each user or a role, and the role must be enabled in the session.
- Errors show up at run time, not compile time, because name resolution depends on who calls.
- The same call can return different results for different users, which makes testing and debugging harder.
Invoker rights and package state
Package variables are held per session, so each user already has a separate copy of the state. AUTHID does not change this. The only effect is on how SQL and unqualified calls resolve. A package-level cursor, for example, is opened against the schema in effect for the caller who opens it.
Common mistakes
- Putting
AUTHIDin the package body. The compiler rejects this. Place it in the specification. - Expecting a definer-rights package to use roles. Grant the privilege directly to the owner instead.
- Granting
EXECUTEon an invoker-rights package and assuming that is enough. The caller also needs privileges on each underlying table. - Assuming
%TYPEfollows the caller. It is fixed when the package compiles. - Calling third-party invoker-rights code from an administrator account without restricting
INHERIT PRIVILEGES.
Exercise
User HR owns package reportapi, declared with AUTHID CURRENTUSER. It runs SELECT COUNT(*) FROM orders. User SCOTT has EXECUTE on HR.reportapi and owns a table SCOTT.orders. HR also owns HR.orders, and SCOTT has no privilege on it. Which table does the query read when SCOTT calls the package? What changes if you remove AUTHID CURRENTUSER?
Show answer
With AUTHID CURRENT_USER the unqualified name orders resolves in the caller's schema, so the query reads SCOTT.orders. Without the clause the package uses definer rights, so the name resolves in HR's schema and the query reads HR.orders, even though SCOTT has no direct privilege on it. SCOTT only needs EXECUTE on the package.
Exercise
A package is declared with AUTHID CURRENT_USER and a developer adds AUTHID DEFINER to the package body to make one procedure run with owner rights. Will this work? What is the correct way to get both behaviors?
Show answer
It will not work. AUTHID is allowed only in the specification and applies to the whole package. To get both behaviors, split the code into two packages, one declared with AUTHID DEFINER and one with AUTHID CURRENT_USER, and call between them as needed.