Some time ago, after reading the "clean code", I found that many of these methods help to write more secure code. I used to read some web apps and found some vulnerabilities in the coding style. For example, a CMS has the functions of document classification, article display, comment, and so on, when processing parameters of each function, parameter filtering is performed independently. That is to say, there are a lot of redundant filtering code, which may be safe in a certain function, there is a vulnerability in another function. There are still many such extreme examples. Therefore:
When writing a function, we try our best to ensure that the function code is short. The longer the code, the more problems we may encounter, whether it is a functional bug or a possible security risk. So how to write the function more short? In fact, it is very simple to let the function process a function and focus on this function. If it is not related to this function, we compile it into another function to call it. For example, many web apps are implemented as follows:
$ Id = $ _ GET ['id];
// The code for filtering the middle one long seek
$ SQL = 'select * from articles where id = '. $ id
$ Query = Mysql: query ($ SQL)
Then there is a similar parameter processing code for different functions in another place. Therefore, in this case, we should ensure that: 1. There must be no code with repeated functions in the program, write all repeated functions into one function or put them in a class. 2. Short functions divide many trivial functions into other functions. 3. Do not care about other functions, this is also the significance of the function itself. That is to say, in the above example, the filter code can be written as a function separately, and all other similar places should only call this function, instead of writing a long code for filtering in each place.
How short should the function be? I don't know. It's almost the same as the number of five lines to 20 lines. Currently, my blog program is fully object-oriented. Currently, the maximum number of rows of a function or method is five, usually one or three rows, it may be a little longer in the future, but it will never be too much. The smaller the function, the higher the efficiency we find and solve the problem-at least I think so. By the way, you need to give the function a reasonable name that can be viewed by the function name, so don't even understand what this function is.
Use annotations
Remember that there is a "TODO" comment in java, as you can see in the code generated by VS, which roughly indicates what functions should be implemented in the place where TODO comments are made. When I write code, I first put the main effort into completing the function, wrote a TODO comment in areas that may involve security issues, and finally processed it according to the comment. For example, a search function of my blog:
Public function searchKey ($ key)
{
// TODO: filter cross-site and inject www.2cto.com
Return self: getResultArray ("select ar_title, ar_category, post_time, post_author from
Lx_articles where ar_title like '% $ key % '");
}
Finally, solve the security problem. Of course, this is just a personal method.
Use Mind Map
Mind Map is a way of organizing and clearly expressing thinking. When looking at a lot of web apps, we can see from the code style that their thinking is hard and confusing. The extreme example is like the problem I mentioned above. There are too many redundant Security Processing codes. Looking at such a program will increase my confidence in finding the vulnerability. I believe it is well handled here, and most of the other places are problematic. In fact, the processing code may be referred to online or directly plagiarized. From this point, we can know that the author has a weak awareness of security. However, we can still pretend to be awesome. First, we can draw a mind map for the overall functions to be completed. In the middle, we can draw out possible problems, similar to security modeling, however, the graph we plot is more inclined to the overall framework of the program. For example, the following is an extremely simple graph for the search function, which may be improved at any time with my new ideas:
Drawn by FreeMind (a software running on Linux to draw a mind map)
Others, no more.
I haven't posted my blog for a long time, but I have to boast about it. If you think the article is too disgusting, you can close it directly. If you want to target me, you can use the comment function below after reading the blog post, abuse or contempt for keywords allowed in Baidu space. However, you can't help but respect me for posting this blog post in a network environment with a download speed of around 4 K early in the morning.
Author chaos snow