printf 和不安全的格式化字符串

发布于 2025-01-03 15:51:06 字数 225 浏览 1 评论 0原文

相关应用程序允许用户在纯文本配置文件中定义自己的消息(主要用于自定义和/或本地化目的),这些消息在运行时传递给 printf 样式函数。如果用户定义的格式化字符串有错误,可能会发生很多不好的事情。

清理此类用户输入的格式字符串的最佳方法是什么?或者我应该完全放弃这种方法并使用另一种方法来让用户安全地自定义消息?

解决方案必须以某种方式可移植(Windows、Linux、BSD、x86、x86-64)。

The application in question allows users to define their own messages (mainly for customization and/or localization purposes) in plain-text configuration file, which are passed to printf-style functions at runtime. If the user-defined formatting string is faulty, a whole lot of bad things can happen.

What is the best way to sanitize such user-inputted formatting strings? Or should I drop this approach entirely and use another method to let users safely customize the messages?

Solution must be somehow portable (Windows, Linux, BSD, x86, x86-64).

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

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

发布评论

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

评论(2

凯凯我们等你回来 2025-01-10 15:51:06

定义您自己的格式化语言,您的代码将其转换为有效的格式字符串,从而限制用户可能遇到的麻烦(例如,根本不允许使用%,并定义您自己的符号/标记来指示应该出现%在输出中)。

Define your own formatting language, which your code translates into a valid format string, thereby restricting what trouble the user can get into (for example, not allowing % at all, and defining your own symbol/marker to use to indicate a % should appear in the output).

装迷糊 2025-01-10 15:51:06

你有两个选择:

  1. 让用户的混乱(有意或无意)自己搞乱,即不要让用户的个人配置相互干扰

  2. 不要让用户自定义结果。或者,如果您这样做,请将定制设置得如此有限,以至于他们无法做任何有害的事情。
    例如,我经常做这样的事情:允许用户向诸如 printf() 之类的内容提供自己的输入,但过滤器只允许具有特定(非常有限)字符集的内容。例如,我将使用类似 ^[a-zA-Z0-9_]+$ 的正则表达式,并且不要让其他任何内容进入。

任何时候您提供定制,您都打开了大门问题。在这些地方小心行走。

You have two choices:

  1. Let the user's mess-ups (intentional or not) mess up only themselves, i.e. don't let the users' personal configurations interfere with each other

  2. Don't let users customize the results. Or if you do, make the customization so limited that there is nothing they can do that is harmful.
    For example, I've frequently done things where users are allowed to provide their own input to things like printf(), but the filters only allowed for things with a certain (very limited) character set. E.g., I'll use a regexp of something like ^[a-zA-Z0-9_]+$ and don't let anything else in.

Any time you offer customization, you open the door to problems. Walk carefully in these grounds.

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