fix: 11 bugs including critical data-loss and security issues#3974
Closed
mishrarenu066-beep wants to merge 1 commit intoMHSanaei:mainfrom
Closed
fix: 11 bugs including critical data-loss and security issues#3974mishrarenu066-beep wants to merge 1 commit intoMHSanaei:mainfrom
mishrarenu066-beep wants to merge 1 commit intoMHSanaei:mainfrom
Conversation
## Critical Fixes
### 1. DATA LOSS: 5 functions discard all other clients when updating one
Functions affected:
- SetClientTelegramUserID
- ToggleClientEnableByEmail
- ResetClientIpLimitByEmail
- ResetClientExpiryTimeByEmail
- ResetClientTrafficLimitByEmail
All five built a `newClients` slice by only appending the client
matching the target email, then replaced the entire client list.
Every other client in the inbound was silently deleted.
Fix: update client in-place with break instead of building new slice.
### 2. DATA LOSS: ResetSettings never deletes user credentials
ResetSettings() called `.Where("1 = 1").Error` instead of
`.Delete(model.User{}).Error`. The reset command did nothing to users.
### 3. SECURITY: WebSocket CheckOrigin allows cross-origin hijacking
The fallback `(originHost == "" || requestHost == "")` accepted
any origin with a missing host component. Removed the fallback and
added proper host normalization for IPv6/ports.
### 4. GRACEFUL SHUTDOWN: Server.Stop() uses cancelled context
s.cancel() was called before s.httpServer.Shutdown(s.ctx), making
the context already-done. Shutdown returned immediately (forced kill)
instead of waiting 10 seconds. Moved s.cancel() to end and used
context.WithTimeout(10s) for shutdown. Same fix applied to sub.go.
## Medium Fixes
### 5. Wrong success messages on error paths (~11 endpoints)
When validation failed, endpoints returned messages like
"inboundUpdateSuccess" alongside the error. Fixed to use
"somethingWentWrong" for all error paths.
### 6. resetAllTraffics/resetAllClientTraffics trigger restart on error
SetToNeedRestart() was called in else branch that ran even on failure.
Restructured to only call after confirming success.
### 7. disableInvalidClients has duplicate unreachable error check
Same "User %s not found" string check was nested twice.
Removed the inner duplicate.
### 8. DelInbound logs uninitialized tag variable
The else branch logged empty tag variable instead of actual inbound id.
### 9. check_cpu_usage.go index-out-of-range panic
cpu.Percent() can return empty slice. Added len(percent) > 0 guard.
### 10. Dead code: cron.Remove(entry) on never-added entry
var entry cron.EntryID defaults to 0; cron.Remove(0) is a no-op.
### 11. checkEmailExistForInbound duplicates checkEmailsExistForClients
Refactored to delegate to existing function instead of reimplementing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes 11 bugs found through a comprehensive deep scan of the entire codebase, including 4 critical issues that cause data loss or security vulnerabilities.
🔴 Critical Fixes
1. DATA LOSS: 5 functions silently delete all other clients when updating one
Files:
web/service/inbound.goFunctions:
SetClientTelegramUserID,ToggleClientEnableByEmail,ResetClientIpLimitByEmail,ResetClientExpiryTimeByEmail,ResetClientTrafficLimitByEmailAll five functions had the same copy-paste bug: they built a
newClientsslice by only appending the client matching the target email, then replaced the entire client list with just that one client. Every other client in the inbound was silently deleted.2. DATA LOSS:
ResetSettings()never deletes user credentialsFile:
web/service/setting.goThe function called
.Where("1 = 1").Errorinstead of.Delete(model.User{}).Error. This only reads the error from a query that does nothing — users were never deleted on reset.3. SECURITY: WebSocket
CheckOriginallows cross-origin hijackingFile:
web/controller/websocket.goThe fallback
|| originHost == "" || requestHost == ""accepted any origin with a missing host component, allowing WebSocket hijacking from malicious sites. Removed the fallback and added proper host normalization.4. GRACEFUL SHUTDOWN:
Server.Stop()uses cancelled contextFiles:
web/web.go,sub/sub.gos.cancel()was called befores.httpServer.Shutdown(s.ctx), making the context already-done.Shutdownreturned immediately (forced kill) instead of gracefully draining connections. Moveds.cancel()to end and usedcontext.WithTimeout(10s).🟡 Medium Fixes
5. Wrong success messages on error paths (~11 endpoints)
File:
web/controller/inbound.goWhen validation failed, endpoints returned messages like
"inboundUpdateSuccess"alongside the error. Fixed to use"somethingWentWrong"for all error paths.6.
resetAllTraffics/resetAllClientTrafficstrigger restart on errorSetToNeedRestart()was in anelseblock that ran even on failure. Restructured to only call after confirming success.7.
disableInvalidClientshas duplicate unreachable error checkSame
"User %s not found"string check was nested twice. Removed the inner duplicate.8.
DelInboundlogs uninitializedtagvariableThe
elsebranch logged the emptytagvariable instead of the actual inboundid.9.
check_cpu_usage.goindex-out-of-range paniccpu.Percent()can return an empty slice. Addedlen(percent) > 0guard.10. Dead code:
cron.Remove(entry)on never-added entryvar entry cron.EntryIDdefaults to 0;cron.Remove(0)is a no-op.11.
checkEmailExistForInboundduplicatescheckEmailsExistForClientsRefactored to delegate to existing function instead of reimplementing.
Files Changed
web/controller/inbound.go— error messages + flow fixesweb/controller/websocket.go— CheckOrigin security fixweb/service/inbound.go— 5 client-discard fixes + refactorweb/service/setting.go— ResetSettings fixweb/web.go— graceful shutdown + dead code removalsub/sub.go— graceful shutdown fixweb/job/check_cpu_usage.go— panic guard