Skip to content

Commit 7546b1d

Browse files
committed
Privacy Violation: Heap Inspection
1 parent 689d475 commit 7546b1d

File tree

5 files changed

+143
-3
lines changed

5 files changed

+143
-3
lines changed

Path Manipulation/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
This attack is also known as _“dot-dot-slash”_, _“directory traversal”_, _“directory climbing”_ and _“backtracking”_.
77

8+
## Mitigation
9+
10+
Path Manipulation can be mitigated by validating the filename, folder name and extension validation and use the values further in the code only after the validations.
11+
812
## NOTE
913
The code for the Path Manipulation only check for the Filename validation, Extension Validation, File Size Validation, Unique Filename Validation. ___It doesn't check for the File Contents and Magic Numbers. Use this logic when you are concerned about the Path Manipulation issue ONLY___.
1014

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Privacy Violation: Heap Inspection
2+
3+
__Privacy Violation: Heap Inspection__ is a source code security issue that occurs mostly in C#, Java, and Swift applications. Strings are immutable in these languages, meaning that if they are used to store sensitive information such as passwords, credit card numbers, secrets, or tokens, these values will remain in memory until the JVM garbage collector (in Java) or ARC (Automatic Reference Counting) (in Swift) removes them.
4+
5+
There is no guarantee as to when garbage collection will take place. In the event of an application crash, a memory dump might reveal sensitive data, making it accessible to anyone inspecting the heap before garbage collection occurs.
6+
7+
## :warning: Important NOTE
8+
9+
___Never hardcode passwords or sensitive information in your code___. Use secret managers or vaults to securely store passwords and other sensitive data.
10+
11+
Even if you retrieve sensitive values from a secret manager or vault and assign them to a string variable, the issue remains. This is because sensitive data stored in a string cannot be cleared from memory immediately — it persists until garbage collection occurs.
12+
13+
For example, even if you initialize a StringBuffer or StringBuilder as shown below, an anonymous string object ("SecurePassword") is still created in heap memory and will remain there until garbage collection takes place:
14+
15+
```java
16+
StringBuffer password = new StringBuffer("SecurePassword");
17+
```
18+
19+
## Mitigation
20+
21+
To mitigate the Privacy Violation: Heap Inspection issue in Java, C#, and Swift applications:
22+
23+
:white_check_mark: Use character arrays (char[]) instead of strings to store sensitive information.
24+
25+
:white_check_mark: Manually clear arrays after use (e.g., overwriting with '\0').
26+
27+
:white_check_mark: Ensure cleanup happens in a finally block to guarantee execution.
28+
29+
By following these best practices, you reduce the risk of exposing sensitive data in memory dumps.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Text;
3+
4+
public class HeapInspectionCharArrayExample
5+
{
6+
public static void Main(string[] args)
7+
{
8+
Console.WriteLine("Before clearing sensitive data");
9+
/*
10+
Never Hardcode the passwords or any sensitive information in your code and use the Secret Managers or Secret Vaults to store the sensitive data. If this value is being populated from the Secret Managers or Secret Vaults, even then follow the same approach to clear the sensitive data.
11+
*/
12+
13+
char[] passwordChars = { 'Y', 'o', 'u', 'r', 'S', 't', 'r', 'o', 'n', 'g', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '1', '2', '3', '!' };
14+
StringBuilder connectionString = new StringBuilder("jdbc:sqlserver://localhost:1433;encrypt=true;user=sa;password=").Append(passwordChars).Append("columnEncryptionSetting=Enabled;");
15+
16+
Console.WriteLine("Password: ");
17+
foreach (char c in passwordChars)
18+
{
19+
Console.Write(c);
20+
}
21+
Console.WriteLine("");
22+
Console.WriteLine("Connection String: " + connectionString);
23+
24+
/*
25+
Codebase to do some operation with the sensitive data
26+
*/
27+
28+
// Clear the password and connection string from memory
29+
ClearSensitiveData(passwordChars);
30+
ClearSensitiveData(connectionString);
31+
32+
Console.WriteLine("");
33+
Console.WriteLine("After clearing sensitive data");
34+
Console.WriteLine("Password: ");
35+
foreach (char c in passwordChars)
36+
{
37+
Console.Write(c);
38+
}
39+
Console.WriteLine("");
40+
Console.WriteLine("Connection String: " + connectionString);
41+
}
42+
43+
private static void ClearSensitiveData(char[] arr)
44+
{
45+
for (int i = 0; i < arr.Length; i++)
46+
{
47+
arr[i] = '\0';
48+
}
49+
arr = null;
50+
}
51+
52+
private static void ClearSensitiveData(StringBuilder sb)
53+
{
54+
sb.Clear();
55+
sb = null;
56+
}
57+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class HeapInspectionCharArrayExample {
2+
public static void main(String[] args) {
3+
4+
System.out.println("Before clearing sensitive data");
5+
/*
6+
Never Hardcode the passwords or any sensitive information in your code and user the Secret Managers or Secret Vaults to store the sensitive data. If this value is being populated from the Secret Managers or Secret Vaults, even then follow the same approach to clear the sensitive data.
7+
*/
8+
9+
char[] passwordChars = {'Y', 'o', 'u', 'r', 'S', 't', 'r', 'o', 'n', 'g', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '1', '2', '3', '!'};
10+
StringBuffer connectionString = new StringBuffer("jdbc:sqlserver://localhost:1433;encrypt=true;user=sa;password=").append(passwordChars).append("columnEncryptionSetting=Enabled;");
11+
12+
System.out.println("Password: ");
13+
for(char c : passwordChars) {
14+
System.out.print(c);
15+
}
16+
System.out.println("");
17+
System.out.println("Connection String: " + connectionString);
18+
19+
/*
20+
Codebase to do some operation with the sensitive data
21+
*/
22+
23+
// Clear the password and connection string from memory
24+
clearSensitiveData(passwordChars);
25+
clearSensitiveData(connectionString);
26+
27+
System.out.println("");
28+
System.out.println("After clearing sensitive data");
29+
System.out.println("Password: ");
30+
for(char c : passwordChars) {
31+
System.out.print(c);
32+
}
33+
System.out.println("");
34+
System.out.println("Connection String: " + connectionString);
35+
}
36+
37+
private static void clearSensitiveData(char[] arr){
38+
for(int i = 0; i < arr.length; i++) {
39+
arr[i] = '\0';
40+
}
41+
arr = null;
42+
}
43+
44+
private static void clearSensitiveData(StringBuffer sb){
45+
sb.delete(0, sb.length());
46+
sb = null;
47+
}
48+
}

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
# secure-coding-examples
1+
# Secure Coding Examples
22

33
![Java CI](https://github.com/sahildari/secure-coding-examples/actions/workflows/java-ci.yml/badge.svg)
44
![Python CI](https://github.com/sahildari/secure-coding-examples/actions/workflows/python-ci.yml/badge.svg)
55
![GitHub repo size](https://img.shields.io/github/repo-size/sahildari/secure-coding-examples)
66
![GitHub last commit](https://img.shields.io/github/last-commit/sahildari/secure-coding-examples)
77
![GitHub contributors](https://img.shields.io/github/contributors/sahildari/secure-coding-examples)
88

9-
The purpose of this repo is to serve as secure coding examples targetting the most popular vulnerabilities.
9+
This repository is a collection of Secure Coding Examples. Here you will be able to find the code level fixes for the issues in your application. Currently the repo contains the code examples for the Java and Python Languages. You can use the logics implemented in the code examples to mitigate the issues in your Applications and we can follow to the Secure Coding Principles.
1010

1111
## Tech Stack
1212

1313
![Made with Python3](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54) ![Made with Flask](https://img.shields.io/badge/flask-%23000.svg?style=for-the-badge&logo=flask&logoColor=white)
1414
![Made with Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white) ![Made with Spring](https://img.shields.io/badge/spring-%236DB33F.svg?style=for-the-badge&logo=spring&logoColor=white)
1515

16+
Tech Stack used in this repository to create the Secure Coding Examples : __Java with SpringBoot__, __Python with Flask__.
17+
1618
## Licensing
1719
This project is licensed under the [GPL-3.0 license](https://opensource.org/license/gpl-3-0)
1820

19-
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
21+
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

0 commit comments

Comments
 (0)