Skip to content

Commit 71b2808

Browse files
authored
Merge pull request #3 from github-samples/security-for-beginners
Security for beginners
2 parents e7b68d0 + 4251595 commit 71b2808

File tree

14 files changed

+459
-30
lines changed

14 files changed

+459
-30
lines changed

.env

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# WARNING: This file contains intentionally exposed secrets for GitHub Advanced Security demo
2+
# DO NOT USE THESE IN PRODUCTION
3+
4+
# Stripe API Keys (fake but realistic pattern)
5+
NEXT_PUBLIC_API_KEY=sk_live_51A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0
6+
STRIPE_SECRET_KEY=sk_test_4eC39HqLyjWDarjtT1zdp7dc
7+
8+
# Azure Connection String (fake but realistic pattern)
9+
AZURE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=storageaccount;AccountKey=AKIAIOSFODNN7EXAMPLE;EndpointSuffix=core.windows.net
10+
11+
# AWS Access Keys (fake but realistic pattern)
12+
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
13+
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
14+
15+
# GitHub Personal Access Token (fake but realistic pattern)
16+
GITHUB_TOKEN=ghp_1234567890abcdefghijklmnopqrstuvwxyz12
17+
18+
# Database credentials (intentionally insecure for demo)
19+
DB_HOST=localhost
20+
DB_USER=admin
21+
DB_PASSWORD=SuperSecretPassword123!
22+
DATABASE_URL=postgresql://admin:SuperSecretPassword123!@localhost:5432/mydb

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

.gitignore

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
1-
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2-
31
# dependencies
42
/node_modules
53
/.pnp
64
.pnp.js
75

8-
# testing
9-
/coverage
10-
11-
# next.js
12-
/.next/
13-
/out/
14-
15-
# production
16-
/build
17-
18-
# misc
19-
.DS_Store
20-
*.pem
21-
22-
# debug
23-
npm-debug.log*
24-
yarn-debug.log*
25-
yarn-error.log*
26-
276
# local env files
7+
# WARNING: .env is not ignored for GitHub Advanced Security demo purposes
8+
# In production, you should ALWAYS ignore .env files
289
.env*.local
29-
.env
30-
31-
# vercel
32-
.vercel
10+
# .env
3311

34-
# typescript
35-
*.tsbuildinfo
36-
next-env.d.ts

README.md

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
1-
# gitfolio
2-
Minimal and modern developer portfolio template built with Next.js and Tailwind CSS.
1+
# Gitfolio - GitHub Advanced Security Demo
2+
3+
Minimal and modern developer portfolio template built with Next.js - **intentionally vulnerable for educational purposes**.
4+
5+
## ⚠️ Important Security Notice
6+
7+
**THIS REPOSITORY CONTAINS INTENTIONAL SECURITY VULNERABILITIES**
8+
9+
This is a demonstration repository for teaching GitHub Advanced Security features. It includes:
10+
- Exposed secrets and API keys
11+
- Vulnerable dependencies
12+
- Insecure code patterns
13+
14+
**DO NOT USE THIS CODE IN PRODUCTION**
15+
16+
## 🎯 Demo Scenarios
17+
18+
### 1. Secret Scanning Demo 🔐
19+
20+
**Files to review:**
21+
- [.env](.env) - Environment variables with exposed secrets
22+
- [config.js](config.js) - Hardcoded API keys and credentials
23+
24+
**What will be detected:**
25+
- Stripe API keys
26+
- AWS credentials
27+
- Azure connection strings
28+
- GitHub tokens
29+
- Database passwords
30+
31+
**Expected behavior:** GitHub Secret Scanning will automatically detect these patterns and create alerts in the Security tab.
32+
33+
### 2. Dependabot Demo 📦
34+
35+
**Files to review:**
36+
- [package.json](package.json)
37+
38+
**Vulnerable dependencies included:**
39+
- `axios@0.21.1` - Known CVE for Server-Side Request Forgery
40+
- `lodash@4.17.19` - Multiple security vulnerabilities
41+
- `express@4.17.1` - Potential security issues
42+
- `next@12.0.0` - Outdated version
43+
- `react@17.0.2` - Outdated version
44+
45+
**Expected behavior:** Dependabot will:
46+
1. Scan dependencies on push
47+
2. Create alerts for known vulnerabilities
48+
3. Automatically open pull requests with suggested updates
49+
50+
### 3. CodeQL Analysis Demo 🔍
51+
52+
**Vulnerable API endpoints:**
53+
54+
#### Command Injection
55+
- **File:** [pages/api/user-search.js](pages/api/user-search.js)
56+
- **Vulnerability:** User input directly concatenated into shell command
57+
- **Attack example:** `username=admin; rm -rf /`
58+
59+
#### SQL Injection
60+
- **File:** [lib/db.js](lib/db.js)
61+
- **Vulnerability:** Unsanitized user input in SQL queries
62+
- **Attack example:** `username=admin' OR '1'='1`
63+
64+
#### Path Traversal
65+
- **File:** [pages/api/download.js](pages/api/download.js)
66+
- **Vulnerability:** Unrestricted file path access
67+
- **Attack example:** `filename=../../../../etc/passwd`
68+
69+
#### Cross-Site Scripting (XSS)
70+
- **File:** [pages/api/display-message.js](pages/api/display-message.js)
71+
- **Vulnerability:** Unsanitized user input rendered in HTML
72+
- **Attack example:** `message=<script>alert('XSS')</script>`
73+
74+
**Expected behavior:** CodeQL will trace data flow from user inputs to dangerous sinks and create security alerts.
75+
76+
## 🚀 Setup Instructions
77+
78+
### Prerequisites
79+
- Node.js 14+ installed
80+
- GitHub account with Advanced Security enabled
81+
- Git configured locally
82+
83+
### Quick Start
84+
85+
### Enable GitHub Advanced Security
86+
87+
1. **Push code to GitHub:**
88+
```bash
89+
git add .
90+
git commit -m "Add security demo scenarios"
91+
git push origin security-for-beginners
92+
```
93+
2. **Enable Advanced Security features:**
94+
- Go to repository Settings → Security & analysis
95+
- Enable Dependency graph
96+
- Enable Dependabot alerts
97+
- Enable Dependabot security updates
98+
- Enable Secret scanning
99+
- Enable Code scanning (CodeQL analysis)
100+
101+
3. **View security alerts:**
102+
- Navigate to Security tab
103+
- Check "Code scanning" for CodeQL alerts
104+
- Check "Secret scanning" for exposed credentials
105+
- Check "Dependabot" for vulnerable dependencies
106+
107+
## 🛡️ Remediation Examples
108+
109+
### Fix Secret Scanning Issues:
110+
1. Remove hardcoded secrets from code
111+
2. Use environment variables properly
112+
3. Add `.env` to `.gitignore`
113+
4. Rotate exposed credentials
114+
5. Use GitHub Secrets for CI/CD
115+
116+
### Fix Dependabot Issues:
117+
1. Review Dependabot PRs
118+
2. Test updated dependencies
119+
3. Merge security updates
120+
4. Configure Dependabot settings
121+
122+
### Fix CodeQL Issues:
123+
1. **Command Injection:** Use parameterized commands or allowlists
124+
2. **SQL Injection:** Use prepared statements/parameterized queries
125+
3. **Path Traversal:** Validate and sanitize file paths
126+
4. **XSS:** Sanitize user input, use React's built-in escaping
127+
128+
## 📚 Educational Resources
129+
130+
- [GitHub Advanced Security Documentation](https://docs.github.com/en/code-security)
131+
- [CodeQL Documentation](https://codeql.github.com/docs/)
132+
- [Secret Scanning Patterns](https://docs.github.com/en/code-security/secret-scanning/secret-scanning-patterns)
133+
- [Dependabot Documentation](https://docs.github.com/en/code-security/dependabot)
134+
135+
## 🎓 Learning Objectives
136+
137+
After working through this demo, you should understand:
138+
1. How GitHub automatically detects security vulnerabilities
139+
2. The difference between Secret Scanning, Dependabot, and CodeQL
140+
3. How to interpret and remediate security alerts
141+
4. Best practices for secure coding
142+
5. How to configure security policies for your repositories
143+
144+
---
145+
146+
**Remember:** This repository is for educational purposes only. Never deploy vulnerable code to production!

config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// WARNING: This config file contains hardcoded secrets for demo purposes
2+
// DO NOT USE IN PRODUCTION
3+
4+
const config = {
5+
stripe: {
6+
// Hardcoded Stripe secret key (vulnerability for Secret Scanning demo)
7+
secretKey: 'sk_live_51A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0',
8+
publishableKey: 'pk_live_51A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0'
9+
},
10+
11+
database: {
12+
// Hardcoded database password (vulnerability for Secret Scanning demo)
13+
connectionString: 'postgresql://dbuser:P@ssw0rd123!@localhost:5432/portfolio'
14+
},
15+
16+
api: {
17+
// Hardcoded API key (vulnerability for Secret Scanning demo)
18+
key: 'AIzaSyD-1234567890abcdefghijklmnopqrstuv',
19+
endpoint: 'https://api.example.com'
20+
}
21+
};
22+
23+
module.exports = config;

lib/db.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// WARNING: VULNERABLE CODE - SQL Injection Demo
2+
// DO NOT USE IN PRODUCTION
3+
// This module demonstrates SQL injection vulnerabilities for CodeQL detection
4+
5+
import { createConnection } from 'mysql2/promise';
6+
7+
// Mock database connection for demo purposes
8+
const dbConfig = {
9+
host: 'localhost',
10+
user: 'root',
11+
password: 'password',
12+
database: 'gitfolio'
13+
};
14+
15+
// VULNERABILITY: SQL Injection - Direct string concatenation
16+
export async function getUserByName(username) {
17+
const connection = await createConnection(dbConfig);
18+
19+
// User input concatenated directly into SQL query
20+
// An attacker could use input like: "admin' OR '1'='1"
21+
const query = `SELECT * FROM users WHERE username = '${username}'`;
22+
23+
const [rows] = await connection.execute(query);
24+
await connection.end();
25+
26+
return rows;
27+
}
28+
29+
// VULNERABILITY: SQL Injection in search function
30+
export async function searchUsers(searchTerm) {
31+
const connection = await createConnection(dbConfig);
32+
33+
// Another example of SQL injection
34+
const query = `SELECT id, username, email FROM users WHERE username LIKE '%${searchTerm}%' OR email LIKE '%${searchTerm}%'`;
35+
36+
const [rows] = await connection.execute(query);
37+
await connection.end();
38+
39+
return rows;
40+
}
41+
42+
// VULNERABILITY: SQL Injection in delete operation
43+
export async function deleteUser(userId) {
44+
const connection = await createConnection(dbConfig);
45+
46+
// Direct concatenation in DELETE statement
47+
const query = `DELETE FROM users WHERE id = ${userId}`;
48+
49+
await connection.execute(query);
50+
await connection.end();
51+
52+
return { success: true };
53+
}

next.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
swcMinify: true,
5+
}
6+
7+
module.exports = nextConfig

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "gitfolio",
3+
"version": "1.0.0",
4+
"description": "Minimal and modern developer portfolio template built with Next.js and Tailwind CSS",
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"next": "12.0.0",
13+
"react": "17.0.2",
14+
"react-dom": "17.0.2",
15+
"axios": "0.21.1",
16+
"lodash": "4.17.19",
17+
"express": "4.17.1"
18+
},
19+
"devDependencies": {
20+
"eslint": "8.0.0",
21+
"eslint-config-next": "12.0.0"
22+
}
23+
}

pages/_app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../styles/globals.css'
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp

pages/api/display-message.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// WARNING: VULNERABLE CODE - XSS (Cross-Site Scripting) Demo
2+
// DO NOT USE IN PRODUCTION
3+
// This API endpoint demonstrates XSS vulnerabilities for CodeQL detection
4+
5+
export default function handler(req, res) {
6+
const { message } = req.query;
7+
8+
if (!message) {
9+
return res.status(400).json({ error: 'Message is required' });
10+
}
11+
12+
// VULNERABILITY: Reflected XSS
13+
// User input is directly embedded into HTML response without sanitization
14+
// An attacker could inject: "<script>alert('XSS')</script>"
15+
const html = `
16+
<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<title>Message Display</title>
20+
</head>
21+
<body>
22+
<h1>Your Message:</h1>
23+
<div>${message}</div>
24+
</body>
25+
</html>
26+
`;
27+
28+
res.setHeader('Content-Type', 'text/html');
29+
res.status(200).send(html);
30+
}

0 commit comments

Comments
 (0)