Skip to content

Commit

Permalink
Fixed missing parameter injection problem for custom_action API
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Feb 18, 2025
1 parent 2d5a5b6 commit 1378d2c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 67 deletions.
2 changes: 1 addition & 1 deletion blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func GetAppCategories() []AppCategory {
Name: "AI",
Color: "#FFC107",
Icon: "AI",
ActionLabels: []string{"Answer Question", "Run Action"},
ActionLabels: []string{"Answer Question", "Run Action", "Run LLM"},
},
AppCategory{
Name: "Other",
Expand Down
2 changes: 1 addition & 1 deletion codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func MakePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
// This is gibberish :)
for _, param := range parameters {
if strings.Contains(param, "headers=") {
headerParserCode = "if len(headers) > 0:\n for header in headers.split(\"\\n\"):\n if ':' in header:\n headersplit=header.split(':')\n request_headers[headersplit[0].strip()] = ':'.join(headersplit[1:]).strip()\n elif '=' in header:\n headersplit=header.split('=')\n request_headers[headersplit[0].strip()] = '='.join(headersplit[1:]).strip()"
headerParserCode = "if isinstance(headers, dict):\n request_headers = headers\n elif len(headers) > 0:\n for header in str(headers).split(\"\\n\"):\n if ':' in header:\n headersplit=header.split(':')\n request_headers[headersplit[0].strip()] = ':'.join(headersplit[1:]).strip()\n elif '=' in header:\n headersplit=header.split('=')\n request_headers[headersplit[0].strip()] = '='.join(headersplit[1:]).strip()"

} else if strings.Contains(param, "queries=") {
queryParserCode = "\n if len(queries) > 0:\n if queries[0] == \"?\" or queries[0] == \"&\":\n queries = queries[1:len(queries)]\n if queries[len(queries)-1] == \"?\" or queries[len(queries)-1] == \"&\":\n queries = queries[0:-1]\n for query in queries.split(\"&\"):\n if isinstance(query, list) or isinstance(query, dict):\n try:\n query = json.dumps(query)\n except:\n pass\n if '=' in query:\n headersplit=query.split('=')\n params[requests.utils.quote(headersplit[0].strip())] = requests.utils.quote(headersplit[1].strip())\n else:\n params[requests.utils.quote(query.strip())] = None\n params = '&'.join([k if v is None else f\"{k}={v}\" for k, v in params.items()])"
Expand Down
6 changes: 3 additions & 3 deletions kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

//var model = "gpt-4-turbo-preview"
//var model = "gpt-4o"
var model = "gpt-4o-mini"
//var model = "gpt-4o-mini"
var model = "gpt-4o"

func GetKmsCache(ctx context.Context, auth AppAuthenticationStorage, key string) (string, error) {
//log.Printf("\n\n[DEBUG] Getting KMS cache for key %s\n\n", key)
Expand Down Expand Up @@ -1449,7 +1449,7 @@ func AutofixAppLabels(app WorkflowApp, label string) WorkflowApp {
}

if len(actionStruct.Action) == 0 {
log.Printf("[ERROR] No action found for app %s (%s) based on label %s (1)", app.Name, app.ID, label)
log.Printf("[ERROR] From LLM auto-label: No action found for app %s (%s) based on label %s (1). Output: %s", app.Name, app.ID, label, string(output))
//return app
} else {
newname := strings.Trim(strings.ToLower(strings.Replace(GetCorrectActionName(actionStruct.Action), " ", "_", -1)), " ")
Expand Down
142 changes: 80 additions & 62 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -18026,69 +18026,73 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by

// FIXME: We need to inject missing empty auth here in some cases
// This is NOT a good solution, but a good bypass
if app.Authentication.Required && len(action.AuthenticationId) == 0 {
authFields := 0
foundFields := []string{}
for _, actionParam := range action.Parameters {
if actionParam.Configuration {
authFields += 1
foundFields = append(foundFields, actionParam.Name)
if app.Authentication.Required {
if len(action.AuthenticationId) > 0 {
log.Printf("[INFO] Found auth ID for single action: %s", action.AuthenticationId)
} else {
authFields := 0
foundFields := []string{}
for _, actionParam := range action.Parameters {
if actionParam.Configuration {
authFields += 1
foundFields = append(foundFields, actionParam.Name)
}
}
}

// Usually url
if authFields <= 2 {
if !ArrayContains(foundFields, "apikey") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "apikey",
Configuration: true,
})
}
// Usually url
if authFields <= 2 {
if !ArrayContains(foundFields, "apikey") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "apikey",
Configuration: true,
})
}

if !ArrayContains(foundFields, "access_token") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "access_token",
Configuration: true,
})
}
if !ArrayContains(foundFields, "access_token") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "access_token",
Configuration: true,
})
}

if !ArrayContains(foundFields, "username_basic") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "username_basic",
Configuration: true,
})
}
if !ArrayContains(foundFields, "username_basic") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "username_basic",
Configuration: true,
})
}

if !ArrayContains(foundFields, "password_basic") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "password_basic",
Configuration: true,
})
if !ArrayContains(foundFields, "password_basic") {
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
Name: "password_basic",
Configuration: true,
})
}
}
}

auths, err := GetAllWorkflowAppAuth(ctx, user.ActiveOrg.Id)
if err != nil {
log.Printf("[ERROR] Failed getting auth for single action: %s", err)
} else {
//latestTimestamp := int64(0)
for _, auth := range auths {
if auth.App.ID != appId {
continue
}
auths, err := GetAllWorkflowAppAuth(ctx, user.ActiveOrg.Id)
if err != nil {
log.Printf("[ERROR] Failed getting auth for single action: %s", err)
} else {
//latestTimestamp := int64(0)
for _, auth := range auths {
if auth.App.ID != appId {
continue
}

// Fallback to latest created
/*
if latestTimestamp < auth.Created {
latestTimestamp = auth.Created
// Fallback to latest created
/*
if latestTimestamp < auth.Created {
latestTimestamp = auth.Created
action.AuthenticationId = auth.Id
}
*/

// If valid, just choose it
if auth.Validation.Valid {
action.AuthenticationId = auth.Id
break
}
*/

// If valid, just choose it
if auth.Validation.Valid {
action.AuthenticationId = auth.Id
break
}
}
}
Expand Down Expand Up @@ -18127,8 +18131,6 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by
newParams = append(newParams, param)
}

//log.Printf("URL %#v", originalUrl)

action.Parameters = newParams

action.Sharing = app.Sharing
Expand Down Expand Up @@ -18181,6 +18183,7 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by
badRequest.Method = "GET"

workflowExecution, _, errString, err := PrepareWorkflowExecution(ctx, workflow, badRequest, 10)

if err != nil || len(errString) > 0 {

// FIXME: Handle other error returns as well?
Expand Down Expand Up @@ -22254,20 +22257,33 @@ func PrepareWorkflowExecution(ctx context.Context, workflow Workflow, request *h
newParams = append(newParams, param)
}
} else {
// Rebuild params with the right data. This is to prevent issues on the frontend
// This may make the system miss fields.
addedParamIndexes := []string{}
for _, param := range action.Parameters {

for _, authparam := range curAuth.Fields {
if param.Name == authparam.Key {
param.Value = authparam.Value
//log.Printf("Name: %s - value: %s", param.Name, param.Value)
//log.Printf("Name: %s - value: %s\n", param.Name, param.Value)
break
for paramIndex, authparam := range curAuth.Fields {
if param.Name != authparam.Key {
continue
}

addedParamIndexes = append(addedParamIndexes, string(paramIndex))
param.Value = authparam.Value
break
}

newParams = append(newParams, param)
}

for paramIndex, authparam := range curAuth.Fields {
if ArrayContains(addedParamIndexes, string(paramIndex)) {
continue
}

newParams = append(newParams, WorkflowAppActionParameter{
Name: authparam.Key,
Value: authparam.Value,
})
}
}

action.Parameters = newParams
Expand Down Expand Up @@ -25876,6 +25892,8 @@ func RunCategoryAction(resp http.ResponseWriter, request *http.Request) {
if len(selectedAction.Name) == 0 && value.Label != "discover_app" {
log.Printf("[WARNING] Couldn't find the label '%s' in app '%s'.", value.Label, selectedApp.Name)

//selectedApp := AutofixAppLabels(selectedApp, value.Label)

if value.Label != "app_authentication" && value.Label != "authenticate_app" && value.Label != "discover_app" {
resp.WriteHeader(500)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "app_id": "%s", "reason": "Failed finding action '%s' labeled in app '%s'. If this is wrong, please suggest a label by finding the app in Shuffle, OR contact support@shuffler.io and we can help with labeling."}`, selectedApp.ID, value.Label, strings.ReplaceAll(selectedApp.Name, "_", " "))))
Expand Down

0 comments on commit 1378d2c

Please sign in to comment.