PHP 安全性和独特的命名约定
像这样命名用户上传的图像(例如头像)有什么问题吗:
/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
确保在将用户名用作文件名的一部分之前对其进行了适当的清理。用户 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.
您给图像起的名称纯粹是约定俗成的。我不认为泄露用户的用户名是一个安全问题。 (如果是,那么您最好立即检查您的 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)一般来说,这没问题,但有些用户可能不喜欢显示他们的名字,并且如果您的站点不完全安全或者将来发现当前未知的 PHP 漏洞,作为主键的用户 ID 可能存在漏洞。
对于这类事情,我倾向于使用以下代码
,这应该给出一个类似 20110702155513.jpg 的字符串 - 这是图像命名的完整日期和时间,并且是唯一的。
如果你真的想安全,那么你可以编写一个回调函数,如果由于文件名不唯一而失败(通常是因为同一秒内有 2 个请求 - 不太可能但有可能),那么你可以使用回退在字符串中间添加用户 ID 或使用回退作为主要命名方法,例如
这允许隐藏用户 ID,但完全唯一。
*编辑 - *另一个选择是使用现有格式并创建 MD5 哈希,代码类似于
我希望这有帮助
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
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
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
I hope this helps
如果您经常公开使用该 ID,则使用该 ID 还可以让您的用户轻松查找/链接他们的图像。另一方面,如果您在 URL 中只有用户名 - 例如
/profile/$username
,那么我也会在图像文件名中使用用户名 - 以保持一致。关于安全性:如果您清理用户名,一切都很好。只需确保它是唯一的,这样人们就不能覆盖彼此的名字(这意味着您需要对数据库中的用户名使用与图像文件名中的用户名相同的卫生规则)。
如果您需要手动查找图像,则将用户名作为图像名称也可以“更轻松”地查找图像,但这种情况可能发生的频率不够高,对您来说没有价值。
总而言之,我会选择 ID。
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.
像您在第一个示例中使用的方式命名文件并没有巨大的风险,但是为了使其更安全,为什么不使用更好的东西,就像
我认为最好避免使用用户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
i think its better to avoid using users ID's
唯一的 user_id 就足够了,不需要“用户名”。
问题不在于此处,而在于文件数(在一个文件夹中)。
在文件夹中将它们分割为 1000(或分割为 100):将 ID 字符串的第一个、第二个和第三个符号放入单独的目录中:
如果很难编写算法,您可以尝试此功能:
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:
If it's hard to write algorithm, you can try this function:
我通常将图像命名为随机字符串,然后将其存储在附加到上传者 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
只是我自己的两分钱。
如果您要添加更多头像供用户选择,最好使用像 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.