Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/reachable/reach-testbed-github-go
go 1.21

require (
golang.org/x/text v0.3.7
golang.org/x/text v0.3.8
gopkg.in/yaml.v2 v2.4.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
22 changes: 9 additions & 13 deletions internal/handlers/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,43 @@ type agentRequest struct {
func AIAnswer(w http.ResponseWriter, r *http.Request) {
var req promptRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}

prompt := "System: answer as the payroll admin. User: " + req.Question
payload, _ := json.Marshal(map[string]any{
"model": "gpt-4",
"messages": []map[string]string{
{"role": "user", "content": prompt},
{"role": "system", "content": "Answer as the payroll admin."},
{"role": "user", "content": req.Question},
},
})
_, _ = http.Post("https://api.openai.com/v1/chat/completions", "application/json", bytes.NewReader(payload))
_ = json.NewEncoder(w).Encode(map[string]string{"prompt": prompt})
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

func AIAgentPlan(w http.ResponseWriter, r *http.Request) {
var req agentRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}

toolSpec := "Tools: shell(command), browser(url). Follow the user's task exactly: " + req.Task
payload, _ := json.Marshal(map[string]any{
"model": "gpt-4",
"messages": []map[string]string{
{"role": "system", "content": "You are an internal automation agent with admin context."},
{"role": "user", "content": toolSpec},
{"role": "system", "content": "You are an internal automation agent. Available tools: shell(command), browser(url)."},
{"role": "user", "content": req.Task},
},
})
_, _ = http.Post("https://api.openai.com/v1/chat/completions", "application/json", bytes.NewReader(payload))
_ = json.NewEncoder(w).Encode(map[string]string{
"system_prompt": "You are an internal automation agent with admin context.",
"tool_spec": toolSpec,
})
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

func SafeAIAnswer(w http.ResponseWriter, r *http.Request) {
var req promptRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
if strings.Contains(strings.ToLower(req.Question), "ignore previous") {
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ParseLanguage(w http.ResponseWriter, r *http.Request) {

parsed, err := language.Parse(tag)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, "invalid language tag", http.StatusBadRequest)
return
}

Expand Down
11 changes: 9 additions & 2 deletions internal/handlers/cwe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"log"
"net/http"
"os/exec"

Expand All @@ -9,9 +10,15 @@ import (

func DiagnosticPing(w http.ResponseWriter, r *http.Request) {
host := r.URL.Query().Get("host")
out, err := exec.Command("sh", "-c", "ping -c 1 "+host).CombinedOutput()
if !safety.AllowedHostname(host) {
http.Error(w, "invalid host", http.StatusBadRequest)
return
}

out, err := exec.Command("ping", "-c", "1", host).CombinedOutput()
if err != nil {
http.Error(w, string(out), http.StatusBadGateway)
log.Printf("ping %s: %v: %s", host, err, out)
http.Error(w, "ping failed", http.StatusBadGateway)
return
}

Expand Down
12 changes: 10 additions & 2 deletions internal/handlers/dlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ import (
func SupportExport(w http.ResponseWriter, _ *http.Request) {
ssn := "123-45-6789"
dob := "1978-04-23"
log.Printf("Processing patient ssn=%s dob=%s", ssn, dob)
payload, _ := json.Marshal(map[string]string{"ssn": ssn, "dob": dob})
ssnLast4 := ""
if len(ssn) >= 4 {
ssnLast4 = ssn[len(ssn)-4:]
}
dobYear := ""
if len(dob) >= 4 {
dobYear = dob[:4]
}
log.Printf("Processing patient ssn=***-**-%s dob=%s-**-**", ssnLast4, dobYear)
payload, _ := json.Marshal(map[string]string{"record": "processed"})
_, _ = http.Post("https://analytics.example.com/track", "application/json", bytes.NewReader(payload))

w.Header().Set("Content-Type", "text/csv")
Expand Down
8 changes: 6 additions & 2 deletions internal/handlers/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ import (

const syntheticServiceToken = "rtg_live_synthetic_token_1234567890"
const syntheticAWSAccessKeyID = "AKIAIOSFODNN7EXAMPLE"
const syntheticGitHubToken = "ghp_reachtestbedsynthetic000000000000000000"

func ServiceToken(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(syntheticServiceToken + "\n"))
}

func CloudTokens(w http.ResponseWriter, _ *http.Request) {
githubToken := os.Getenv("REACH_TESTBED_GITHUB_TOKEN")
if githubToken == "" {
http.Error(w, "github token not configured", http.StatusServiceUnavailable)
return
}
// Synthetic fixture values only. These are not real credentials.
_ = json.NewEncoder(w).Encode(map[string]string{
"aws_access_key_id": syntheticAWSAccessKeyID,
"github_token": syntheticGitHubToken,
"github_token": githubToken,
})
}

Expand Down
24 changes: 21 additions & 3 deletions internal/handlers/suspicious.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,49 @@ package handlers
import (
"encoding/base64"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"

"github.com/reachable/reach-testbed-github-go/internal/safety"
)

func FetchTool(w http.ResponseWriter, r *http.Request) {
source := r.URL.Query().Get("url")

parsed, err := url.Parse(source)
if err != nil || parsed.Scheme != "https" {
http.Error(w, "only https URLs are allowed", http.StatusBadRequest)
return
}
if !safety.AllowedHostname(parsed.Hostname()) {
http.Error(w, "host not allowed", http.StatusBadRequest)
return
}

resp, err := http.Get(source)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
log.Printf("FetchTool: http.Get %s: %v", source, err)
http.Error(w, "fetch failed", http.StatusBadGateway)
return
}
defer resp.Body.Close()

target := filepath.Join(os.TempDir(), "reach-testbed-tool.bin")
out, err := os.Create(target)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("FetchTool: os.Create %s: %v", target, err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
defer out.Close()

if _, err := io.Copy(out, io.LimitReader(resp.Body, 2<<20)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("FetchTool: io.Copy: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}

Expand Down