Skip to content

Commit e5f80b8

Browse files
committed
fix: set global options as environment variables for security
Signed-off-by: Donnie Adams <[email protected]>
1 parent 39e4e76 commit e5f80b8

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

gptscript.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
4949
defer lock.Unlock()
5050
gptscriptCount++
5151

52-
if serverURL == "" {
52+
disableServer := os.Getenv("GPT_SCRIPT_DISABLE_SERVER") == "true"
53+
54+
if serverURL == "" && disableServer {
5355
serverURL = os.Getenv("GPTSCRIPT_URL")
5456
}
5557

56-
if serverProcessCancel == nil && os.Getenv("GPTSCRIPT_DISABLE_SERVER") != "true" {
58+
if serverProcessCancel == nil && !disableServer {
5759
if serverURL == "" {
5860
l, err := net.Listen("tcp", "127.0.0.1:0")
5961
if err != nil {
@@ -71,7 +73,8 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
7173
ctx, cancel := context.WithCancel(context.Background())
7274

7375
in, _ := io.Pipe()
74-
serverProcess = exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "--listen-address", serverURL, "sdkserver")...)
76+
serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver")
77+
serverProcess.Env = append(os.Environ(), opts.toEnv()...)
7578
serverProcess.Stdin = in
7679

7780
serverProcessCancel = func() {

gptscript_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
2020
}
2121

2222
var err error
23-
g, err = NewGPTScript(GlobalOptions{})
23+
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY")})
2424
if err != nil {
2525
panic(fmt.Sprintf("error creating gptscript: %s", err))
2626
}

opts.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ type GlobalOptions struct {
88
DefaultModel string `json:"DefaultModel"`
99
}
1010

11-
func (g GlobalOptions) toArgs() []string {
11+
func (g GlobalOptions) toEnv() []string {
1212
var args []string
1313
if g.OpenAIAPIKey != "" {
14-
args = append(args, "--openai-api-key", g.OpenAIAPIKey)
14+
args = append(args, "OPENAI_API_KEY="+g.OpenAIAPIKey)
1515
}
1616
if g.OpenAIBaseURL != "" {
17-
args = append(args, "--openai-base-url", g.OpenAIBaseURL)
17+
args = append(args, "OPENAI_BASE_URL="+g.OpenAIBaseURL)
1818
}
1919
if g.DefaultModel != "" {
20-
args = append(args, "--default-model", g.DefaultModel)
20+
args = append(args, "GPTSCRIPT_DEFAULT_MODEL="+g.DefaultModel)
2121
}
2222

2323
return args

0 commit comments

Comments
 (0)