Secure coding - What is it all about?
Last Updated :
15 Dec, 2022
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

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.
Similar Reads
Cyber Security Tutorial
Cyber security, also known as information technology security, refers to the practice of protecting systems, networks, and programs from digital attacks. These cyber-attacks are usually aimed at accessing, changing, or destroying sensitive information, extorting money from users, or interrupting nor
6 min read
Introduction
OSI Security Architecture
The OSI Security Architecture is internationally recognized and provides a standardized technique for deploying security measures within an organization. It focuses on three major concepts: security attacks, security mechanisms, and security services, which are critical in protecting data and commun
8 min read
Active and Passive attacks in Information Security
In Cybersecurity, there are several kinds of cyber threats you need to know these days, that can relate to computer security, network security, and information security. There are basically two forms of threats: active and passive attacks. An active attack is an attack in which attackers directly ha
9 min read
Types of Security Mechanism
A security mechanism is a method or technology that protects data and systems from unauthorized access, attacks, and other threats. Security measures provide data integrity, confidentiality, and availability, thereby protecting sensitive information and maintaining trust in digital transactions. In
3 min read
A Model for Network Security
When we send our data from the source side to the destination side we have to use some transfer method like the internet or any other communication channel by which we are able to send our message. The two parties, who are the principals in this transaction, must cooperate for the exchange to take p
2 min read
Cyber Technology
Basics of Wi-Fi
We've been studying a lot about the Wired Network. Ethernet is the most common example. Wired networks differ from wireless which uses radio waves rather than transmitting electrical signals over the cables. Wi-Fi stands for Wireless Fidelity. It is a technology for wireless local area networking wi
3 min read
The Internet and the Web
Introduction :The internet is a global network of interconnected computers and servers that allows people to communicate, share information, and access resources from anywhere in the world. It was created in the 1960s by the US Department of Defense as a way to connect computers and share informatio
6 min read
What is a Website ?
A website is a collection of many web pages, and web pages are digital files that are written using HTML(HyperText Markup Language). To make your website available to every person in the world, it must be stored or hosted on a computer connected to the Internet round a clock. Such computers are know
5 min read
Cryptography and Network Security Principles
In the present-day scenario security of the system is the sole priority of any organization. The main aim of any organization is to protect their data from attackers. In cryptography, attacks are of two types: Passive attacks and Active attacks. Passive attacks are those that retrieve information fr
9 min read
Public Key Infrastructure
Public key infrastructure or PKI is the governing body behind issuing digital certificates. It helps to protect confidential data and gives unique identities to users and systems. Thus, it ensures security in communications. The public key infrastructure uses a pair of keys: the public key and the p
7 min read
What is Electronic Signature?
Electronic signature or e-signature is an electronic way of signing a document or data through electronic devices, this means that such a digital form of signing is also seen as legal and authentic like the conventional hand-written one, whereby signatory has read all contents and accepted them, the
7 min read
Identity and Access Management
In a recent study by Verizon, 63% of the confirmed data breaches are due to either weak, stolen, or default passwords used. There is a saying in the cybersecurity world that goes like this âNo matter how good your chain is itâs only as strong as your weakest link.â and exactly hackers use the weakes
11 min read
What Is Cloud Computing ? Types, Architecture, Examples and Benefits
Nowadays, Cloud computing is adopted by every company, whether it is an MNC or a startup many are still migrating towards it because of the cost-cutting, lesser maintenance, and the increased capacity of the data with the help of servers maintained by the cloud providers. Cloud Computing means stori
15 min read
Cyber Crimes
Cyber Crime
Cybercrime refers to criminal activities carried out using computers and the internet, including hacking, data theft, malware attacks, and financial fraud. With businesses, governments, and individuals relying heavily on digital platforms, cyber threats have escalated, leading to billions in financi
12 min read
Cyber Criminals and their types
Cybercriminals are people who use the internet to commit illegal activities. They hack into computers, steal personal information, or spread harmful software. Their actions can harm individuals, businesses, and organizations. Often, they aim to make money, cause disruption, or gain unauthorized acce
5 min read
Psychological Profiling in Cybersecurity
The Cybersecurity Profiling is about keeping the computer systems safe from the bad peoples who want to steal the information or can cause harm. To do this better experts study the minds of these bad peoples called the cybercriminals. This study is called the psychological profiling. It helps us to
7 min read
What is Social Engineering? Working, Types, Prevention and Impact
Social Engineering is an umbrella term for multiple malicious activities done by cyber criminal over internet through human interaction. It doesn't involve the use of technical hacking techniques. Attackers use psychology and manipulation to trick users into performing actions that could compromise
8 min read
Cyberstalking
In Cyber Stalking, a cyber criminal uses the internet to threaten somebody consistently. This crime is often done through email, social media, and other online mediums. Cyber Stalking can even occur in conjunction with the additional ancient type of stalking, wherever the bad person harasses the vic
7 min read
How to Defend Against Botnets ?
A botnet is a collection of compromised computers (called bots) residing on the internet that can be controlled by cybercriminals. Botnets are used for all sorts of nefarious purposes, from spamming to stealing confidential information from computers to launching cyber attacks on other websites. The
4 min read
Emerging Attack Vectors in Cyber Security
In Cyber Security, knowing about attack vectors is key to keeping information safe and systems secure. An attack vector is a way that cybercriminals use to break into a network, system, or application by taking advantage of weaknesses. Attack vectors refer to the various paths or methods that attack
7 min read
What is Malware? And its Types
Malware is malicious software and refers to any software that is designed to cause harm to computer systems, networks, or users. Malware can take many forms. Individuals and organizations need to be aware of the different types of malware and take steps to protect their systems, such as using antivi
8 min read
What is Phishing?
Phishing is a form of online fraud in which hackers attempt to get your private information such as passwords, credit cards, or bank account data. This is usually done by sending false emails or messages that appear to be from trusted sources like banks or well-known websites. They aim to convince y
12 min read
Cyber Crime - Identity Theft
Identity Theft also called Identity Fraud is a crime that is being committed by a huge number nowadays. Identity theft happens when someone steals your personal information to commit fraud. This theft is committed in many ways by gathering personal information such as transactional information of an
5 min read
What is Cyber Terrorism?
In the computerized age, where innovation saturates each part of day-to-day existence, the idea of digital psychological warfare has arisen as a huge danger. Digital illegal intimidation alludes to the purposeful utilization of computerized assaults to inflict any kind of damage, interruption, or dr
13 min read
Keyloggers and Spyware
Worms, Viruses and beyond !!
This article introduces some very basic types of malicious content which may harm your PC in some way or the other.. The Threat The computer systems may become a victim of virus, worm, hacking etc types of attacks. The computer systems may crash, sensitive data can be stolen and misused or driver pr
5 min read
What is a Trojan Horse? Definition, Examples and More
The name "Trojan Horse" is taken from a classical story of the Trojan War. It is a code that is malicious and has the capacity to take control of the computer. It is designed to steal, damage, or do some harmful actions on the computer. It tries to deceive the user to load and execute the files on t
6 min read
Image Steganography in Cryptography
The word Steganography is derived from two Greek words- 'stegos' meaning 'to cover' and 'grayfia', meaning 'writing', thus translating to 'covered writing', or 'hidden writing'. Steganography is a method of hiding secret data, by embedding it into an audio, video, image, or text file. It is one of t
8 min read
Difference between DOS and DDOS attack
Here in the spectrum of cybersecurity, the various types of attacks should be distinguished for systems and networks to be protected. There are two categories of these; DOS, the short form for Denial of Service, and DDOS, which stands for Distributed Denial of Service. Both are meant to flood the ta
5 min read
Types of SQL Injection (SQLi)
SQL Injection is an attack that employs malicious SQL code to manipulate backend databases in order to obtain information that was not intended to be shown, The data may include sensitive corporate data, user lists, or confidential consumer details. This article contains types of SQL Injection with
6 min read
Buffer Overflow Attack with Example
A buffer is a temporary area for data storage. When more data (than was originally allocated to be stored) gets placed by a program or system process, the extra data overflows. It causes some of that data to leak out into other buffers, which can corrupt or overwrite whatever data they were holding.
3 min read
Reverse Engineering - Software Engineering
Software Reverse Engineering is a process of recovering the design, requirement specifications, and functions of a product from an analysis of its code. It builds a program database and generates information from this. This article focuses on discussing reverse engineering in detail. What is Reverse
6 min read
Difference Between Vulnerability and Exploit
The concepts of vulnerability and exploit are fundamental in Cyber Security, yet they represent different aspects of security risks. While a vulnerability refers to a weakness or flaw in a system that could potentially be exploited, an exploit is the actual method or tool used by attackers to take a
5 min read
Basic Network Attacks in Computer Network
Many people rely on the Internet for many of their professional, social and personal activities. But there are also people who attempt to damage our Internet-connected computers, violate our privacy and render inoperable the Internet services. Given the frequency and variety of existing attacks as w
7 min read
Kali Linux - Hacking Wi-Fi
These days the Wi-Fi networks are more secure than the older days, These days most wireless access points use WPA(Wi-Fi Protection Access) 2 Pre Shared Key in order to secure the network. This WPA 2 uses a stronger encryption algorithm which is known as AES which is very difficult to crack. When it
4 min read
Web Server and its Types of Attacks
Web Servers are where websites are stored. They are computers that run an operating system and are connected to a database to run multiple applications. A web server's primary responsibility is to show website content by storing, processing, and distributing web pages to users. Web servers are essen
6 min read
Types of VoIP Hacking and Countermeasures
Voice over IP or Voice over Internet Protocol (VoIP) is a collection of different technologies and practices that allows the delivery of voice communication, images, audio, video, through packet data networks over the internet protocol. This makes it very cost-efficient, flexible, and various other
4 min read
How to Spoof SMS Message in Linux ?
In this article, we will show how to spoof SMS messages in Linux using two of the following tools:- fake-smsSocial Engineering Toolkit (SET)1.) Fake-sms It is a tool written in simple script to send SMS anonymously. Features:Send sms anonymouslyFast sms deliveryInternational sms sending available.On
2 min read
Prevention and Protection
Difference Between Backup and Recovery
As technology continues to evolve, everyone uses a device for either work or entertainment, resulting in data being generated continuously. Keeping the data safe is very important. With the increase in data, ensuring its safety has become very important. Proper storage and protection of data have be
4 min read
Manual Code Review : Security Assessment
Secure Code Review is code assessment for identifying security vulnerabilities at an early stage in development lifecycle. When used together with penetration testing(automated and manual), it can significantly improve security posture of an organization. This article does not discuss a process for
3 min read
Penetration Testing - Software Engineering
In this guide, we'll explore the fundamentals of penetration testing, its importance in cybersecurity, and how it fits into the software development lifecycle (SDLC). From network security to web application security, we'll be going into various aspects of pen testing, equipping you with the knowled
10 min read
Security Testing Tools - Software Testing
Security testing tools are essential for identifying and addressing vulnerabilities in applications, systems, and networks before they can be exploited by malicious attackers. These tools play a crucial role in safeguarding sensitive data, ensuring compliance, and maintaining trust with users. In mo
8 min read
Intrusion Detection System (IDS)
Intrusion is when an attacker gets unauthorized access to a device, network, or system. Cyber criminals use advanced techniques to sneak into organizations without being detected. Intrusion Detection System (IDS) observes network traffic for malicious transactions and sends immediate alerts when it
9 min read
What is Vulnerability Assessment?
Living in a world with more and more complex threats posted by cybercriminals, it is imperative that you shield your networks. A vulnerability scanning is done to understand areas that are prone to an attack by the invader before they exploit the system. The above measures not only protect data and
6 min read
Secure coding - What is it all about?
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 h
6 min read
Cyber Crime Investigation