如何检查 URI 动态生成的变量名称的有效性

发布于 2024-12-07 20:32:33 字数 558 浏览 0 评论 0原文

我正在为我的自定义 MVC 项目构建路由器。

在我的路由器中,对于漂亮的 URL 名称,我遇到了问题。通过 URI 处理动态生成的变量名称的最佳实践是什么?

示例:

http://knjiskicrv.comoj.com/book/id/2/

将生成:

$page = 'book';
$id = '2';

现在,当有人故意开始搞乱 URI 时,可能会出现问题。喜欢:

http://knjiskicrv.comoj.com/book/id+one/2/

我会得到:

$page = 'book';
$id one = '2';

希望有人能给我一些如何预防和解决这个问题的建议?谢谢。

I am building router for my custom MVC project.

In my router, for pretty URL names, I ran into problem. What is the best practice for dealing with dynamically generated variables names via URI?

Example:

http://knjiskicrv.comoj.com/book/id/2/

Will generate:

$page = 'book';
$id = '2';

Now, problem may arise when someone deliberately start messing up with URI. Like:

http://knjiskicrv.comoj.com/book/id+one/2/

I will get:

$page = 'book';
$id one = '2';

Hope someone could give me some advice how to prevent and solve this? Thanks.

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

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

发布评论

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

评论(3

你是我的挚爱i 2024-12-14 20:32:33

我认为您是在询问如何减轻“跨站脚本”(XSS)漏洞。

这是一个很大的话题。请记住:(可能是恶意的)用户可以通过多种方式“故意开始搞乱……URI”。

建议:开始阅读:)

这里有一些链接:

http://seancoates.com/blogs/xss-woes< /a>

http://www.cgisecurity.com/xss-faq.html

http://www.uri.edu/webservices/phpGuideline.html

I think you're asking about mitigating "Cross Site Scripting" (XSS) vulnerabilities.

That's a big topic. And remember: there are LOTS of ways for a (potentially malicious) user to "deliberately start messing ... with the URI".

Suggestion: start reading :)

Here are some links:

http://seancoates.com/blogs/xss-woes

http://www.cgisecurity.com/xss-faq.html

http://www.uri.edu/webservices/phpGuideline.html

江湖彼岸 2024-12-14 20:32:33

首先,输入清理该网址。不要从可欺骗的输入源创建动态变量。好吧,您必须知道给定页面上会发生什么。这些变量保存什么变量以及什么类型的变量。

如果您必须显示一组类别,并且其中一个类别的名称是“id”

/products/monkeys/white/id/,该怎么办 - 您正确地...d

选择不同的约定来处理您的 URI。

就像将 URI 划分为区域、部分和页面元素一样。

http://www.oink.com/products/pigs/spottyones/angry /the_big_spotty_pig.html

区域 = '产品'
节 = array('spottyones','愤怒')
page = the_big_spotty_pig (这唯一标识了文章、产品等)。

当我必须使用变量时,这些变量主要与排序、页码等有关。因此可以将这些变量作为查询字符串参数附加。

更新
消毒:

你必须为自己设定规则。假设 URI 只能包含某些字符。

//Sanitization 
$uri = $_SERVER['REQUEST_URI']; // /products/monkey/angry/page.html

//allow only characters, numbers, underline and dash 
if (!preg_match('~^[a-z0-9-_]$~isD',$uri)) 
$uri = '/'; //URI has been tampered with

$uriparts = explode('/',$uri); 
/* array('products','monkey','angry','page.html') */

//Do whatever you want with the uri parts ...

First of all, input sanitize that url. Do not create dynamic variables from a spoofable input source. Well, you have to know, what to expect on the given page. What variables and what type of variables these hold.

What if you have to display a set of categories and one of the categories' name is 'id'

/products/monkeys/white/id/ - you are properly ...d

Choose a different convention for processing your URI.

Like divide the URI into area, section and page elements.

http://www.oink.com/products/pigs/spottyones/angry/the_big_spotty_pig.html

area = 'products'

section = array('spottyones','angry')

page = the_big_spotty_pig (this uniquely identifies the article, product etc.)

When I have to use variables, these are mostly about ordering, page nr, etc. So these can be appended as query string parameters.

UPDATE
Sanitization:

You have to set the rules for yourself. Let's say the URI can only contain certain characters.

//Sanitization 
$uri = $_SERVER['REQUEST_URI']; // /products/monkey/angry/page.html

//allow only characters, numbers, underline and dash 
if (!preg_match('~^[a-z0-9-_]$~isD',$uri)) 
$uri = '/'; //URI has been tampered with

$uriparts = explode('/',$uri); 
/* array('products','monkey','angry','page.html') */

//Do whatever you want with the uri parts ...
若水微香 2024-12-14 20:32:33

您可以将这些变量存储在数组中,这样您

$var['id one'] = '2';

就得到了我的建议。

You could store those variables in an array, so you get

$var['id one'] = '2';

Just my suggestion.

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