Chapter 11 · Users, Roles, Authentication, Authorization, and Least Privilege
PROXY, DEFINER, SQL SECURITY, Stored Objects, and Privilege-Escalation Risks
Understand PROXY, DEFINER, SQL SECURITY, stored-object execution context, orphaned definers, and safe privilege-crossing patterns without creating hidden administrator backdoors.
Learning outcomes
Chapter 10 introduced definer and invoker execution. Here the question is explicitly security-focused: can a stored object become a controlled privilege boundary, and when can it accidentally become a privilege-escalation path? We will also place PROXY correctly: it is an authentication-plugin mapping capability, not a generic “assume another role” command.
Explain DEFINER and SQL SECURITY as an execution-context boundary for views and stored routines.
Build a least-privilege definer procedure that exposes one approved operation without granting direct table DML.
Identify unsafe broad definers and understand MySQL 8.4 controls around arbitrary or nonexistent definers.
Explain PROXY prerequisites and why the mandatory Community Server lab does not pretend default password authentication provides proxy mapping.
Audit stored-object definers, effective identity, roles, and negative authorization tests for escalation risk.
Execution context: invoker identity is not always privilege identity
USER() identifies the connected client identity; CURRENT_USER() identifies the account whose privileges are currently effective. For ordinary SQL they often align, but definer-context stored objects can deliberately make them differ.
Views and routines can specify SQL SECURITY DEFINER or SQL SECURITY INVOKER. Definer context can be useful: the application receives EXECUTE on a narrow operation while direct table privileges remain absent. The danger is equally clear: a definer with broad global privileges can unintentionally turn a routine into a privilege-escalation interface.
| Object | Security-context characteristic |
|---|---|
| View | Has DEFINER and SQL SECURITY DEFINER/INVOKER |
| Procedure/function | Has DEFINER and SQL SECURITY DEFINER/INVOKER |
| Trigger | Has a DEFINER; no SQL SECURITY clause |
| Event | Has a DEFINER; no SQL SECURITY clause |
Build a purpose-specific definer account
DROP USER IF EXISTS 'svc11_logic_owner'@'127.0.0.1';CREATE USER 'svc11_logic_owner'@'127.0.0.1' IDENTIFIED BY 'LabOnly-LogicOwner!2026' ACCOUNT LOCK;GRANT SELECT,UPDATEON servicehub_security_lab.work_ordersTO 'svc11_logic_owner'@'127.0.0.1';SHOW GRANTS FOR 'svc11_logic_owner'@'127.0.0.1';The owner account is deliberately narrow and locked against normal login. The goal is not to create a “hidden root”; it is to create an object owner that has exactly the underlying privileges needed by one stored operation.
Create a definer procedure as a narrow API
-- Run as an administrator that is permitted to specify this DEFINER.DROP PROCEDURE IF EXISTS servicehub_security_lab.close_work_order;DELIMITER //CREATE DEFINER='svc11_logic_owner'@'127.0.0.1'PROCEDURE servicehub_security_lab.close_work_order(IN p_work_order_id BIGINT UNSIGNED)SQL SECURITY DEFINERMODIFIES SQL DATABEGIN UPDATE servicehub_security_lab.work_orders SET status='CLOSED' WHERE work_order_id=p_work_order_id; IF ROW_COUNT()=0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='work order not found'; END IF;END//DELIMITER ;REVOKE UPDATEON servicehub_security_lab.work_ordersFROM 'r_servicehub_writer';GRANT EXECUTEON PROCEDURE servicehub_security_lab.close_work_orderTO 'r_servicehub_writer';SHOW CREATE PROCEDURE servicehub_security_lab.close_work_order;In a fresh application session, direct UPDATE should now be denied while CALL servicehub_security_lab.close_work_order(1001) succeeds. That is a deliberate privilege crossing: the caller gets one reviewed operation, not arbitrary table updates.
SELECT USER(),CURRENT_USER(),CURRENT_ROLE();-- Expected denial:UPDATE servicehub_security_lab.work_ordersSET private_note='should fail'WHERE work_order_id=1001;-- Expected success through the reviewed definer routine:CALL servicehub_security_lab.close_work_order(1001);SELECT work_order_id,statusFROM servicehub_security_lab.work_ordersWHERE work_order_id=1001;Wrong design: powerful or orphaned definers
Two dangerous patterns deserve separate names:
- Broad definer: a routine/view is owned by a global administrator even though it needs one table privilege. A bug in the object can expose much more authority than intended.
- Orphaned definer: the stored object names an account that does not exist. This complicates execution and ownership, and can create adoption/escalation concerns if a matching account later appears.
MySQL 8.4 includes specific dynamic privileges such as SET_ANY_DEFINER and ALLOW_NONEXISTENT_DEFINER around creation/alteration of objects with arbitrary or nonexistent definers. These are administrator capabilities, not permissions to give migration code casually.
SELECT 'VIEW' AS object_type,TABLE_NAME AS object_name,DEFINER,SECURITY_TYPEFROM INFORMATION_SCHEMA.VIEWSWHERE TABLE_SCHEMA='servicehub_security_lab'UNION ALLSELECT ROUTINE_TYPE,ROUTINE_NAME,DEFINER,SECURITY_TYPEFROM INFORMATION_SCHEMA.ROUTINESWHERE ROUTINE_SCHEMA='servicehub_security_lab'UNION ALLSELECT 'TRIGGER',TRIGGER_NAME,DEFINER,NULLFROM INFORMATION_SCHEMA.TRIGGERSWHERE TRIGGER_SCHEMA='servicehub_security_lab'UNION ALLSELECT 'EVENT',EVENT_NAME,DEFINER,NULLFROM INFORMATION_SCHEMA.EVENTSWHERE EVENT_SCHEMA='servicehub_security_lab';Where PROXY actually fits
The PROXY privilege is not the same as a role and is not a normal application impersonation command. Proxying depends on an authentication plugin or server-supported mapping that authenticates an external identity and maps it to a different MySQL account whose privileges become effective. The proxy account must also have PROXY on the proxied account.
Common production examples use external-authentication plugins such as PAM or LDAP; some of those are Enterprise Edition capabilities. The mandatory Community Server lab therefore does not fake a proxy login using caching_sha2_password. Instead, learn the metadata and security model locally, then reproduce actual proxy mapping only in an environment whose authentication plugin documents support.
SELECT User,Host,Proxied_user,Proxied_host,With_grantFROM mysql.proxies_privORDER BY User,Host,Proxied_user,Proxied_host;SHOW GRANTS FOR CURRENT_USER;PAM authentication is an Enterprise Edition plugin and supports proxy mapping. Do not make it a mandatory lab dependency. The portable lesson is the trust boundary: external authentication mapping + PROXY grant + proxied account privileges.
Production judgment
| Question | Safer default |
|---|---|
| Need to expose one database operation? | Consider EXECUTE on a narrow definer routine owned by a narrowly privileged account |
| Need caller-specific privilege behavior? | Use invoker context and test the caller’s active roles |
| Need to specify another definer? | Keep SET_ANY_DEFINER restricted to controlled deployment administration |
| Object owner being deleted? | Inventory/reassign objects before account removal; do not leave ownership ambiguous |
| Need external identity mapping? | Use a documented authentication plugin/proxy architecture; do not substitute roles or application-level “impersonation” |
Hands-on privilege-boundary test
- Create and lock the narrow logic-owner account.
- Create the definer procedure and replace direct application UPDATE with EXECUTE.
- From the application session, prove direct UPDATE is denied and the procedure succeeds.
- Inspect the routine definer/security type and the owner's grants.
- Inventory all definers in the lab schema and verify none point to a missing account.
- Inspect
mysql.proxies_privbut do not configure plugin-dependent PROXY mapping unless your environment explicitly supports it.
Knowledge check
- Why can a definer procedure improve least privilege?
- Why is a root-like definer risky?
- What is an orphaned definer?
- Is PROXY equivalent to SET ROLE?
- Why is PROXY not a mandatory Community lab here?
Reveal answers
- It can expose one reviewed operation through EXECUTE while withholding broad direct table privileges from the caller.
- Any defect or overly flexible parameter in the stored object may execute with far more privilege than the operation requires.
- A stored object whose DEFINER account no longer exists.
- No. PROXY is an authentication/proxied-user mapping mechanism; roles are privilege collections activated within an authenticated account session.
- Practical proxy mapping depends on authentication-plugin support; common documented examples such as PAM are Enterprise capabilities.
Summary and bridge to Lesson 5
You have now crossed privilege boundaries deliberately: roles compose privileges within an account; definer objects can expose narrowly elevated operations; PROXY belongs to authentication identity mapping. Lesson 5 turns the whole chapter into an auditable access model with actor/action matrices, fresh-session tests, rotation, revocation, and offboarding procedures.