-
Notifications
You must be signed in to change notification settings - Fork 458
fix: Accurate session count to avoid constant upward drift #1348
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
base: dev
Are you sure you want to change the base?
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,25 @@ | ||||||||||||||||||
| package server | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "bytes" | ||||||||||||||||||
| "crypto/rand" | ||||||||||||||||||
| "crypto/rsa" | ||||||||||||||||||
| "crypto/tls" | ||||||||||||||||||
| "crypto/x509" | ||||||||||||||||||
| "encoding/base64" | ||||||||||||||||||
| "encoding/pem" | ||||||||||||||||||
| "io" | ||||||||||||||||||
| "net/http" | ||||||||||||||||||
| "net/http/httptest" | ||||||||||||||||||
| "sync" | ||||||||||||||||||
| "sync/atomic" | ||||||||||||||||||
| "testing" | ||||||||||||||||||
| "time" | ||||||||||||||||||
|
|
||||||||||||||||||
| "github.com/google/uuid" | ||||||||||||||||||
| jsoniter "github.com/json-iterator/go" | ||||||||||||||||||
| "github.com/projectdiscovery/interactsh/pkg/storage" | ||||||||||||||||||
| "github.com/rs/xid" | ||||||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -70,3 +82,72 @@ func TestWriteResponseFromDynamicRequest(t *testing.T) { | |||||||||||||||||
| require.Equal(t, resp.Header.Get("Test"), "Another", "could not get correct result") | ||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func TestSessionTotalMetric(t *testing.T) { | ||||||||||||||||||
| stats := &Metrics{} | ||||||||||||||||||
| removed := make(chan struct{}) | ||||||||||||||||||
| closeOnce := sync.Once{} | ||||||||||||||||||
|
|
||||||||||||||||||
| store, err := storage.New(&storage.Options{ | ||||||||||||||||||
| EvictionTTL: 5 * time.Minute, | ||||||||||||||||||
| OnRemoval: func() { | ||||||||||||||||||
| atomic.AddInt64(&stats.Sessions, -1) | ||||||||||||||||||
| closeOnce.Do(func() { close(removed) }) | ||||||||||||||||||
| }, | ||||||||||||||||||
| }) | ||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||
| defer store.Close() | ||||||||||||||||||
|
|
||||||||||||||||||
| h := &HTTPServer{ | ||||||||||||||||||
| options: &Options{ | ||||||||||||||||||
| Storage: store, | ||||||||||||||||||
| Stats: stats, | ||||||||||||||||||
| }, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Generate a client key pair and registration payload. | ||||||||||||||||||
| priv, err := rsa.GenerateKey(rand.Reader, 2048) | ||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||
| pubBytes, err := x509.MarshalPKIXPublicKey(&priv.PublicKey) | ||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||
| pubPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: pubBytes}) | ||||||||||||||||||
| pubB64 := base64.StdEncoding.EncodeToString(pubPem) | ||||||||||||||||||
|
Comment on lines
+111
to
+114
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Verify what PEM types are accepted by register path key parsing.
rg -n -C3 'SetIDPublicKey|pem\.Decode|ParsePKIXPublicKey|ParsePKCS1PublicKey|block\.Type|PUBLIC KEY|RSA PUBLIC KEY'Repository: projectdiscovery/interactsh Length of output: 9821 Use PKIX-consistent PEM block type on line 111.
Proposed fix- pubPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: pubBytes})
+ pubPem := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes})📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| correlationID := xid.New().String() | ||||||||||||||||||
| secretKey := uuid.New().String() | ||||||||||||||||||
|
|
||||||||||||||||||
| // --- Register --- | ||||||||||||||||||
| regBody, err := jsoniter.Marshal(&RegisterRequest{ | ||||||||||||||||||
| PublicKey: pubB64, | ||||||||||||||||||
| SecretKey: secretKey, | ||||||||||||||||||
| CorrelationID: correlationID, | ||||||||||||||||||
| }) | ||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||
| req := httptest.NewRequest("POST", "/register", bytes.NewReader(regBody)) | ||||||||||||||||||
| w := httptest.NewRecorder() | ||||||||||||||||||
| h.registerHandler(w, req) | ||||||||||||||||||
| require.Equal(t, http.StatusOK, w.Code) | ||||||||||||||||||
|
|
||||||||||||||||||
| require.Equal(t, int64(1), atomic.LoadInt64(&stats.Sessions), "sessions should be 1 after register") | ||||||||||||||||||
| require.Equal(t, int64(1), atomic.LoadInt64(&stats.SessionsTotal), "sessions_total should be 1 after register") | ||||||||||||||||||
|
|
||||||||||||||||||
| // --- Deregister --- | ||||||||||||||||||
| deregBody, err := jsoniter.Marshal(&DeregisterRequest{ | ||||||||||||||||||
| SecretKey: secretKey, | ||||||||||||||||||
| CorrelationID: correlationID, | ||||||||||||||||||
| }) | ||||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||||
| req = httptest.NewRequest("POST", "/deregister", bytes.NewReader(deregBody)) | ||||||||||||||||||
| w = httptest.NewRecorder() | ||||||||||||||||||
| h.deregisterHandler(w, req) | ||||||||||||||||||
| require.Equal(t, http.StatusOK, w.Code) | ||||||||||||||||||
|
|
||||||||||||||||||
| select { | ||||||||||||||||||
| case <-removed: | ||||||||||||||||||
| case <-time.After(2 * time.Second): | ||||||||||||||||||
| t.Fatal("timed out waiting for OnRemoval callback") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| require.Equal(t, int64(0), atomic.LoadInt64(&stats.Sessions), "sessions should be 0 after deregister") | ||||||||||||||||||
| require.Equal(t, int64(1), atomic.LoadInt64(&stats.SessionsTotal), "sessions_total should remain 1 after deregister") | ||||||||||||||||||
| } | ||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.