Sub topic 11
Granting EXECUTE and Choosing AUTHID
Prerequisites and the security model
This chapter assumes that you know basic SQL, anonymous PL/SQL blocks, Oracle data types, and how stored subprograms are created. You also need a schema with permission to create procedures and functions.
A stored procedure or function has two separate access questions:
- Who may call the subprogram?
- Whose privileges does the subprogram use while it runs?
The EXECUTE privilege answers the first question. The AUTHID clause answers the second.
Granting EXECUTE
The owner of a stored subprogram can grant another user permission to run it:
GRANT EXECUTE ON tax_rate_for TO app_user;This grant does not give APP_USER permission to read the source code or change the function. It only permits calls to the function. The owner can grant EXECUTE on a procedure, function, or package.
A user may also grant the privilege to another user if that user received the privilege with WITH GRANT OPTION, or if the user has suitable administrative authority.
The caller can use a schema-qualified name when necessary:
BEGIN
payroll.raise_salary(100, 5);
END;
/The caller must have EXECUTE on the PAYROLL package. If the subprogram accesses tables, its AUTHID setting determines which user's table privileges matter.
Definer's rights and invoker's rights
By default, a stored procedure or function uses definer's rights. You can state this explicitly with AUTHID DEFINER:
CREATE OR REPLACE FUNCTION payroll_total (
p_employee_id IN NUMBER
) RETURN NUMBER
AUTHID DEFINER
IS
v_total NUMBER;
BEGIN
SELECT salary + NVL(commission_pct, 0) * salary
INTO v_total
FROM employees
WHERE employee_id = p_employee_id;
RETURN v_total;
END payroll_total;
/Here, Oracle checks access to EMPLOYEES using the function owner's privileges. For definer's-rights code, privileges needed to access referenced objects generally must be granted directly to the owner. Privileges received only through roles do not apply during compilation or execution.
Invoker's rights use AUTHID CURRENT_USER:
CREATE OR REPLACE FUNCTION tax_rate_for (
p_country_code IN VARCHAR2
) RETURN NUMBER
AUTHID CURRENT_USER
IS
BEGIN
RETURN CASE p_country_code
WHEN 'DE' THEN 0.19
WHEN 'FR' THEN 0.20
ELSE 0
END;
END tax_rate_for;
/
GRANT EXECUTE ON tax_rate_for TO app_user;When APP_USER calls this function, Oracle treats APP_USER as the current user for privilege checks. Invoker's rights are useful for shared utility code that should operate within each caller's access, but they can make behavior differ between callers.
| Choice | Privileges used for protected operations | Common use |
|---|---|---|
| `AUTHID DEFINER` | Owner's privileges | Controlled API over protected tables |
| `AUTHID CURRENT_USER` | Calling user's privileges | Utilities that respect caller access |
Choosing safely
Prefer definer's rights when a package exposes a narrow, validated operation over private tables. Validate inputs and avoid dynamic SQL unless it is required. Prefer invoker's rights when callers must retain control over which objects and data they can access.
Exercise
A user has EXECUTE on a function declared with AUTHID CURRENT_USER but receives ORA-00942 when calling it. What should you check first?
Show answer
Check whether that user has the required direct or effective privilege on the table or view that the function accesses.