Insecure Design (Güvensiz Tasarım)
Genel Bakış
Bu güvenlik açığı, uygulamanın tasarım aşamasında güvenlik önlemlerinin yeterince düşünülmemesinden kaynaklanır. Yani, güvenlik önlemlerinin sonradan eklenmeye çalışılması yerine, baştan tasarlanması gereken durumlarda ortaya çıkar.
Yaygın Örnekler
1. Weak Authentication Design (Zayıf Kimlik Doğrulama Tasarımı)
Örnek: Kimlik doğrulama sürecinin yetersiz tasarlanması.
// Kötü örnek
class AuthService {
login(username, password) {
if (username && password) {
return true;
}
return false;
}
}
// İyi örnek
class AuthService {
async login(username, password) {
const user = await this.findUser(username);
if (!user) {
throw new Error('Kullanıcı bulunamadı');
}
const isValid = await bcrypt.compare(password, user.password);
if (!isValid) {
throw new Error('Geçersiz şifre');
}
return this.generateToken(user);
}
}
2. Insufficient Authorization Control (Yetersiz Yetkilendirme Kontrolü)
Örnek: Kullanıcı rollerinin ve izinlerinin yetersiz tasarlanması.
// Kötü örnek
function checkAccess(user, resource) {
return user.isLoggedIn;
}
// İyi örnek
class AccessControl {
async checkAccess(user, resource, action) {
const permissions = await this.getUserPermissions(user.id);
return permissions.some(p =>
p.resource === resource &&
p.action === action
);
}
}
Korunma Yöntemleri
- Güvenlik mimarisini baştan tasarlayın
- Güvenlik gereksinimlerini belirleyin
- Güvenlik testleri yapın
- Güvenlik dokümantasyonu hazırlayın
- Güvenlik eğitimleri verin
Örnek Kod
Güvenli Tasarım Örneği
class SecureApplication {
constructor() {
this.authService = new AuthService();
this.accessControl = new AccessControl();
this.auditLogger = new AuditLogger();
}
async handleRequest(req, res) {
try {
// Kimlik doğrulama
const user = await this.authService.authenticate(req);
// Yetkilendirme
const hasAccess = await this.accessControl.checkAccess(
user,
req.resource,
req.action
);
if (!hasAccess) {
throw new Error('Yetkisiz erişim');
}
// İşlemi gerçekleştir
const result = await this.processRequest(req);
// Denetim kaydı
await this.auditLogger.log(user, req, result);
return result;
} catch (error) {
await this.auditLogger.logError(error);
throw error;
}
}
}