Aug
22
2009

Mysql View DEFINER and SQL Security

A little back I tried to backup a database from my webhost and restored it on my localhost mysql. It had a few views. On accessing the views in phpmyadmin mysql gave the error :

#1449 – There is no ‘projects’@'localhost’ registered

#1449 – There is no ‘projects’@'localhost’ registered

So I opened the dump file and found that the error was being caused by a line which looks like this :

/*!50013 DEFINER=`projects`@`localhost` SQL SECURITY DEFINER */

This line appeared in every view definition. So the simple reason was that projects@localhost was the user which had created the view (on the server) and when importing the database on local system there was no user named ‘projects’ in mysql.

There appeared to be no way by which this line could be prevented from coming up in the dump file. So a possible solution was to erase it from the dump file.

On ubuntu like this :

sed ‘/^\/\*\!50013/d’ backup.sql > backup_without_50013.sql

in PHP I tried this :

$contents = file_get_contents('backups/backup.sql');
$contents = preg_replace('@\/\*\!50013 DEFINER.*?\*\/@' , '' , $contents);
file_put_contents('backups/backup.sql' , $contents);

The above solutions worked.
But I was still looking for a technique where I didnt have to modify the dump file.

The solution is to create a view with sql security invoker like this :

CREATE SQL SECURITY invoker VIEW my_view as select …….

The above statement allows this view to be viewed by any user who invokes the view.By default the SQL SECURITY is DEFINER which means the definer user can only view it.

Popularity: 14% [?]

2 Comments + Add Comment

  • veggen is right. Views with SQL SECURITY DEFINER is a mechanism to provide data to users that otherwise wouldn’t have the privileges to execute the SELECT query that defines the VIEW.

    In other words, the VIEW’s SELECT query (or anything within a FUNCTION or PROCEDURE) is executed with (typically) elevated privileges.

  • I don’t think you got this right. Using INVOKER security context means that the view/procedure/function will execute with the privileges of the invoker. On the other hand, using DEFINER security context, means the view will execute with definer’s privileges so, in this case, invoker’s privileges are irrelevant.

Leave a comment