-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1365 lines (1176 loc) · 41.4 KB
/
main.go
File metadata and controls
1365 lines (1176 loc) · 41.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package main implements a secure OAuth2 server for the GitHub PR dashboard.
// It serves static files and handles GitHub OAuth authentication flow.
package main
import (
"context"
"crypto/rand"
"crypto/subtle"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/codeGROOVE-dev/gsm"
"github.com/codeGROOVE-dev/retry"
)
// Constants for configuration.
const (
defaultPort = "8080"
defaultAppID = 1546081
defaultClientID = "Iv23liYmAKkBpvhHAnQQ"
defaultRedirectURI = "https://reviewGOOSE.dev/oauth/callback"
baseDomain = "reviewGOOSE.dev"
// Rate limiting.
rateLimitRequests = 10
rateLimitWindow = 1 * time.Minute
// Timeouts.
httpTimeout = 10 * time.Second
shutdownTimeout = 30 * time.Second
stateExpiry = 5 * time.Minute
// Security.
maxRequestSize = 1 << 20 // 1MB
maxHeaderSize = 1 << 20 // 1MB
maxFailedLogins = 5
failedLoginWindow = 15 * time.Minute
)
//go:embed index.html
//go:embed assets/*
var staticFiles embed.FS
var (
port = flag.String("port", "", "Port to listen on (overrides $PORT)")
appID = flag.Int("app-id", defaultAppID, "GitHub App ID")
clientID = flag.String("client-id", defaultClientID, "GitHub OAuth Client ID")
clientSecret = flag.String("client-secret", "", "GitHub OAuth Client Secret")
redirectURI = flag.String("redirect-uri", defaultRedirectURI, "OAuth redirect URI")
allowedOrigins = flag.String("allowed-origins", "", "Comma-separated list of allowed origins for CORS")
// Build timestamp for cache busting (set at startup).
buildTimestamp string
// Security: Track failed login attempts.
failedAttempts = make(map[string][]time.Time)
failedMutex sync.Mutex
// One-time auth code exchange (token -> code mapping).
// Used to securely transfer tokens from auth subdomain to user subdomain.
authCodes = make(map[string]authCodeData)
authCodesMutex sync.Mutex
// Rate limiter for auth code exchange endpoint (prevent brute force attacks).
exchangeRateLimiter *rateLimiter
// CSRF protection using Go 1.25's CrossOriginProtection (Fetch Metadata).
csrfProtection *http.CrossOriginProtection
)
// authCodeData stores a one-time use auth code with expiration.
type authCodeData struct {
expiry time.Time
token string
username string
returnTo string
used bool
}
// rateLimiter implements a simple in-memory rate limiter.
type rateLimiter struct {
requests map[string][]time.Time
window time.Duration
limit int
mu sync.Mutex
}
func (rl *rateLimiter) limitHandler(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)
rl.mu.Lock()
defer rl.mu.Unlock()
now := time.Now()
cutoff := now.Add(-rl.window)
// Clean old requests - reuse slice to reduce allocations
validRequests := rl.requests[ip][:0]
for _, t := range rl.requests[ip] {
if t.After(cutoff) {
validRequests = append(validRequests, t)
}
}
if len(validRequests) >= rl.limit {
log.Printf("[SECURITY] Rate limit exceeded: ip=%s requests=%d limit=%d window=%v", ip, len(validRequests), rl.limit, rl.window)
http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)
return
}
rl.requests[ip] = append(validRequests, now)
// Prevent memory exhaustion: periodically clean up IPs with no recent requests
// This protects against DoS attacks using many different IPs
if len(rl.requests)%100 == 0 {
for oldIP, times := range rl.requests {
if len(times) == 0 || (len(times) > 0 && times[len(times)-1].Before(cutoff)) {
delete(rl.requests, oldIP)
}
}
}
next(w, r)
}
}
// isValidGitHubHandle validates that a string looks like a valid GitHub handle.
// GitHub handles can only contain alphanumeric characters and single hyphens,
// cannot begin or end with a hyphen, and must be 1-39 characters long.
func isValidGitHubHandle(handle string) bool {
if handle == "" || len(handle) > 39 {
return false
}
// Cannot start or end with hyphen
if strings.HasPrefix(handle, "-") || strings.HasSuffix(handle, "-") {
return false
}
// Check each character
for i, ch := range handle {
if ch >= 'a' && ch <= 'z' {
continue
}
if ch >= 'A' && ch <= 'Z' {
continue
}
if ch >= '0' && ch <= '9' {
continue
}
if ch == '-' {
// No consecutive hyphens
if i > 0 && handle[i-1] == '-' {
return false
}
continue
}
return false
}
return true
}
// clientIP extracts the client IP address from the request.
func clientIP(r *http.Request) string {
// SECURITY: Only use RemoteAddr to prevent header spoofing attacks
// X-Forwarded-For and X-Real-IP are trivially spoofable and should not be trusted
// for security-critical functions like rate limiting
//
// When behind a trusted proxy (like Cloud Run), RemoteAddr will be the proxy IP
// This means rate limiting happens at the proxy level, which is acceptable
// as it still prevents single-source DoS attacks
ip := r.RemoteAddr
if colon := strings.LastIndex(ip, ":"); colon != -1 {
return ip[:colon]
}
return ip
}
// securityHeaders adds security headers to all responses.
func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add request ID for tracking
requestID := r.Header.Get("X-Request-ID")
if requestID == "" {
requestID = generateID(8)
}
w.Header().Set("X-Request-ID", requestID)
// Prevent clickjacking
w.Header().Set("X-Frame-Options", "DENY")
// Prevent MIME type sniffing
w.Header().Set("X-Content-Type-Options", "nosniff")
// Enable XSS protection
w.Header().Set("X-XSS-Protection", "1; mode=block")
// Referrer policy
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
// Permissions policy
w.Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()")
// Content Security Policy
csp := []string{
"default-src 'self' https://reviewGOOSE.dev https://*.reviewGOOSE.dev",
"script-src 'self' https://reviewGOOSE.dev https://*.reviewGOOSE.dev",
"style-src 'self' https://reviewGOOSE.dev https://*.reviewGOOSE.dev",
"img-src 'self' https://reviewGOOSE.dev https://*.reviewGOOSE.dev https://avatars.githubusercontent.com data:",
"connect-src 'self' https://api.github.com https://turn.github.codegroove.app",
"font-src 'self' https://reviewGOOSE.dev https://*.reviewGOOSE.dev",
"object-src 'none'",
"frame-src 'none'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'none'",
"upgrade-insecure-requests",
}
w.Header().Set("Content-Security-Policy", strings.Join(csp, "; "))
// HSTS with preload (only for HTTPS)
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
// 2 years (recommended for preload), includeSubDomains, and preload directive
w.Header().Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload")
}
next.ServeHTTP(w, r)
})
}
// oauthTokenResponse represents the GitHub OAuth token response.
type oauthTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
// githubUser represents a GitHub user.
type githubUser struct {
Login string `json:"login"`
Name string `json:"name"`
ID int `json:"id"`
}
// loadClientSecret retrieves the GitHub OAuth client secret from environment or Secret Manager.
func loadClientSecret(ctx context.Context) string {
// Check environment variable first
if value := os.Getenv("GITHUB_CLIENT_SECRET"); value != "" {
log.Print("Using GITHUB_CLIENT_SECRET from environment variable")
return value
}
// Check if running in Cloud Run
isCloudRun := os.Getenv("K_SERVICE") != "" || os.Getenv("CLOUD_RUN_TIMEOUT_SECONDS") != ""
if !isCloudRun {
log.Print("Not running in Cloud Run, skipping Secret Manager")
return ""
}
// Fetch from Secret Manager (auto-detects project ID from metadata server)
log.Print("Fetching GITHUB_CLIENT_SECRET from Google Secret Manager")
secretValue, err := gsm.Fetch(ctx, "GITHUB_CLIENT_SECRET")
if err != nil {
log.Printf("Failed to fetch secret from Secret Manager: %v", err)
return ""
}
if secretValue == "" {
log.Print("WARNING: Secret Manager returned empty value for GITHUB_CLIENT_SECRET")
} else {
log.Print("Successfully fetched GITHUB_CLIENT_SECRET from Google Secret Manager")
}
return secretValue
}
func main() {
flag.Parse()
// Set build timestamp for cache busting
buildTimestamp = strconv.FormatInt(time.Now().Unix(), 10)
// Determine port with flag taking precedence over environment
serverPort := *port
if serverPort == "" {
serverPort = os.Getenv("PORT")
}
if serverPort == "" {
serverPort = defaultPort
}
// Allow environment variables to override empty flag values
if *appID == defaultAppID {
if envAppID := os.Getenv("GITHUB_APP_ID"); envAppID != "" {
if id, err := strconv.Atoi(envAppID); err == nil {
*appID = id
}
}
}
if *clientID == defaultClientID || *clientID == "" {
if envClientID := os.Getenv("GITHUB_CLIENT_ID"); envClientID != "" {
*clientID = envClientID
}
}
// Load client secret from environment or Secret Manager
if *clientSecret == "" {
ctx := context.Background()
*clientSecret = loadClientSecret(ctx)
}
if *redirectURI == defaultRedirectURI || *redirectURI == "" {
if envRedirectURI := os.Getenv("OAUTH_REDIRECT_URI"); envRedirectURI != "" {
*redirectURI = envRedirectURI
}
}
if *allowedOrigins == "" {
if envAllowedOrigins := os.Getenv("ALLOWED_ORIGINS"); envAllowedOrigins != "" {
*allowedOrigins = envAllowedOrigins
}
}
// Initialize rate limiter for auth code exchange (strict: 10 attempts per minute per IP)
exchangeRateLimiter = &rateLimiter{
requests: make(map[string][]time.Time),
limit: rateLimitRequests,
window: rateLimitWindow,
}
// Initialize CSRF protection using Go 1.25's CrossOriginProtection
// Uses Fetch Metadata (Sec-Fetch-Site header) for reliable cross-origin detection
csrfProtection = http.NewCrossOriginProtection()
// Trust requests from our own domain and all subdomains
if err := csrfProtection.AddTrustedOrigin("https://" + baseDomain); err != nil {
log.Fatalf("CRITICAL: Failed to configure CSRF protection for base domain: %v", err)
}
if err := csrfProtection.AddTrustedOrigin("https://*." + baseDomain); err != nil {
log.Fatalf("CRITICAL: Failed to configure CSRF protection for subdomains: %v", err)
}
// Allow localhost for development (covers all ports)
if err := csrfProtection.AddTrustedOrigin("http://localhost"); err != nil {
log.Fatalf("CRITICAL: Failed to configure CSRF protection for localhost: %v", err)
}
// Set up routes
mux := http.NewServeMux()
// OAuth endpoints
// Register API endpoints before catch-all to ensure they match first
// Auth code exchange has rate limiting + CSRF protection (Go 1.25 CrossOriginProtection)
mux.Handle("/oauth/exchange", csrfProtection.Handler(exchangeRateLimiter.limitHandler(handleExchangeAuthCode)))
mux.HandleFunc("/oauth/login", handleOAuthLogin)
mux.HandleFunc("/oauth/callback", handleOAuthCallback)
mux.HandleFunc("/oauth/user", handleGetUser)
// Health check endpoint
mux.HandleFunc("/health", handleHealthCheck)
// Serve everything else as SPA (including assets)
// This MUST be registered last as it's a catch-all
mux.HandleFunc("/", serveStaticFiles)
// Wrap with security middleware
handler := requestLogger(requestSizeLimiter(securityHeaders(mux)))
// Start server with graceful shutdown
addr := ":" + serverPort
srv := &http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: httpTimeout,
WriteTimeout: httpTimeout,
IdleTimeout: httpTimeout * 12, // 2 minutes
MaxHeaderBytes: maxHeaderSize,
}
log.Printf("Starting server on %s", addr)
log.Printf("GitHub App ID: %d", *appID)
log.Printf("OAuth Client ID: %s", *clientID)
log.Printf("OAuth Redirect URI: %s", *redirectURI)
if *clientSecret == "" {
log.Print("WARNING: OAuth Client Secret not set. OAuth login will not work.")
log.Print("Set GITHUB_CLIENT_SECRET environment variable or use --client-secret flag")
} else {
log.Print("OAuth Client Secret: configured")
}
// Start auth code cleanup goroutine
go func() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
authCodesMutex.Lock()
now := time.Now()
for code, data := range authCodes {
if now.After(data.expiry) {
delete(authCodes, code)
}
}
authCodesMutex.Unlock()
}
}()
// Start server in goroutine
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Server failed to start: %v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown: %v", err)
}
log.Println("Server exited")
}
func serveStaticFiles(w http.ResponseWriter, r *http.Request) {
// Only allow GET, HEAD, and OPTIONS methods
if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodOptions {
log.Printf("[serveStaticFiles] Rejecting %s request to %s (405)", r.Method, r.URL.Path)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Redirect base domain frontpage to codegroove.dev
currentHost := r.Header.Get("X-Original-Host")
if currentHost == "" {
currentHost = r.Host
}
// Check if this is the base domain (not a subdomain) and the frontpage
if strings.EqualFold(currentHost, baseDomain) && (r.URL.Path == "/" || r.URL.Path == "") {
http.Redirect(w, r, "https://codegroove.dev/reviewgoose/", http.StatusFound)
return
}
// CORS: Allow subdomains to load assets from naked domain
// Check Origin header and allow all subdomains of reviewGOOSE.dev
origin := r.Header.Get("Origin")
if origin != "" {
// Parse origin to validate it's one of our subdomains
if u, err := url.Parse(origin); err == nil {
host := strings.ToLower(u.Hostname())
baseDomainLower := strings.ToLower(baseDomain)
// Allow naked domain and all subdomains (case-insensitive)
if host == baseDomainLower || strings.HasSuffix(host, "."+baseDomainLower) {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type")
w.Header().Set("Vary", "Origin")
}
}
}
// Handle preflight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
// Clean the path
path := filepath.Clean(r.URL.Path)
// Prevent directory traversal
if strings.Contains(path, "..") || strings.Contains(path, "~") {
http.NotFound(w, r)
return
}
// Remove leading slash for embed.FS
if path == "/" || path == "." {
path = "index.html"
} else {
path = strings.TrimPrefix(path, "/")
}
// Try to read the file from embedded FS
data, err := staticFiles.ReadFile(path)
if err != nil {
// If file not found and not an asset, serve index.html for SPA routing
if !strings.HasPrefix(path, "assets/") && !strings.HasSuffix(path, ".ico") {
data, err = staticFiles.ReadFile("index.html")
if err != nil {
log.Printf("Failed to serve fallback index.html: %v", err)
http.Error(w, "Service temporarily unavailable", http.StatusServiceUnavailable)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
if _, err := w.Write(data); err != nil {
log.Printf("Failed to write response: %v", err)
}
return
}
http.NotFound(w, r)
return
}
// Set content type and cache headers based on file extension
switch {
case strings.HasSuffix(path, ".html"):
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Never cache HTML files - they contain BUILD_TIMESTAMP references to versioned assets
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
// Replace BUILD_TIMESTAMP placeholder with actual timestamp for cache busting
data = []byte(strings.ReplaceAll(string(data), "BUILD_TIMESTAMP", buildTimestamp))
case strings.HasSuffix(path, ".css"):
w.Header().Set("Content-Type", "text/css; charset=utf-8")
// Cache CSS for 1 year since URL includes version query param
if r.URL.Query().Get("v") != "" {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
case strings.HasSuffix(path, ".js"):
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
// Cache JS for 1 year since URL includes version query param
if r.URL.Query().Get("v") != "" {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
case strings.HasSuffix(path, ".json"):
w.Header().Set("Content-Type", "application/json; charset=utf-8")
case strings.HasSuffix(path, ".png"):
w.Header().Set("Content-Type", "image/png")
case strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"):
w.Header().Set("Content-Type", "image/jpeg")
case strings.HasSuffix(path, ".svg"):
w.Header().Set("Content-Type", "image/svg+xml")
case strings.HasSuffix(path, ".ico"):
w.Header().Set("Content-Type", "image/x-icon")
default:
// No specific content type
}
// Write the file content
if _, err := w.Write(data); err != nil {
log.Printf("Failed to write file content: %v", err)
}
}
// validateReturnToURL validates that a return_to URL is safe to redirect to.
// Returns the validated URL or empty string if invalid.
func validateReturnToURL(returnTo string) string {
if returnTo == "" {
return ""
}
parsedURL, err := url.Parse(returnTo)
if err != nil {
return ""
}
host := parsedURL.Hostname()
urlScheme := parsedURL.Scheme
// Only allow http/https schemes
switch urlScheme {
case "http", "https":
// Valid scheme, continue validation
default:
log.Printf("[SECURITY] Invalid return_to scheme: %s", urlScheme)
return ""
}
// Validate domain is ours
if host != baseDomain && !strings.HasSuffix(host, "."+baseDomain) {
log.Printf("[SECURITY] Invalid return_to domain: %s", host)
return ""
}
// Validate subdomain format if not base domain
if host != baseDomain {
parts := strings.Split(host, ".")
if len(parts) >= 3 {
subdomain := parts[0]
// Reserved subdomains that don't need GitHub handle validation
reservedSubdomains := []string{"www", "dash", "api", "login", "auth-callback", "my"}
isReserved := false
for _, reserved := range reservedSubdomains {
if strings.EqualFold(subdomain, reserved) {
isReserved = true
break
}
}
// Validate subdomain is a valid GitHub handle (prevents punycode, homograph attacks, etc.)
// unless it's a reserved subdomain
if !isReserved && !isValidGitHubHandle(subdomain) {
log.Printf("[SECURITY] Invalid GitHub handle in return_to subdomain: %s", subdomain)
return ""
}
}
}
return returnTo
}
func handleOAuthLogin(w http.ResponseWriter, r *http.Request) {
if *clientID == "" {
log.Print("OAuth login attempted but client ID not configured. Set GITHUB_CLIENT_ID environment variable or use --client-id flag")
http.Error(w, "Service temporarily unavailable", http.StatusServiceUnavailable)
return
}
// Get current host to determine return destination
currentHost := r.Header.Get("X-Original-Host")
if currentHost == "" {
currentHost = r.Host
}
isSecure := r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https"
scheme := "http"
if isSecure {
scheme = "https"
}
// If not on base domain, redirect there with return_to parameter
// Use case-insensitive comparison since DNS hostnames are case-insensitive
if !strings.EqualFold(currentHost, baseDomain) {
returnTo := fmt.Sprintf("%s://%s/", scheme, currentHost)
authURL := fmt.Sprintf("%s://%s/oauth/login?return_to=%s", scheme, baseDomain, url.QueryEscape(returnTo))
log.Printf("[OAuth] Redirecting to base domain for OAuth: %s", authURL)
http.Redirect(w, r, authURL, http.StatusFound)
return
}
// We're on base domain - proceed with OAuth flow
// Store return_to in state
returnTo := r.URL.Query().Get("return_to")
// Generate state for CSRF protection (include return_to)
stateData := generateID(16)
if returnTo != "" {
// Store return_to in cookie so callback can use it
returnCookie := &http.Cookie{
Name: "oauth_return_to",
Value: returnTo,
Path: "/",
HttpOnly: true,
Secure: isSecure,
SameSite: http.SameSiteLaxMode, // Lax required for OAuth redirect from GitHub
MaxAge: 900, // 15 minutes
}
http.SetCookie(w, returnCookie)
}
// Store state in cookie
stateCookie := &http.Cookie{
Name: "oauth_state",
Value: stateData,
Path: "/",
HttpOnly: true,
Secure: isSecure,
SameSite: http.SameSiteLaxMode, // Lax required for OAuth redirect from GitHub
Expires: time.Now().Add(stateExpiry),
}
http.SetCookie(w, stateCookie)
// Build authorization URL (always use reviewGOOSE.dev callback)
authURL := fmt.Sprintf(
"https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s",
url.QueryEscape(*clientID),
url.QueryEscape(*redirectURI),
url.QueryEscape("repo read:org"),
url.QueryEscape(stateData),
)
log.Printf("[OAuth] Starting OAuth with return_to=%s", returnTo)
http.Redirect(w, r, authURL, http.StatusFound)
}
func handleOAuthCallback(w http.ResponseWriter, r *http.Request) {
if *clientID == "" || *clientSecret == "" {
log.Printf("OAuth callback attempted but not configured: client_id=%q client_secret_set=%v",
*clientID, *clientSecret != "")
log.Print("Set GITHUB_CLIENT_SECRET environment variable or --client-secret flag")
http.Error(w, "Service temporarily unavailable", http.StatusServiceUnavailable)
return
}
// Check for OAuth errors from GitHub
if errCode := r.URL.Query().Get("error"); errCode != "" {
errDesc := r.URL.Query().Get("error_description")
log.Printf("OAuth error: %s - %s", errCode, errDesc)
// Return user-friendly error page
escapedMsg := strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
"\"", """,
"'", "'",
).Replace("Authentication was cancelled or failed. Please try again.")
html := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>Authentication Failed</title>
</head>
<body>
<h1>Authentication Failed</h1>
<p>%s</p>
<p>You can close this window and try again.</p>
</body>
</html>
`, escapedMsg)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if _, err := w.Write([]byte(html)); err != nil {
log.Printf("Failed to write error response: %v", err)
}
return
}
// Check if this is a GitHub App installation callback
installationID := r.URL.Query().Get("installation_id")
setupAction := r.URL.Query().Get("setup_action")
if installationID != "" && setupAction != "" {
// This is a GitHub App installation callback
log.Printf("GitHub App installation callback: installation_id=%s, setup_action=%s", installationID, setupAction)
// Return a success page for app installations
escapedAction := strings.NewReplacer("&", "&", "<", "<", ">", ">", "\"", """, "'", "'").Replace(setupAction)
escapedID := strings.NewReplacer("&", "&", "<", "<", ">", ">", "\"", """, "'", "'").Replace(installationID)
html := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>GitHub App Installation</title>
</head>
<body>
<h1>GitHub App Installed Successfully</h1>
<p>The GitHub App has been %s successfully.</p>
<p>Installation ID: %s</p>
<p>This window will close automatically in 3 seconds.</p>
<script>
// Auto-close after 3 seconds
setTimeout(function() {
window.close();
}, 3000);
</script>
</body>
</html>
`, escapedAction, escapedID)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if _, err := w.Write([]byte(html)); err != nil {
log.Printf("Failed to write GitHub App installation response: %v", err)
}
return
}
// Regular OAuth flow - verify state
state := r.URL.Query().Get("state")
if state == "" {
trackFailedAttempt(clientIP(r))
log.Printf("[OAuth] Missing state parameter from %s", clientIP(r))
clearStateCookie(w)
http.Error(w, "Missing state parameter", http.StatusBadRequest)
return
}
cookie, err := r.Cookie("oauth_state")
if err != nil {
trackFailedAttempt(clientIP(r))
log.Printf("[OAuth] Missing oauth_state cookie from %s: %v", clientIP(r), err)
log.Printf("[OAuth] Available cookies: %d present", len(r.Cookies()))
clearStateCookie(w)
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
// Use constant-time comparison to prevent timing attacks
if subtle.ConstantTimeCompare([]byte(cookie.Value), []byte(state)) != 1 {
trackFailedAttempt(clientIP(r))
log.Printf("[OAuth] State mismatch from %s", clientIP(r))
clearStateCookie(w)
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
log.Printf("[OAuth] State validation successful for %s", clientIP(r))
// Get authorization code
code := r.URL.Query().Get("code")
if code == "" || len(code) > 512 {
trackFailedAttempt(clientIP(r))
clearStateCookie(w)
http.Error(w, "Invalid authorization code", http.StatusBadRequest)
return
}
// Exchange code for token (use registered callback URI)
ctx := r.Context()
token, err := exchangeCodeForToken(ctx, code, *redirectURI)
if err != nil {
trackFailedAttempt(clientIP(r))
log.Printf("Failed to exchange code for token: %v", err)
http.Error(w, "Authentication failed", http.StatusInternalServerError)
return
}
// Fetch username to determine personal workspace
user, err := userInfo(ctx, token)
if err != nil {
log.Printf("Failed to get user info after OAuth: %v", err)
http.Error(w, "Failed to get user info", http.StatusInternalServerError)
return
}
// Validate username format
if !isValidGitHubHandle(user.Login) {
log.Printf("[SECURITY] Invalid username format from GitHub OAuth: %s", user.Login)
http.Error(w, "Invalid username format", http.StatusBadRequest)
return
}
// Clear the state cookie after all validations pass
clearStateCookie(w)
// Get return_to from cookie
returnTo := ""
if returnCookie, err := r.Cookie("oauth_return_to"); err == nil && returnCookie.Value != "" {
returnTo = returnCookie.Value
// Clear the return_to cookie
http.SetCookie(w, &http.Cookie{
Name: "oauth_return_to",
Value: "",
Path: "/",
MaxAge: -1,
HttpOnly: true,
})
}
// Validate return_to URL
isSecure := r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https"
scheme := "http"
if isSecure {
scheme = "https"
}
// Validate and use return_to URL, or default to personal workspace (my subdomain)
redirectURL := validateReturnToURL(returnTo)
if redirectURL == "" {
redirectURL = fmt.Sprintf("%s://my.%s", scheme, baseDomain)
}
// Create one-time auth code for secure token transfer
authCode := generateID(32)
authCodesMutex.Lock()
authCodes[authCode] = authCodeData{
token: token,
username: user.Login,
expiry: time.Now().Add(10 * time.Second), // Short-lived (10s sufficient for modern browsers)
returnTo: redirectURL,
used: false,
}
authCodesMutex.Unlock()
// Redirect with one-time auth code in fragment (not sent to server)
// Fragment identifiers are not sent in Referer headers or logged by servers
redirectWithCode := fmt.Sprintf("%s#auth_code=%s", redirectURL, url.QueryEscape(authCode))
log.Printf("[OAuth] Redirecting to %s with one-time auth code (in fragment)", sanitizeURL(redirectURL))
http.Redirect(w, r, redirectWithCode, http.StatusFound)
}
func handleExchangeAuthCode(w http.ResponseWriter, r *http.Request) {
// Record start time for constant-time responses (prevent timing attacks)
startTime := time.Now()
// Minimum response time: 50ms to prevent timing-based code validity detection
const minResponseTime = 50 * time.Millisecond
// Ensure constant-time response at function exit
defer func() {
elapsed := time.Since(startTime)
if elapsed < minResponseTime {
time.Sleep(minResponseTime - elapsed)
}
}()
log.Printf("[handleExchangeAuthCode] Called with method=%s path=%s", r.Method, r.URL.Path)
if r.Method != http.MethodPost {
log.Printf("[handleExchangeAuthCode] Rejecting non-POST request: %s", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// CSRF Protection is handled by Go 1.25's CrossOriginProtection middleware (wraps this handler)
// It uses Fetch Metadata (Sec-Fetch-Site header) which is more reliable than Origin header
// Get auth code from request
var req struct {
AuthCode string `json:"auth_code"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
if req.AuthCode == "" {
http.Error(w, "Missing auth_code", http.StatusBadRequest)
return
}
// Atomically validate and consume auth code (all checks under single lock to prevent TOCTOU race)
authCodesMutex.Lock()
data, exists := authCodes[req.AuthCode]
// Perform all validation checks before releasing lock
if !exists {
authCodesMutex.Unlock()
log.Printf("[OAuth] Invalid or expired auth code from %s", clientIP(r))
http.Error(w, "Invalid or expired auth code", http.StatusUnauthorized)
return
}
if data.used {
authCodesMutex.Unlock()
log.Printf("[SECURITY] Attempt to reuse auth code from %s", clientIP(r))
http.Error(w, "Auth code already used", http.StatusUnauthorized)
return
}
if time.Now().After(data.expiry) {
authCodesMutex.Unlock()
log.Printf("[OAuth] Expired auth code from %s", clientIP(r))
http.Error(w, "Auth code expired", http.StatusUnauthorized)
return
}
// All validations passed - atomically delete the auth code before releasing lock
delete(authCodes, req.AuthCode)
authCodesMutex.Unlock()
// Return token and username
response := struct {
Token string `json:"token"`
Username string `json:"username"`
}{
Token: data.token,
Username: data.username,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Printf("Failed to encode auth exchange response: %v", err)
}
log.Printf("[OAuth] Successfully exchanged auth code for user %s", data.username)
}
func handleGetUser(w http.ResponseWriter, r *http.Request) {
// Get token from Authorization header
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "Missing authorization header", http.StatusUnauthorized)
return
}
token := strings.TrimPrefix(authHeader, "Bearer ")
if token == authHeader {
http.Error(w, "Invalid authorization header", http.StatusUnauthorized)
return
}
// Get user info from GitHub
ctx := r.Context()
user, err := userInfo(ctx, token)
if err != nil {