Skip to content

Commit 8558856

Browse files
committed
fix: use real agent ID in challenge pages (first 8 chars for security)
Previously challenge pages showed random placeholder like "DEF-CJW5" instead of the real agent ID. This was confusing and inconsistent with D-Agent-ID headers. Changes: - Added agentID field to ChallengeManager struct - Added SetAgentID() method to set real agent ID from config - Added GetAgentID() method to return first 8 chars (security) - Updated all ChallengeTemplateData to use cm.GetAgentID() - Added SetAgentID(agentID) call in main.go after config initialization - Updated getShortAgentID() as fallback for tests Security consideration: - Only first 8 characters of agent ID are displayed (matches D-Agent-ID format) - Full agent ID is never exposed in challenge pages - Consistent with HTTP header format: GEO+First8Chars Example: - Real agent ID: "agent-a1b2c3d4-e5f6-7890-abcd-ef1234567890" - Challenge page shows: "a1b2c3d4" - HTTP header shows: "US+a1b2c3d4" Logs: [ChallengeManager] Agent ID set for challenge pages: a1b2c3d4
1 parent 0254192 commit 8558856

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

firewall/challenges.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type ChallengeManager struct {
3838
captchaCache map[string]*CaptchaData
3939
stopChan chan struct{}
4040
template *template.Template
41+
agentID string // Real agent ID from config (first 8 chars used for display)
4142
}
4243

4344
type CaptchaData struct {
@@ -165,7 +166,7 @@ func (cm *ChallengeManager) IssueCookieChallenge(w http.ResponseWriter, r *http.
165166
ShowLoader: true,
166167
RayID: rayID,
167168
ClientIP: clientIP,
168-
AgentID: getShortAgentID(),
169+
AgentID: cm.GetAgentID(),
169170
JSCode: template.JS(redirectScript),
170171
}
171172

@@ -1520,3 +1521,32 @@ func (cm *ChallengeManager) CreateSessionCookie(sessionID string, secure bool) *
15201521
MaxAge: 86400, // 24 hours
15211522
}
15221523
}
1524+
1525+
// SetAgentID sets the real agent ID for display in challenge pages
1526+
// This should be called once during initialization with the actual agent ID from config
1527+
func (cm *ChallengeManager) SetAgentID(agentID string) {
1528+
cm.mu.Lock()
1529+
defer cm.mu.Unlock()
1530+
1531+
// Store only first 8 characters for security (matches D-Agent-ID format: GEO+ID)
1532+
if len(agentID) > 8 {
1533+
cm.agentID = agentID[:8]
1534+
} else {
1535+
cm.agentID = agentID
1536+
}
1537+
1538+
log.Printf("[ChallengeManager] Agent ID set for challenge pages: %s", cm.agentID)
1539+
}
1540+
1541+
// GetAgentID returns the stored agent ID (first 8 chars) for challenge pages
1542+
func (cm *ChallengeManager) GetAgentID() string {
1543+
cm.mu.RLock()
1544+
defer cm.mu.RUnlock()
1545+
1546+
if cm.agentID != "" {
1547+
return cm.agentID
1548+
}
1549+
1550+
// Fallback to old behavior if not set
1551+
return getShortAgentID()
1552+
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func main() {
5353

5454
configMgr := config.NewConfigManager(coreURL, agentID, agentKey)
5555

56+
// Set agent ID for challenge pages (used in D-Agent-ID header format: GEO+ID[:8])
57+
challengeMgr := firewall.GetChallengeManager()
58+
challengeMgr.SetAgentID(agentID)
59+
5660
// Set up connection limit updater callback
5761
configMgr.SetConnectionLimitUpdater(func(maxConnPerIP int) {
5862
connLimiter := firewall.GetConnectionLimiter()

0 commit comments

Comments
 (0)