The Wayback Machine - https://web.archive.org/web/20240710093239/https://www.geeksforgeeks.org/secure-coding-what-is-it-all-about/
Open In App

Secure coding – What is it all about?

Last Updated : 15 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

  
 

So you think you can code? Well that’s great to know… The world definitely needs more geeks and nerds like you and me… But, are your programs secure? This is what this whole article is all about.

 

As a programmer, it is not only your job but also moral responsibility to ensure that your codes don’t have any margin which can be later on exploited by any other Black Hat Hacker. This is what secure coding is all about. If you do a quick search on Google about Secure Coding, the first link that will attract your attention will be our own Wiki.

 

Secure coding is the practice of developing computer software in a way that guards against the accidental introduction of security vulnerabilities. Defects, bugs and logic flaws are consistently the primary cause of commonly exploited software vulnerabilities.

 

Okay! That’s enough of jargon… What does that really mean? Let me give you an example. Now, since, I am a snake charmer, I will use Python 2.7x…

 

Python3




#test_run.py
pswd ="MY PASSWORD"
not_secret ="Geeks rock!"
  
inputVal = input("Please enter number of geeks") #A VERY BAD IDEA
print ("There are", inputVal,"geeks here, chanting", not_secret)


Now, go ahead and give it a try… It compiles successfully and you know what, it gives the desired output! So, here is what I got when I tried different inputs…
Run – 1
Please enter number of geeks 5 
 

            There are 5 geeks here, chanting Geeks rock!

Run – 2
          Please enter number of geeks dir() 
 

There are [‘pswd’, ‘not_secret’, ‘__builtins__’, ‘__doc__’, ‘__file__’,
‘__name__’, ‘__package__’] geeks here, chanting Geeks rock!

Run – 3
          Please enter number of geekspswd 
 

          There are MY PASSWORD geeks here, chanting Geeks rock!

 

If you didn’t realise it till now, let me state it… The program worked perfectly! But, not in the way we wanted it to… It printed out our secret data… Now, you can’t blame the language for this, and neither can you blame the programmer… He/she did what he was asked to do… This is where Secure Coding comes into play. Now, this example was just a small example, a very small one. There are endless number of possibilities of exploiting a program. All you need is a smart mind and an experience of exploiting the vulnerabilities. And if you are a network security guy/gal, then hiring a coder with no or very little knowledge about secure coding standards can prove to be the biggest mistake you can make. Thus, to have a safe professional future, it becomes a necessity to have complete knowledge about secure coding standards.

 

Now, who can decide what safe way of coding is? It is not something that a single programmer can do. Thankfully, we don’t need to bother about it. Go and check out the SEI CERT  Coding Standards. It has a very nice collection of recommended steps to take to ensure that your program is secure and that also sorted according to the programming languages – C, C++, Java, Perl, and Android. But, sadly, for the easiest language (in my opinion) there are no such standards given. Does that mean that a Python program is always secure? NO!! Fortunately, some Python enthusiasts set forward to make a list of similar recommendations for Python and resulted in the birth of what is known today as PEP 0008. Known as the Style Guide for Python Code, it was created in 2001.

 

With an exhaustive list of “safe” and “unsafe” programs, it serves as a must use for any Python programmer.

 

Now, enough of theory! Let’s get back to some coding stuff! I am now going to use a Hi-Fi term that you can further use to impress someone 😉 and that term is Cross Site Scripting (XSS). In the present scenario when every site has a comment section where they allow the visitors to share their experiences, XSS has come up to become a frequently used method by hackers (not a good term!) to steal data/ launch Distributed Denial Of Service (DDOS) attacks/ installing viruses and malwares into the system of client and many other ‘not – so – good’ acts.

 

Most of the comment sections allow the users to write in HTML code to provide an opportunity of formatting. This means that the comment is first processed and then the result is printed on the site. So, suppose instead of a comment, I write a JavaScript code like this:

 

window.alert(“Your comment has been received! – Geeks4Geeks”);

 

Now, going by what I discussed just now, the code will be processed and the client will receive a pop – up mentioning, “Your comment has been received! – Geeks4Geeks”. That doesn’t sound bad… But, just imagine the possibilities. One can write a simple script which will download a malware/virus in the system of client, or show an advertisement with content that will attract him/her to click on it which will be present in an IFrame which can steal cookies (this is called Clickjacking) which further leads to what is known as Session Hijacking; options are not limited! So, what should we do? Again, the solution lies in Secure Coding! Just to give you an example, of how you can avoid XSS, and Clickjacking using Django:

  
 

#Clickjacking

response = render_to_response(“webpage.html”, {},
context_instance=RequestContext(request))
response[‘X-Frame-Options’] = ‘DENY’ #Frame Killing
response[‘Content-Security-Policy’] = “frame-ancestors ‘none’”
return response

#XSS
#Django by default escapes HTML, so most programs will be safe from #XSS attacks
**{{ contents }}** #is safe
**{{ contents|safe }}** #Overriding escape, not a good idea 
 

So, basically the summary is that if you want to be successful in the field of coding, then you should make it a habit to follow Secure Coding Standards. Why? Because only those programs are safe that are not accessible to public… As soon as they are available to everyone, there is a whole lot of people out there trying to break your code… You better take care of that!

With this I take my leave!
Auf Wiedersehen! !(See you again in German)
About the author: 
 

Vishwesh Shrimali is an Undergraduate Mechanical Engineering student at BITS Pilani. He fulfils

vish

 about all the requirements not taught in his branch- white hat hacker, network security operator, and an ex – Competitive Programmer. As a firm believer in power of Python, his majority work has been in the same language. Whenever he get some time apart from programming, attending classes, watching CSI Cyber, he go for a long walk and play guitar in silence. His motto of life is – “Enjoy your life, ‘cause it’s worth enjoying!”

If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.
 



Previous Article
Next Article

Similar Reads

Best Practices for Secure Coding in Web Applications
Web applications are essential for corporations to deliver digital offerings, and they have got grow to be increasingly important in recent years as increasingly human beings get proper access to offerings online. However, with the upward push of cyber-assaults and data breaches, it’s vital to put in force web software safety best practices to prot
6 min read
Geeks Coding Challenge [GCC 2019] - A Three Day Coding Challenge By GeeksforGeeks
Remember the memories of your childhood when you accepted the challenge to solve some Jigsaw Puzzle, Complex Mathematics Problems, Tic-Tac-Toe, Sudoku or Rubik's cube kinda brain teaser game. Maybe it was scary the first time but when you solved the problem it was exciting, fun, challenging and interesting for you. Whether it's a game or it's real-
7 min read
Crack the Coding Interview With GeeksforGeeks Weekly Coding Contest
"The difference between ordinary and extraordinary is practice", quoted Vladamir Horowitz. What he said was nothing sort of unique as achieving perfection and success in your programming career can only be done by practicing programming questions till you ace them smoothly. This is where GeeksforGeeks dive along with you to help you become the ace
3 min read
Making your WordPress Website More Secure
Why security is Needed? Blogging now a days has become a hobby of so many people and Wordpress is the most opted platform for the same.By default, wordpress blogs employ a minimal level of security and also its files and/or plug-ins may often be outdated. These files are traceable and vulnerable to be hacked easily. Internet is not a secure place t
5 min read
Blockchain to Secure IoT Data
INTRODUCTION: Internet of things and Blockchain are two technologies which are gaining popularity since the time of their creation. In the near future, IoT is going to influence almost every day-to-day items we use. As the usage of this technology increases, the threat to misuse it also increases. Existing technologies are not enough to deal with t
7 min read
Different Ways To Secure an eCommerce Store
Are you worried about how you can keep your e-commerce customers safe? Any form of a data breach can quickly transform into a crisis. An eCommerce business may have to deal with fraud transactions- a process of chargeback that may affect the reputation of the company that may be unpleasant to them. Through a simple security audit, you can detect th
5 min read
Why HTTP is not Secure ?
Well everyone of us at least once come across the statement: “Make sure abc website uses HTTPS before entering your private information.” If yes then have you ever tried to find the reason behind this statement. Let’s find out the reason. Before proceeding further two points must be clear- HTTP : HyperText Transfer Protocol (HTTP) is the core commu
2 min read
How to Secure a Live Server?
Security is of utmost importance nowadays but still, some startups tend to ignore it because of the extra time it consumes. If not now, startups face hurdles at a later stage of scaling because of insecure systems. And in the current era where ransomware is at its peak, in order to make sure that open services running on your server are not comprom
4 min read
Social Engineering -Time To Be More Secure Than Before
Social Engineering is a method of hacking which is based on spoofing a person's identity and getting their details using socializing skills. It is an art of using Psychology things and marketing skills together for influencing the target victim and manipulate them for getting sensitive information. They generally not only get into systems but also
7 min read
5 Best Practices for Secure File Sharing
Businesses tend to thrive with the timely sharing of information or data. It is important for the organization to have access to the information that they need to accomplish their tasks, efficiently and conveniently. And that's why file-sharing is quite important to any business. Fortunately, technological advancement has made file sharing easy. Ho
4 min read
Practice Tags :
three90RightbarBannerImg