-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from unkn0wn-root/verify_api_hostname
feat(api): add hostname verify middleware
- Loading branch information
Showing
5 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
api: | ||
enabled: true | ||
host: localhost | ||
port: 8081 | ||
host: admin.domain.com | ||
port: 8085 | ||
|
||
database: | ||
path: "./auth.db" | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package admin | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
|
||
"github.com/unkn0wn-root/terraster/internal/middleware" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// HostnameMiddleware validates incoming requests against a configured hostname | ||
type HostnameMiddleware struct { | ||
hostname string // Expected hostname to validate against | ||
logger *zap.Logger | ||
} | ||
|
||
func NewHostnameMiddleware(hostname string, logger *zap.Logger) middleware.Middleware { | ||
return &HostnameMiddleware{ | ||
hostname: hostname, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (m *HostnameMiddleware) Middleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Extract hostname from request, ignoring port number | ||
host, _, err := net.SplitHostPort(r.Host) | ||
if err != nil { | ||
m.logger.Error("Could not split host", zap.Error(err)) | ||
http.Error(w, "Could not verify target host", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// If does not match - you shall not pass | ||
if host != m.hostname { | ||
m.logger.Warn("Invalid hostname", | ||
zap.String("expected", m.hostname), | ||
zap.String("received", host), | ||
zap.String("ip", r.RemoteAddr), | ||
) | ||
http.Error(w, "Invalid host", http.StatusForbidden) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
23 changes: 17 additions & 6 deletions
23
internal/admin/middleware.go → internal/admin/middleware/log.go
This file contains 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
This file contains 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