Skip to content

fix: use safe type assertions to prevent panics#50

Merged
occult merged 1 commit into
occult:mainfrom
VitorFOG:fix/safe-type-assertions
Jan 13, 2026
Merged

fix: use safe type assertions to prevent panics#50
occult merged 1 commit into
occult:mainfrom
VitorFOG:fix/safe-type-assertions

Conversation

@VitorFOG

Copy link
Copy Markdown
Contributor

Summary

  • Replace unsafe type assertions with comma-ok pattern in session handling code
  • Prevents panic when session data is corrupted or has unexpected types from gob deserialization
  • Gracefully handles edge cases instead of crashing the application

Changes

  • pkg/services/auth.go: Safe assertion for user ID from session
  • pkg/msg/msg.go: Safe assertion for flash messages
  • pkg/ui/request.go: Safe assertions for CSRF token, authenticated user, and config

Problem

Direct type assertions like value.(string) panic if the value is not of the expected type. This can happen when:

  • Session cookies are corrupted
  • Gob deserialization returns unexpected types
  • Old session data has different type format

Solution

Use the comma-ok idiom: value, ok := x.(string) to safely check types and handle mismatches gracefully.

🤖 Generated with Claude Code

Replace unsafe type assertions with comma-ok pattern to gracefully
handle corrupted session data or unexpected types from gob deserialization.

- auth.go: Safe assertion for user ID from session
- msg.go: Safe assertion for flash messages
- request.go: Safe assertions for CSRF, user, and config

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@occult occult merged commit 7133073 into occult:main Jan 13, 2026
1 check passed
felipekafuri pushed a commit to axioms-studio/bandeira that referenced this pull request Feb 18, 2026
Replace unsafe type assertions with comma-ok pattern to gracefully
handle corrupted session data or unexpected types from gob deserialization.

- auth.go: Safe assertion for user ID from session
- msg.go: Safe assertion for flash messages
- request.go: Safe assertions for CSRF, user, and config

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
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