Bug #113969 Adding host restriction to the user account breaks the views that were created
Submitted: 12 Feb 2024 23:43 Modified: 14 Feb 2024 7:51
Reporter: sakkanan Packiaraj Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Security: Privileges Severity:S3 (Non-critical)
Version:8.0.23 OS:Any
Assigned to: CPU Architecture:Any
Tags: Definer, Host Limit, VIEW

[12 Feb 2024 23:43] sakkanan Packiaraj
Description:
Changing Host Limit value from '%' to restricted value like '10.10.%.%' results into `ERROR 1045 (28000): Access denied for user 'user'@'10.10.%.%' (using password: YES)` when selecting from views.

Also noticed below  warning message while changing host value 

---------------
14:52:48	RENAME USER 'inc30587'@'%' TO 'inc30587'@'10.10.%.%'	0 row(s) affected, 
1 warning(s): 4005 User 'inc30587'@'%' is referenced as a definer account in a view.	0.048 sec
----------------

How to repeat:
Here are the detailed steps

1. Login as admin or other user who has permissions to create database and users
2. create two databases 
     CREATE DATABASE IF NOT EXISTS test_inc30587;
     CREATE DATABASE IF NOT EXISTS test_inc30587_dw;
3. create user and grant permissions to DBs that are created in step 2. 
    CREATE USER 'inc30587'@'%' IDENTIFIED BY 'inc30587';
    GRANT ALTER, CREATE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EXECUTE, INDEX, INSERT, LOCK TABLES, SELECT, SHOW VIEW, UPDATE on test_inc30587.* TO 'inc30587'@'%';
    GRANT ALTER, CREATE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EXECUTE, INDEX, INSERT, LOCK TABLES, SELECT, SHOW VIEW, UPDATE on test_inc30587_dw.* TO 'inc30587'@'%';
     FLUSH PRIVILEGES;

4. Login as the user created in step 3, in this case user inc30587. This is very important as the DEFINER of the view depends on logged-in user (CurrentUser)
5. create tables, views and insert some test data (make sure you are logged in as test user)
    CREATE TABLE test_inc30587.Customer (
        CustomerId int NOT NULL AUTO_INCREMENT,
        CustomerName varchar(255),
        ContactName varchar(255),
        Address varchar(255),
        City varchar(255),
        PostalCode varchar(255),
        Country varchar(255),
        PRIMARY KEY (CustomerId)
    );
    CREATE OR REPLACE VIEW test_inc30587_dw.CustomerNameCountry as SELECT * FROM test_inc30587.Customer;

    INSERT INTO test_inc30587.Customer (CustomerName, ContactName, Address, City, PostalCode, Country)
    VALUES
    ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
    ('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
    ('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');

    SELECT * from test_inc30587.Customer;
    SELECT * from test_inc30587_dw.CustomerNameCountry;

6. Logout test user; in this case inc30587
7. login as admin or any other user who have permission to manage users
8. Add host restriction to the test-user 
    RENAME USER 'inc30587'@'%' TO 'inc30587'@'10.10.%.%';
    FLUSH PRIVILEGES;
   Note that IP restriction is based on my VPC. Change it to you VPC. At this step you can notice a warning message from DB server 
---------------
14:52:48	RENAME USER 'inc30587'@'%' TO 'inc30587'@'10.10.%.%'	0 row(s) affected, 
1 warning(s): 4005 User 'inc30587'@'%' is referenced as a definer account in a view.	0.048 sec
----------------
9. Login as test user (inc30587) from the allowed network or VPC
10. try selecting from view. Select from DB will work fine
   select * from test_inc30587_dw.CustomerNameCountry; 
above sql will result in `ERROR 1045 (28000): Access denied for user 'inc30587'@'10.10.%.%' (using password: YES)` error
[14 Feb 2024 7:51] MySQL Verification Team
Hello sakkanan Packiaraj,

Thank you for the report and feedback.
IMHO this is an expected and documented behavior, quoting from the manual "RENAME USER causes the privileges held by the old user to be those held by the new user. However, RENAME USER does not automatically drop or invalidate databases or objects within them that the old user created. This includes stored programs or views for which the DEFINER attribute names the old user. Attempts to access such objects may produce an error if they execute in definer security context. ". Please see https://dev.mysql.com/doc/refman/8.0/en/rename-user.html & https://dev.mysql.com/doc/refman/5.7/en/stored-objects-security.html

I would suggest you to follow "Risk-Minimization Guidelines" in https://dev.mysql.com/doc/refman/8.0/en/stored-objects-security.html

For now, marking this as duplicate of Bug #113937

regards,
Umesh