Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/claude-code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ jobs:
with:
fetch-depth: 1

- name: Setup GitHub App Token
uses: anthropics/claude-code-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Run Claude Code Review
id: claude-review
uses: anthropics/claude-code-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
prompt: |
Please review this pull request and provide feedback on:
Expand Down
10 changes: 3 additions & 7 deletions techblog_cms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@
print(f"IS_TESTING: {IS_TESTING}")

if IS_TESTING:
# Testing uses explicit env vars to keep CI simple
# Testing uses SQLite for simplicity
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('POSTGRES_DB', 'techblogdb'),
'USER': os.environ.get('POSTGRES_USER', 'techblog'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'techblogpass'),
'HOST': os.environ.get('POSTGRES_HOST', 'db'),
'PORT': os.environ.get('POSTGRES_PORT', '5432'),
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
Copy link
Owner

@dendencat dendencat Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot SQLiteに変える必要はありません。README.mdにそう書いていたのかもしれませんが、PostgreSQLでデータベース操作することを考慮しているので、ここを変更する必要はありません。

}
}
# Disable CSRF for testing
Expand Down
93 changes: 93 additions & 0 deletions techblog_cms/tests/test_login_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from django.test import TestCase, Client
from django.contrib.auth.models import User
from django.urls import reverse
import time


class LoginSecurityTests(TestCase):
"""Test cases for login security to prevent username enumeration attacks."""

def setUp(self):
self.client = Client()
# Create a test user
self.test_user = User.objects.create_user(
username='testuser',
password='testpass123'
)
self.login_url = reverse('login') # assuming the login view is named 'login'

def test_login_error_message_consistency(self):
"""Test that error messages are generic for both existing and non-existing users."""
# Test with non-existing user
response_nonexistent = self.client.post(self.login_url, {
'username': 'nonexistentuser',
'password': 'wrongpassword'
})

# Test with existing user but wrong password
response_wrong_password = self.client.post(self.login_url, {
'username': 'testuser',
'password': 'wrongpassword'
})

# Both should return the same generic error message
self.assertEqual(response_nonexistent.status_code, 401)
self.assertEqual(response_wrong_password.status_code, 401)

# Check that error messages are the same and generic
error_msg_nonexistent = response_nonexistent.context.get('error', '')
error_msg_wrong_password = response_wrong_password.context.get('error', '')

# Both should have the same generic error message
self.assertEqual(error_msg_nonexistent, error_msg_wrong_password)

# Error message should be generic (not revealing username existence)
self.assertEqual(error_msg_nonexistent, 'Invalid credentials')

def test_successful_login(self):
"""Test that successful login still works correctly."""
response = self.client.post(self.login_url, {
'username': 'testuser',
'password': 'testpass123'
})

# Should redirect to dashboard on successful login
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('dashboard'))

def test_login_timing_consistency(self):
"""Test that login attempts take similar time regardless of username existence."""
# This is a basic timing test - in a real security audit, more sophisticated timing analysis would be needed

times_nonexistent = []
times_wrong_password = []

# Run multiple attempts to get average timing
for _ in range(5):
# Time non-existent user login
start_time = time.time()
self.client.post(self.login_url, {
'username': 'nonexistentuser',
'password': 'wrongpassword'
})
times_nonexistent.append(time.time() - start_time)

# Time existing user with wrong password
start_time = time.time()
self.client.post(self.login_url, {
'username': 'testuser',
'password': 'wrongpassword'
})
times_wrong_password.append(time.time() - start_time)

# Calculate averages
avg_nonexistent = sum(times_nonexistent) / len(times_nonexistent)
avg_wrong_password = sum(times_wrong_password) / len(times_wrong_password)

# The difference should be minimal (less than 50ms)
# This is a very basic test - sophisticated timing attacks require more precise measurements
time_difference = abs(avg_nonexistent - avg_wrong_password)
self.assertLess(time_difference, 0.05,
f"Timing difference too large: {time_difference:.3f}s. "
f"Avg nonexistent: {avg_nonexistent:.3f}s, "
f"Avg wrong password: {avg_wrong_password:.3f}s")
8 changes: 4 additions & 4 deletions techblog_cms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def login_view(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
print(f"Username: {username}, Password: {password}")

# Use Django's authenticate function but ensure consistent behavior
user = authenticate(username=username, password=password)
print(f"User: {user}")
if user is not None:
print("Login successful")
login(request, user)
return redirect('dashboard')
else:
return render(request, 'login.html', {"error": "ユーザー名またはパスワードが違います。"}, status=401)
# Generic error message to prevent username enumeration
return render(request, 'login.html', {"error": "Invalid credentials"}, status=401)
return render(request, 'login.html')


Expand Down