Skip to content

Commit c5d85f1

Browse files
authored
chore: openapi: remove https restriction (#916)
Signed-off-by: Grant Linville <[email protected]>
1 parent c39a069 commit c5d85f1

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

docs/docs/03-tools/03-openapi.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ Will be resolved as `https://api.example.com/v1`.
4141

4242
## Authentication
4343

44-
:::warning
45-
All authentication options will be completely ignored if the server uses HTTP and not HTTPS, unless the request is for `localhost` or 127.0.0.1.
46-
This is to protect users from accidentally sending credentials in plain text.
47-
HTTP is only OK, if it's on localhost/127.0.0.1.
48-
:::
49-
5044
### 1. Security Schemes
5145

5246
GPTScript will read the defined [security schemes](https://swagger.io/docs/specification/authentication/) in the OpenAPI definition. The currently supported types are `apiKey` and `http`.

pkg/engine/openapi.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,17 @@ func (e *Engine) runOpenAPI(tool types.Tool, input string) (*Return, error) {
197197
return nil, fmt.Errorf("failed to create request: %w", err)
198198
}
199199

200-
// Check for authentication (only if using HTTPS or localhost)
201-
if u.Scheme == "https" || u.Hostname() == "localhost" || u.Hostname() == "127.0.0.1" {
202-
if len(instructions.SecurityInfos) > 0 {
203-
if err := openapi.HandleAuths(req, envMap, instructions.SecurityInfos); err != nil {
204-
return nil, fmt.Errorf("error setting up authentication: %w", err)
205-
}
200+
// Check for authentication
201+
if len(instructions.SecurityInfos) > 0 {
202+
if err := openapi.HandleAuths(req, envMap, instructions.SecurityInfos); err != nil {
203+
return nil, fmt.Errorf("error setting up authentication: %w", err)
206204
}
205+
}
207206

208-
// If there is a bearer token set for the whole server, and no Authorization header has been defined, use it.
209-
if token, ok := envMap["GPTSCRIPT_"+env.ToEnvLike(u.Hostname())+"_BEARER_TOKEN"]; ok {
210-
if req.Header.Get("Authorization") == "" {
211-
req.Header.Set("Authorization", "Bearer "+token)
212-
}
207+
// If there is a bearer token set for the whole server, and no Authorization header has been defined, use it.
208+
if token, ok := envMap["GPTSCRIPT_"+env.ToEnvLike(u.Hostname())+"_BEARER_TOKEN"]; ok {
209+
if req.Header.Get("Authorization") == "" {
210+
req.Header.Set("Authorization", "Bearer "+token)
213211
}
214212
}
215213

pkg/openapi/run.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"mime/multipart"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"strings"
1312

1413
"github.com/getkin/kin-openapi/openapi3"
@@ -69,22 +68,18 @@ func Run(operationID, defaultHost, args string, t *openapi3.T, envs []string) (s
6968
return "", false, fmt.Errorf("failed to create request: %w", err)
7069
}
7170

72-
// Check for authentication (only if using HTTPS or localhost)
73-
if u.Scheme == "https" || u.Hostname() == "localhost" || u.Hostname() == "127.0.0.1" {
74-
if len(opInfo.SecurityInfos) > 0 {
75-
if err := HandleAuths(req, envMap, opInfo.SecurityInfos); err != nil {
76-
return "", false, fmt.Errorf("error setting up authentication: %w", err)
77-
}
71+
// Check for authentication
72+
if len(opInfo.SecurityInfos) > 0 {
73+
if err := HandleAuths(req, envMap, opInfo.SecurityInfos); err != nil {
74+
return "", false, fmt.Errorf("error setting up authentication: %w", err)
7875
}
76+
}
7977

80-
// If there is a bearer token set for the whole server, and no Authorization header has been defined, use it.
81-
if token, ok := envMap["GPTSCRIPT_"+env.ToEnvLike(u.Hostname())+"_BEARER_TOKEN"]; ok {
82-
if req.Header.Get("Authorization") == "" {
83-
req.Header.Set("Authorization", "Bearer "+token)
84-
}
78+
// If there is a bearer token set for the whole server, and no Authorization header has been defined, use it.
79+
if token, ok := envMap["GPTSCRIPT_"+env.ToEnvLike(u.Hostname())+"_BEARER_TOKEN"]; ok {
80+
if req.Header.Get("Authorization") == "" {
81+
req.Header.Set("Authorization", "Bearer "+token)
8582
}
86-
} else {
87-
fmt.Fprintf(os.Stderr, "no auth")
8883
}
8984

9085
// Handle query parameters

0 commit comments

Comments
 (0)