Repository navigation
Send X-Agent headers with requests to Aikido #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package aikido_types | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // The custom Go toolchain we use for Alpine has no uuid package | ||
| func newSessionID() string { | ||
| var b [16]byte | ||
| if _, err := rand.Read(b[:]); err != nil { | ||
| return "unknown" | ||
| } | ||
|
|
||
| // 48-bit millisecond timestamp, big-endian | ||
| ms := time.Now().UnixMilli() | ||
| for i := range 6 { | ||
| b[i] = byte(ms >> (8 * (5 - i)) & 0xff) //nolint:gosec // masked to a single byte | ||
| } | ||
|
|
||
| b[6] = (b[6] & 0x0f) | 0x70 // version 7 | ||
| b[8] = (b[8] & 0x3f) | 0x80 // RFC 9562 variant | ||
|
|
||
| return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package aikido_types | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNewSessionID(t *testing.T) { | ||
| before := time.Now().UnixMilli() | ||
| id := newSessionID() | ||
| after := time.Now().UnixMilli() | ||
|
|
||
| assert.Regexp(t, `^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`, id) | ||
|
|
||
| ms, err := strconv.ParseInt(id[0:8]+id[9:13], 16, 64) | ||
| assert.NoError(t, err) | ||
| assert.GreaterOrEqual(t, ms, before) | ||
| assert.LessOrEqual(t, ms, after) | ||
|
|
||
| assert.NotEqual(t, id, newSessionID()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| package cloud | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "encoding/json" | ||
| . "main/aikido_types" | ||
| "main/constants" | ||
| "main/globals" | ||
| "main/log" | ||
| "main/utils" | ||
| "net/http" | ||
| "regexp" | ||
| "runtime" | ||
| "strings" | ||
|
|
@@ -34,6 +36,15 @@ func GetAgentInfo(server *ServerData) AgentInfo { | |
| } | ||
| } | ||
|
|
||
| func setAgentHeaders(req *http.Request, server *ServerData) { | ||
| req.Header.Set("X-Agent-Platform", "php") | ||
| req.Header.Set("X-Agent-Library", "firewall-php") | ||
| req.Header.Set("X-Agent-Version", constants.Version) | ||
| req.Header.Set("X-Agent-Hostname", cmp.Or(globals.Machine.HostName, "unknown")) | ||
| req.Header.Set("X-Agent-IP-Address", cmp.Or(globals.Machine.IPAddress, "unknown")) | ||
| req.Header.Set("X-Agent-Session-Id", server.SessionID) | ||
| } | ||
|
Comment on lines
+39
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low - Redirects disclose the new agent identity headers cross-host When either configured Aikido endpoint returns a redirect to another host, the default HTTP clients follow it and retain these newly added X-Agent headers. The redirect destination can therefore receive the machine hostname, local IP address, and stable session identifier even though Go normally removes the Authorization header on a cross-host redirect. Show fixDisable cross-host redirects for cloud requests, or validate the final host and remove the X-Agent headers before following a redirect to a different origin. More info - Reply on this comment to give feedback or ignore the issue. |
||
|
|
||
| func ResetHeartbeatTicker(server *ServerData) { | ||
| if !server.CloudConfig.ReceivedAnyStats { | ||
| log.Info(server.Logger, "Resetting HeartbeatTicker to 1m!") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not reachable but fine to keep idiomatic go error checking