Skip to content

Fix IP filtering logic, remove dead code, and consolidate imports#175

Draft
Copilot wants to merge 5 commits intodevfrom
copilot/sub-pr-170
Draft

Fix IP filtering logic, remove dead code, and consolidate imports#175
Copilot wants to merge 5 commits intodevfrom
copilot/sub-pr-170

Conversation

Copy link
Contributor

Copilot AI commented Dec 28, 2025

Addresses code review feedback from PR #170 on IP whitelist implementation. The original IP filtering used string comparison for CIDR ranges, which fails for IP addresses (e.g., "192.168.1.10" < "192.168.1.9" lexicographically).

Core Fixes

  • IP filtering: Replace string gte/lte with proper ipaddress module checks. Use values_list('id', 'ip_address') to reduce query overhead. Prefix filter candidates by network size (/24 → 3 octets, /16 → 2 octets, /8 → 1 octet with trailing dot to prevent false matches).

  • Unreachable code: Remove impossible else branch in update_user_problem_stats that checked total_submissions > 0 after incrementing it.

  • Dead code removal: Delete 107 lines of commented-out update_user_assignment_stats function and related callback comments.

  • Import consolidation: Move ipaddress and logging to module level. Create single logger instance, removing 12 redundant local imports.

  • Error handling: Raise ValidationError on invalid IP prefix instead of silent pass-through.

Example

# Before: String comparison (broken)
queryset = queryset.filter(
    ip_address__gte=start_ip,
    ip_address__lte=end_ip
)

# After: Proper IP address checking
network = ipaddress.ip_network(ip_prefix, strict=False)
candidate_data = queryset.filter(
    ip_address__startswith=network_prefix
).values_list('id', 'ip_address')

valid_ids = [
    sub_id for sub_id, ip_addr in candidate_data
    if ipaddress.ip_address(ip_addr) in network
]
queryset = queryset.filter(id__in=valid_ids)

Testing

Added 14 test cases covering simple prefix matching, CIDR notation (/8, /16, /24), invalid input handling, and user stats updates across score transitions.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits December 28, 2025 09:39
…tering, improve imports

Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
…list

Co-authored-by: happylittle7 <7501374+happylittle7@users.noreply.github.com>
Copilot AI changed the title [WIP] Add IP whitelist filtering for submission queries Fix IP filtering logic, remove dead code, and consolidate imports Dec 28, 2025
Copilot AI requested a review from happylittle7 December 28, 2025 09:46
Base automatically changed from feat/finish_all_submission_function to dev December 28, 2025 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants