简体   繁体   English

什么时候(以及何时)以及如何清理php中POST POST的数据(这样输出可用于Swift和HTML)

[英]When (and why the when) and how should I sanitize data from POST JSON in php (such that output usable in Swift AND HTML)

The past couple of days, I've read through a lot of resources on the sanitization of input and output data with PHP to prevent (most prominently) XSS and SQL injection, ia a bunch of question on SO. 在过去的几天里,我通过PHP阅读了大量有关输入和输出数据清理的资源,以防止(最突出的)XSS和SQL注入,这是SO上的一堆问题。 At this point, however, I feel like I am more confused and insecure about what I am supposed to do and what I am not supposed to do due in part to some contrary information, eg I've read many times that I don't need to use mysqli_real_escape_string or any other forms of sanitization of input whatsoever if I use prepared statements, other sources say I should just use it anyway or even that I should sanitize it like so ; 然而,在这一点上,我觉得我更加困惑和不安全我应该做什么和我不应该做什么部分由于一些相反的信息,例如我多次读过我不知道如果我使用准备好的语句,需要使用mysqli_real_escape_string或任何其他形式的输入消毒,其他消息来源说我应该只是使用它,或者甚至我应该像这样消毒它 ; this page by Apple rather roughly(?) goes over the topic; Apple的这个页面相当粗略(?)讨论了这个主题; etc. Therefore, I would really appreciate some clarification on what I am supposed to do - preferably but not necessarily, by someone who has got some experience in the field (server-side security) due to eg working in this field, having done a lot of research in it or maybe even being on the attacker's side(?). 因此,我真的很感激对我应该做的事情的一些澄清 - 最好但不一定是由那些在该领域有经验的人(服务器端安全),例如在这个领域工作,做过一个很多研究或甚至可能在攻击者身边(?)。

To understand my situation better, I am going to go over it as concisely as possible: 为了更好地了解我的情况,我将尽可能简明扼要地介绍它:

I am currently programming an app using Swift (iOS) and need to send some data to my server where it is saved in a table using SQL and can be retrieved from by other users (eg for a blog). 我目前正在使用Swift(iOS)编写应用程序,并且需要将一些数据发送到我的服务器,使用SQL将其保存在表中,并且可以从其他用户(例如,博客)中检索。

To do this I send the data via POST, encoded as JSON, to my server (“myphp.php”; with Alamofire, which shouldn't be very important, though) and decode it there. 为此,我通过POST(编码为JSON)将数据发送到我的服务器(“myphp.php”;使用Alamofire,这应该不是非常重要)并在那里解码。 And this is the first spot where I am not sure if I should already sanitize my data in some way (with reference to the question I linked above). 这是第一个我不确定是否应该以某种方式清理我的数据的地方(参考我上面链接的问题)。 Anyway, then I go on to eg insert it in a table using prepared statements (MySQL, so nothing's emulated). 无论如何,然后我继续使用预处理语句将其插入表中(MySQL,因此没有任何模拟)。 Moreover, I would also like the data I output to be usable in html or rather the entire PHP be usable for AJAX, too. 此外,我还希望我输出的数据可以在html中使用,或者整个PHP也可用于AJAX。

Here is an example of what I mean: 这是我的意思的一个例子:

// SWIFT
// set parameters for request
let parameters: Parameters = [
    “key”: “value”,
    ...
]

// request with json encoded parameters
Alamofire.request(“myphp.php”, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.validate().responseJSON(completionHandler: { (response) in
// do things with data (e.g. show blog post)

// PHP
header('Content-Type: application/json');

$decodedPost = json_decode(file_get_contents('php://input'), true);

// what to do with input...?

// PREPARED STATEMENTS: insert, select, etc.

// what to do with output...?

// echo response - json-encoded so that
// json completion handler in swift can work with it 
echo json_encode($output, JSON_NUMERIC_CHECK);

I've asked a friend for some advice on this and he told me he always does the following ( xss_clean() is a function he sent me, too) - whether the data is in- or outputted: 我已经向朋友询问了一些这方面的建议,他告诉我他总是做以下xss_clean()也是他发给我的一个函数) - 数据是输入还是输出:

$key = xss_clean(mysqli_real_escape_string($db, trim(htmlspecialchars($data)))); 
// e.g. $data = decodedPost["key"]

However, not only my research tells me that this probably isn't necessary, but he also told me this has its limitations, most obviously when data is supposed to be retrieved again from the server and displayed again to eg another user - as close to the original input as possible. 然而,不仅我的研究告诉我,这可能不是必要的,但他也告诉我这有其局限性,最明显的是当数据应该再次从服务器检索并再次显示给另一个用户时 - 尽可能接近原始输入尽可能。

As you can see, I am really confused. 如你所见,我真的很困惑。 I want to protect the data of users, which is sent to the server, as well as I can so this is a very important topic for me. 我想保护发送到服务器的用户数据,我也可以这样,这对我来说是一个非常重要的话题。 I hope this question isn't too broad but many other questions were, like I said, either, at least partly, contradictory or very old and eg still using simple mysql extensions and no prepared statements. 我希望这个问题不是太宽泛,但是很多其他问题,如我所说,或者至少部分地,矛盾或非常老,例如仍然使用简单的mysql扩展和没有准备好的语句。 If you need more information, feel free to ask. 如果您需要更多信息,请随时提出。 References to official documents (to support answers) are very much appreciated. 非常感谢参考官方文件(以支持答案)。 Thank you! 谢谢!

Input sanitization is a misleading term that indicates that you can wave a magic wand at all data and make it "safe data". 输入清理是一个误导性的术语,表示您可以在所有数据上挥动魔杖并使其成为“安全数据”。 The problem is that the definition of "safe" changes when the data is interpreted by different pieces of software as do the encoding requirements. 问题在于,当数据由不同的软件解释时,“安全”的定义会发生变化,编码要求也是如此。 Similarly the concept of "valid" data varies depending on context - your data may very well require special characters (',",&,<) - note that SO allows all of these as data. 类似地,“有效”数据的概念根据上下文而变化 - 您的数据可能非常需要特殊字符(',“,&,<) - 请注意,SO允许所有这些作为数据。

Output that may be safe to be embedded in an SQL query may not be safe for embedding in HTML. 嵌入在SQL查询中可能安全的输出可能不适合嵌入HTML。 Or Swift. 或斯威夫特。 Or JSON. 或JSON。 Or shell commands. 或shell命令。 Or CSV. 或者CSV。 And stripping (or outright rejecting) values so that they are safe for embedding in all those contexts (and many others) is too restrictive. 剥离(或彻底拒绝)值,以便它们可以安全地嵌入所有这些上下文(以及许多其他上下文)中,限制性太强。

So what should we do? 那我们该怎么办? Make sure the data is never in a position to do harm. 确保数据永远不会造成伤害。 The best way to achieve this is to avoid interpretation of the data in the first place. 实现这一目标的最佳方法是首先避免解释数据。 Parameterized SQL queries is an excellent example of this; 参数化SQL查询就是一个很好的例子。 the parameters are never interpreted as SQL, they're simply put in the database as, well, data. 这些参数永远不会被解释为SQL,它们只是作为数据放入数据库中。

That same data may be used for other other formats, such as HTML. 相同的数据可以用于其他其他格式,例如HTML。 In that case, the data should be encoded / escaped for that particular language at the moment it's embedded. 在这种情况下,数据应该在嵌入时对该特定语言进行编码/转义。 So, to prevent XSS, data should be HTML-escaped (or javascript or URL escaped) at the time it's being put into the ouput. 因此,为了防止XSS,数据应该在放入输出时进行HTML转义(或javascript或URL转义)。 Not at input time. 不在输入时间。 The same applies to other embedding situations. 这同样适用于其他嵌入情况。

So, should we just pass anything we get straight to the database? 那么,我们应该直接通过数据库传递任何东西吗?

No - there are definitely things you can check about user input, but this is highly context-dependent. 不 - 你肯定可以查看有关用户输入的内容,但这是高度依赖于上下文的。 Let's call this what it is - validation. 让我们称之为它 - 验证。 Make sure this is done on the server. 确保在服务器上完成此操作。 Some examples: 一些例子:

  • If a field is supposed to be an integer, you can certainly validate this field to ensure it contains an integer (or maybe NULL). 如果一个字段应该是一个整数,你当然可以验证这个字段以确保它包含一个整数(或者可能是NULL)。
  • You can often check that a particular value is one of a set of known values (white list validation) 您通常可以检查特定值是否为一组已知值之一(白名单验证)
  • You can require most fields to have a minimum and maximum length. 您可以要求大多数字段具有最小和最大长度。
  • You should usually verify that any string contains only valid characters for its encoding (eg, no invalid UTF-8 sequences) 您通常应验证任何字符串是否仅包含其编码的有效字符(例如,没有无效的UTF-8序列)

As you can see, these checks are very context-dependent. 如您所见,这些检查非常依赖于上下文。 And all of them are to help increase the odds you end up with data that makes sense. 而且所有这些都有助于增加你最终获得有意义数据的几率。 They should not be the only defense to protect your application from malicious input (SQL injection, XSS, command injection, etc), because this is not the place to do that. 它们不应该是保护您的应用程序免受恶意输入(SQL注入,XSS,命令注入等)的唯一防御,因为这不是那样做的地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP和MySQL-在显示给用户之前,从数据库中提取数据时是否应验证/清除数据? - PHP and MySQL - Should I validate/sanitize my data when pulling it from my database before displaying to user? 使用PHP PDO时,我应该清理/过滤用户输入和输出吗? - Should i sanitize/filter user input and output when using PHP PDO? 如果在if语句中使用,我应该清理$ _POST数据吗? - Should i sanitize $_POST data if used in if statement? 为什么我从android发布数据时,PHP中没有收到数据? - Why data not received in PHP when i POST it from android? 与用于注册和身份验证的REST API集成时,应该如何清理用户数据? - How should I sanitize user data when integrating with a REST API for registration and authentication? 当它们不是来自HTML表单时,如何将PHP POST和FILES数据发送到AJAX URL调用? - How do I send PHP POST and FILES data to an AJAX URL call when they are not from an HTML form? 在将文本区域输出回文本区域时,如何正确清理从文本区域接收的数据? - How do I properly sanitize data received from a text area, when outputting it back into the text area? 尝试读取从 php 页面到 swift 页面的 json 编码数据时遇到此问题 - I have this issue when trying to read my data which is json encoded from the php page to the swift page 消毒? 不输出为HTML或不进行SQL查询时 - Sanitize? When it doesn't output as HTML nor go into an SQL query 使用json_encode()时,此json输出的php数组应如何显示? - How should the php array look like for this json output when using json_encode()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM