Skip to content

Commit dd92658

Browse files
authored
Merge pull request #50 from pythonhacker/feature/unit-tests-and-updates
Refactor code and add unit tests
2 parents a7ca996 + 79fc24b commit dd92658

File tree

13 files changed

+1727
-313
lines changed

13 files changed

+1727
-313
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binaries
9+
*.test
10+
11+
# Output of go build
12+
*.out
13+
14+
# Go workspace file (Go 1.18+)
15+
go.work
16+
go.work.sum
17+
18+
# Dependency directories (optional, if you use vendoring)
19+
vendor/
20+
21+
# IDE/editor files
22+
.idea/
23+
.vscode/
24+
*.swp
25+
*.swo
26+
*~
27+
28+
# OS-generated files
29+
.DS_Store
30+
Thumbs.db
31+
allpasswds*

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ PROGRAM=varuh
33

44
all: program
55

6-
program: $(wildcard *.go)
6+
program: scripts/main.go $(wildcard *.go)
77
@echo "Building ${PROGRAM}"
88
@go mod tidy
9-
@go build -o ${PROGRAM} *.go
9+
@go build -o ${PROGRAM} scripts/main.go
1010

1111
install:
1212
@echo -n "Installing ${PROGRAM}"

SECURITY_AUDIT_REPORT.md

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
# Varuh Security Audit Report
2+
3+
## Executive Summary
4+
5+
This document outlines security vulnerabilities and design flaws identified in the Varuh password manager during a comprehensive security audit. The analysis covers cryptographic implementation, database security, input validation, file handling, and overall system architecture.
6+
7+
## Critical Security Issues
8+
9+
### 1. SQL Injection Vulnerabilities
10+
11+
**Severity: HIGH**
12+
13+
**Location:** `db.go:526-530` and `db.go:702`
14+
15+
**Issue:** Direct string interpolation in SQL queries without proper parameterization.
16+
17+
```go
18+
// Vulnerable code in searchDatabaseEntry function
19+
searchTerm = fmt.Sprintf("%%%s%%", term)
20+
query := db.Where(fmt.Sprintf("title like \"%s\"", searchTerm))
21+
22+
for _, field := range []string{"user", "url", "notes", "tags"} {
23+
query = query.Or(fmt.Sprintf("%s like \"%s\"", field, searchTerm))
24+
}
25+
26+
// Vulnerable code in iterateEntries function
27+
rows, err = db.Model(&Entry{}).Order(fmt.Sprintf("%s %s", orderKey, order)).Rows()
28+
```
29+
30+
**Impact:** Attackers could potentially execute arbitrary SQL commands by crafting malicious search terms or order parameters.
31+
32+
**Recommendation:** Use GORM's parameterized queries:
33+
```go
34+
query := db.Where("title LIKE ?", "%"+term+"%")
35+
query = query.Or("user LIKE ?", "%"+term+"%")
36+
// etc.
37+
```
38+
39+
### 2. Weak Random Number Generation
40+
41+
**Severity: MEDIUM-HIGH**
42+
43+
**Location:** `crypto.go:478`
44+
45+
**Issue:** Use of `math/rand` with time-based seeding for password generation.
46+
47+
```go
48+
rand.Seed(time.Now().UnixNano())
49+
length = rand.Intn(4) + 12
50+
```
51+
52+
**Impact:** Predictable random numbers could lead to weaker password generation.
53+
54+
**Recommendation:** Use `crypto/rand` consistently throughout the application.
55+
56+
### 3. Insecure File Permissions
57+
58+
**Severity: MEDIUM**
59+
60+
**Location:** Multiple files
61+
62+
**Issue:** Inconsistent file permission handling.
63+
64+
```go
65+
// In utils.go - config files created with 0644
66+
fh, err := os.OpenFile(configFile, os.O_RDWR, 0644)
67+
68+
// In crypto.go - encrypted files use 0600 (correct)
69+
err = os.WriteFile(encDbPath, encText, 0600)
70+
```
71+
72+
**Impact:** Configuration files may be readable by other users on the system.
73+
74+
**Recommendation:** Use 0600 permissions for all sensitive files.
75+
76+
## Design Flaws
77+
78+
### 4. Password Storage in Memory
79+
80+
**Severity: MEDIUM**
81+
82+
**Location:** Throughout the application
83+
84+
**Issue:** Passwords are stored in plain text in memory during operations and passed between functions as strings.
85+
86+
**Impact:** Passwords may persist in memory longer than necessary and could be exposed through memory dumps.
87+
88+
**Recommendation:**
89+
- Use secure memory clearing functions
90+
- Minimize password lifetime in memory
91+
- Consider using byte slices that can be zeroed
92+
93+
### 5. Insufficient Input Validation
94+
95+
**Severity: MEDIUM**
96+
97+
**Location:** Multiple locations
98+
99+
**Issues:**
100+
- No length limits on user inputs (titles, URLs, notes)
101+
- No validation of URL format beyond basic HTTP/HTTPS prefix
102+
- No sanitization of custom field names
103+
104+
**Impact:** Potential for denial of service or data corruption.
105+
106+
**Recommendation:** Implement comprehensive input validation and sanitization.
107+
108+
### 6. Signal Handling Race Conditions
109+
110+
**Severity: LOW-MEDIUM**
111+
112+
**Location:** `actions.go:40-50` and `actions.go:82-92`
113+
114+
**Issue:** Signal handlers may not properly clean up resources or may cause race conditions.
115+
116+
```go
117+
go func() {
118+
sig := <-sigChan
119+
fmt.Println("Received signal", sig)
120+
// Reencrypt
121+
encryptDatabase(defaultDB, &encPasswd)
122+
os.Exit(1)
123+
}()
124+
```
125+
126+
**Impact:** Potential data loss or corruption during unexpected termination.
127+
128+
**Recommendation:** Implement proper cleanup mechanisms and avoid race conditions.
129+
130+
### 7. Clipboard Security
131+
132+
**Severity: LOW-MEDIUM**
133+
134+
**Location:** `utils.go:599`
135+
136+
**Issue:** Passwords are copied to system clipboard without automatic clearing.
137+
138+
```go
139+
func copyPasswordToClipboard(passwd string) {
140+
clipboard.WriteAll(passwd)
141+
}
142+
```
143+
144+
**Impact:** Passwords may remain in clipboard history accessible to other applications.
145+
146+
**Recommendation:** Implement clipboard clearing after a timeout or provide user notification.
147+
148+
## Cryptographic Concerns
149+
150+
### 8. Argon2 Parameters
151+
152+
**Severity: LOW**
153+
154+
**Location:** `crypto.go:68`
155+
156+
**Issue:** Argon2 parameters may be insufficient for current security standards.
157+
158+
```go
159+
key = argon2.Key([]byte(passPhrase), salt, 3, 32*1024, 4, KEY_SIZE)
160+
```
161+
162+
**Impact:** Weaker key derivation than recommended.
163+
164+
**Recommendation:** Use Argon2id with higher memory (64MB+) and iterations (4+).
165+
166+
### 9. HMAC Implementation
167+
168+
**Severity: LOW**
169+
170+
**Location:** `crypto.go:183-186`
171+
172+
**Issue:** HMAC-SHA512 is used for authentication, which is acceptable but SHA-256 would be sufficient.
173+
174+
**Impact:** Minor performance impact, no security impact.
175+
176+
**Recommendation:** Consider using HMAC-SHA256 for better performance.
177+
178+
## File System Security
179+
180+
### 10. Temporary File Handling
181+
182+
**Severity: MEDIUM**
183+
184+
**Location:** `export.go:171` and `export.go:188`
185+
186+
**Issue:** Temporary files may not be properly cleaned up in all error scenarios.
187+
188+
```go
189+
tmpFile = randomFileName(os.TempDir(), ".tmp")
190+
// ... operations ...
191+
os.Remove(tmpFile) // May not execute in error cases
192+
```
193+
194+
**Impact:** Sensitive data may remain in temporary files.
195+
196+
**Recommendation:** Use defer statements for cleanup and ensure proper error handling.
197+
198+
### 11. File Overwrite Operations
199+
200+
**Severity: MEDIUM**
201+
202+
**Location:** `crypto.go:194-203`
203+
204+
**Issue:** Atomic file operations are attempted but may fail partially.
205+
206+
```go
207+
err = os.WriteFile(encDbPath, encText, 0600)
208+
if err == nil {
209+
err = os.WriteFile(dbPath, encText, 0600)
210+
if err == nil {
211+
os.Remove(encDbPath)
212+
}
213+
}
214+
```
215+
216+
**Impact:** Potential data loss if operations fail partially.
217+
218+
**Recommendation:** Implement proper atomic file operations using rename operations.
219+
220+
## Database Security
221+
222+
### 12. Database Schema Exposure
223+
224+
**Severity: LOW**
225+
226+
**Location:** `db.go:18-60`
227+
228+
**Issue:** Database schema is well-documented and predictable.
229+
230+
**Impact:** Makes it easier for attackers to understand the data structure.
231+
232+
**Recommendation:** Consider obfuscating field names or using generic field names.
233+
234+
### 13. No Database Encryption at Rest
235+
236+
**Severity: MEDIUM**
237+
238+
**Location:** Database operations
239+
240+
**Issue:** SQLite databases are stored unencrypted when not using the application's encryption.
241+
242+
**Impact:** Database files may be readable if accessed directly.
243+
244+
**Recommendation:** Consider using SQLCipher or similar encrypted database solutions.
245+
246+
## Recommendations Summary
247+
248+
### Immediate Actions Required:
249+
1. **Fix SQL injection vulnerabilities** - Use parameterized queries
250+
2. **Implement proper input validation** - Add length limits and sanitization
251+
3. **Fix file permissions** - Use 0600 for all sensitive files
252+
4. **Improve random number generation** - Use crypto/rand consistently
253+
254+
### Medium Priority:
255+
1. **Implement secure memory handling** - Clear sensitive data from memory
256+
2. **Improve file operations** - Use atomic operations
257+
3. **Add clipboard security** - Implement timeout or clearing
258+
4. **Enhance signal handling** - Prevent race conditions
259+
260+
### Long-term Improvements:
261+
1. **Upgrade cryptographic parameters** - Use stronger Argon2 settings
262+
2. **Consider database encryption** - Use SQLCipher
263+
3. **Implement audit logging** - Track security-relevant events
264+
4. **Add comprehensive testing** - Security-focused test suite
265+
266+
## Conclusion
267+
268+
While Varuh implements good cryptographic practices with AES-256 and XChaCha20-Poly1305, several critical security vulnerabilities need immediate attention. The SQL injection vulnerabilities are the most serious concern and should be addressed immediately. The overall architecture is sound, but implementation details need refinement to meet security best practices.
269+
270+
The application would benefit from a security-focused refactoring to address these issues systematically, with particular attention to input validation, memory management, and file handling security.

0 commit comments

Comments
 (0)