PHP 安全性和独特的命名约定

发布于 2024-11-18 16:21:11 字数 201 浏览 2 评论 0原文

像这样命名用户上传的图像(例如头像)有什么问题吗:

/user_id-username.jpg

示例:

/1-feont.jpg

所有图像都有唯一的名称(因为 user_id 是主键),但是安全性怎么样?有什么不好的影响吗?如果有,我应该采取哪些约定?

Is anything wrong with naming images uploaded by users (for example avatars) like that:

/user_id-username.jpg

Example:

/1-feont.jpg

All images has unique name (becouse user_id is primary key), but what about security? Has that any bad influence? If has, what conventions should I do instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半岛未凉 2024-11-25 16:21:11

确保在将用户名用作文件名的一部分之前对其进行了适当的清理。用户 ID 应该是系统生成的,这样就不会导致任何问题。

Make sure that the username is appropriately sanitized before using it as part of the filename. User id should be system generated so that should not cause any problems.

回心转意 2024-11-25 16:21:11

您给图像起的名称纯粹是约定俗成的。我不认为泄露用户的用户名是一个安全问题。 (如果是,那么您最好立即检查您的 CMS!)但是,如果您的网站不完全安全,黑客就可以利用 SQL 注入来访问您的用户数据。

但这种想法实在是太牵强了。您可以继续使用用户名。 :-)

IMO,只需将图像命名为 user-user_id.jpg (这里,“user”是一个普通字符串,后跟整数 - user_id)

The name that you give the images is purely conventional. I do not think it is a security issue for revealing the usernames of your users. (If it is, then you better check your CMS right away!) However, if your website is not completely secure a hacker can make use of SQL injection to access your user data.

But the idea is really far-fetched. You can go ahead with using usernames. :-)

IMO, just name the images as user-user_id.jpg (Here, "user" being a normal string followed by the integer - user_id)

橘味果▽酱 2024-11-25 16:21:11

一般来说,这没问题,但有些用户可能不喜欢显示他们的名字,并且如果您的站点不完全安全或者将来发现当前未知的 PHP 漏洞,作为主键的用户 ID 可能存在漏洞。

对于这类事情,我倾向于使用以下代码

$createName = date('YmdHis');
$fileType = '.jpg';
$imgName = $createName.$fileType;

,这应该给出一个类似 20110702155513.jpg 的字符串 - 这是图像命名的完整日期和时间,并且是唯一的。

如果你真的想安全,那么你可以编写一个回调函数,如果由于文件名不唯一而失败(通常是因为同一秒内有 2 个请求 - 不太可能但有可能),那么你可以使用回退在字符串中间添加用户 ID 或使用回退作为主要命名方法,例如

if($imgName == 'inuse'){
     $createName1 = date('Ym');
     $createName2 = date('dHis');
     $fileType = '.jpg';
     $imgName = $createName1.$userId.$createName2.$fileType;
}

这允许隐藏用户 ID,但完全唯一。

*编辑 - *另一个选择是使用现有格式并创建 MD5 哈希,代码类似于

$user_id = 'user_id';
$username = 'username';
$fileType = '.jpg';
$fileName = md5($user_id).'-'.md5($username).$fileType;

我希望这有帮助

Generally it is fine but some users may not like their name to be displayed and the user ID being a primary key could have vulnerability if your site isn't completely secure or if a PHP vulnerability is found in the future that is currently unknown.

For this sort of thing I tend to use the following code

$createName = date('YmdHis');
$fileType = '.jpg';
$imgName = $createName.$fileType;

This should give a string like 20110702155513.jpg - this is the full date and time the image was named and is unique.

If you really want to be safe then you can write a call back function that if there was a failure due to the file name not being unique (generally because there were 2 requests in the same second - unlikely but possible), then you can use a fall back adding the user ID in the middle of the string or use the fall back as your primary naming method, so for example

if($imgName == 'inuse'){
     $createName1 = date('Ym');
     $createName2 = date('dHis');
     $fileType = '.jpg';
     $imgName = $createName1.$userId.$createName2.$fileType;
}

This allows the user ID to be hidden but entirely unique.

*Edit - * another option is to use the existing format and create an MD5 hash, the code would be something like

$user_id = 'user_id';
$username = 'username';
$fileType = '.jpg';
$fileName = md5($user_id).'-'.md5($username).$fileType;

I hope this helps

半窗疏影 2024-11-25 16:21:11
  • 当用户同时上传时,按照 Ryan 的建议使用日期会失败。
  • 当用户可能更改用户名时,使用用户名会失败。 “失败”在这里有点困难,但是您必须移动文件,这是使用其他解决方案时不需要的东西
  • 如果 ID 是自动生成的自动增量,则使用 id 可以解决所有并发问题来自您数据库的 ID。与Kerrek SB相反,我不认为这会在文件系统上的唯一标识符和图像之间建立连接。我的意思是它是独一无二的,你可以使用它。

如果您经常公开使用该 ID,则使用该 ID 还可以让您的用户轻松查找/链接他们的图像。另一方面,如果您在 URL 中只有用户名 - 例如 /profile/$username,那么我也会在图像文件名中使用用户名 - 以保持一致。

关于安全性:如果您清理用户名,一切都很好。只需确保它是唯一的,这样人们就不能覆盖彼此的名字(这意味着您需要对数据库中的用户名使用与图像文件名中的用户名相同的卫生规则)。

如果您需要手动查找图像,则将用户名作为图像名称也可以“更轻松”地查找图像,但这种情况可能发生的频率不够高,对您来说没有价值。

总而言之,我会选择 ID。

  • Using the date as Ryan suggests fails when you have users that upload at the same time
  • Using the username fails when users may change their username. "Fail" is a bit hard here, but you have to move the files around which is something that's not needed when using another solution
  • Using the id solves all concurrency problems if the ID is an autogenerated auto-increment ID from your database. Opposed to Kerrek SB, I don't see a problem that this makes a connection between a unique identifier and an image on your file system. I mean it's unique and you can use it.

Using the ID also makes it easy for your users to find/link their image if you constantly use the ID publicly. If you on the other hand have only the user names in URLs - like /profile/$username, then I'd use the username in the image file name, too - to be consistent.

About security: If you sanitize the username, all is fine. Just make sure that it's unique, so people can't overwrite each other's names (which means that you need to use the same sanitation rules for usernames in database as you use for usernames in image file names).

Having the username as image name also makes it "easier" to find an image if you manually need to find one, but that probably happens not often enough to be of worth for you.

To summarize, I'd go with the ID.

吻安 2024-11-25 16:21:11

像您在第一个示例中使用的方式命名文件并没有巨大的风险,但是为了使其更安全,为什么不使用更好的东西,就像

    function Naming($username,$imagename)
{
    $uniq = time() ;
    $name  = $uniq.'-'.$username.'-'.$imagename ;
    return $name ;
}

我认为最好避免使用用户ID一样

there is no huge risk to naming files like the way you use in your first example but to make it more secure why don't you use something better like

    function Naming($username,$imagename)
{
    $uniq = time() ;
    $name  = $uniq.'-'.$username.'-'.$imagename ;
    return $name ;
}

i think its better to avoid using users ID's

何其悲哀 2024-11-25 16:21:11

唯一的 user_id 就足够了,不需要“用户名”。
问题不在于此处,而在于文件数(在一个文件夹中)。
在文件夹中将它们分割为 1000(或分割为 100):将 ID 字符串的第一个、第二个和第三个符号放入单独的目录中:

ID = 10524.jpg  
filename = 1/0/5/10524.jpg

如果很难编写算法,您可以尝试此功能:

function getStorePath($filename, $base_dir)
{
    //file should have extension
    $ext_pos = strrpos($filename, '.');
    if ($ext_pos===false) return false;

    //extension will be sanitized (filtered actually)    
    $ext = preg_replace("|\W+|", "", substr($filename, $ext_pos+1));
    if (empty($ext)) return false;
    if (in_array($ext, array('php', 'shtml', 'cgi', 'inc', 'module', 'sh', 'sql', 'class'))) return false;

    //filename will be filtered    
    $filename = preg_replace("|\W+|", "", substr($filename, 0, $ext_pos));

    if (empty($filename)) $filename = mt_rand(100000, 999999).round(microtime(true)*1000000);

    //let's create path to the file.
    //we will take first 3 symbols of filename as names of folders    
    $d = realpath($base_dir).'/';

    //first symbol
    $d .= $filename[0].'/';
    if (!file_exists($d))
    {
        $md = mkdir($d, 0755);
        if ($md===false && !file_exists($d)) return false;
    }

    //second symbol
    if (isset($filename[1]))
    {
        $d .= $filename[1].'/';
        if (!file_exists($d))
        {
            $md = mkdir($d, 0755);
            if ($md===false && !file_exists($d)) return false;
        }
    }

    //and third symbol
    if (isset($filename[2]))
    {
        $d .= $filename[2].'/';
        if (!file_exists($d))
        {
            $md = mkdir($d, 0755);
            if ($md===false && !file_exists($d)) return false;
        }
    }

    if (!file_exists($d.$filename.'.'.$ext)) return $d.$filename.'.'.$ext;
    else return false;
}

Unique user_id is enough, you don't need "username".
Problem isn't here, but in count of files (in one folder).
Split them by 1000 in folder (or by 100): take first, second and third symbols of ID string and put in separate dirs:

ID = 10524.jpg  
filename = 1/0/5/10524.jpg

If it's hard to write algorithm, you can try this function:

function getStorePath($filename, $base_dir)
{
    //file should have extension
    $ext_pos = strrpos($filename, '.');
    if ($ext_pos===false) return false;

    //extension will be sanitized (filtered actually)    
    $ext = preg_replace("|\W+|", "", substr($filename, $ext_pos+1));
    if (empty($ext)) return false;
    if (in_array($ext, array('php', 'shtml', 'cgi', 'inc', 'module', 'sh', 'sql', 'class'))) return false;

    //filename will be filtered    
    $filename = preg_replace("|\W+|", "", substr($filename, 0, $ext_pos));

    if (empty($filename)) $filename = mt_rand(100000, 999999).round(microtime(true)*1000000);

    //let's create path to the file.
    //we will take first 3 symbols of filename as names of folders    
    $d = realpath($base_dir).'/';

    //first symbol
    $d .= $filename[0].'/';
    if (!file_exists($d))
    {
        $md = mkdir($d, 0755);
        if ($md===false && !file_exists($d)) return false;
    }

    //second symbol
    if (isset($filename[1]))
    {
        $d .= $filename[1].'/';
        if (!file_exists($d))
        {
            $md = mkdir($d, 0755);
            if ($md===false && !file_exists($d)) return false;
        }
    }

    //and third symbol
    if (isset($filename[2]))
    {
        $d .= $filename[2].'/';
        if (!file_exists($d))
        {
            $md = mkdir($d, 0755);
            if ($md===false && !file_exists($d)) return false;
        }
    }

    if (!file_exists($d.$filename.'.'.$ext)) return $d.$filename.'.'.$ext;
    else return false;
}
南笙 2024-11-25 16:21:11

我通常将图像命名为随机字符串,然后将其存储在附加到上传者 ID 的数据库中。您还可以通过这种方式存储有关图像的其他数据。

$文件名 = uniqid('', true) 。 '.jpg';

图像表

ID |用户 ID |文件名 | ETC

I typically name my images a random string, then store it in the database attached to the uploaders id. You can also store other data about the image this way.

$filename = uniqid('', true) . '.jpg';

Image Table

id | user_id | filename | etc

夜吻♂芭芘 2024-11-25 16:21:11

只是我自己的两分钱。
如果您要添加更多头像供用户选择,最好使用像 uniqid() 这样的函数而不是 user of。否则,你可以随心所欲地做。

不过,我使用了 userid 和 time() 的 MD5 哈希值。只是一个偏好问题。

Just my own two cents.
If you are gonna add more avatars for users to choose from, you're better off using a function like uniqid() instead of the user of. Else, you can do that all you like.

I use an MD5 hash of the userid and time() though. Just a matter of preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文