Skip to content

fix: set global options as environment variables for security #28

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

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
9 changes: 6 additions & 3 deletions gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
defer lock.Unlock()
gptscriptCount++

if serverURL == "" {
disableServer := os.Getenv("GPT_SCRIPT_DISABLE_SERVER") == "true"

if serverURL == "" && disableServer {
serverURL = os.Getenv("GPTSCRIPT_URL")
}

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

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

serverProcessCancel = func() {
Expand Down
8 changes: 4 additions & 4 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ type GlobalOptions struct {
DefaultModel string `json:"DefaultModel"`
}

func (g GlobalOptions) toArgs() []string {
func (g GlobalOptions) toEnv() []string {
var args []string
if g.OpenAIAPIKey != "" {
args = append(args, "--openai-api-key", g.OpenAIAPIKey)
args = append(args, "OPENAI_API_KEY="+g.OpenAIAPIKey)
}
if g.OpenAIBaseURL != "" {
args = append(args, "--openai-base-url", g.OpenAIBaseURL)
args = append(args, "OPENAI_BASE_URL="+g.OpenAIBaseURL)
}
if g.DefaultModel != "" {
args = append(args, "--default-model", g.DefaultModel)
args = append(args, "GPTSCRIPT_DEFAULT_MODEL="+g.DefaultModel)
}

return args
Expand Down