4
0
Эх сурвалжийг харах

fix: Datetime mangling for Android phones #564 (#602)

James Read 1 жил өмнө
parent
commit
18c5599704

+ 29 - 0
service/internal/executor/arguments.go

@@ -213,3 +213,32 @@ func typeSafetyCheckUrl(value string) error {
 
 
 	return err
 	return err
 }
 }
+
+func mangleInvalidArgumentValues(req *ExecutionRequest) {
+	mangleInvalidDatetimeValues(req)
+}
+
+func mangleInvalidDatetimeValues(req *ExecutionRequest) {
+	for _, arg := range req.Action.Arguments {
+		if arg.Type == "datetime" {
+			value, exists := req.Arguments[arg.Name]
+
+			if !exists || value == "" {
+				continue
+			}
+
+			timestamp, err := time.Parse("2006-01-02T15:04", value)
+
+			if err == nil {
+				log.WithFields(log.Fields {
+					"arg":     arg.Name,
+					"value":   value,
+					"actionTitle": req.Action.Title,
+				}).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
+
+				req.Arguments[arg.Name] = timestamp.Format("2006-01-02T15:04:05")
+			}
+
+		}
+	}
+}

+ 2 - 0
service/internal/executor/executor.go

@@ -402,6 +402,8 @@ func stepParseArgs(req *ExecutionRequest) bool {
 	req.Arguments["ot_executionTrackingId"] = req.TrackingID
 	req.Arguments["ot_executionTrackingId"] = req.TrackingID
 	req.Arguments["ot_username"] = req.AuthenticatedUser.Username
 	req.Arguments["ot_username"] = req.AuthenticatedUser.Username
 
 
+	mangleInvalidArgumentValues(req)
+
 	req.finalParsedCommand, err = parseActionArguments(req.Arguments, req.Action, req.EntityPrefix)
 	req.finalParsedCommand, err = parseActionArguments(req.Arguments, req.Action, req.EntityPrefix)
 
 
 	if err != nil {
 	if err != nil {

+ 36 - 0
service/internal/executor/executor_test.go

@@ -129,6 +129,7 @@ func TestGetLogsLessThanPageSize(t *testing.T) {
 		Title: "blat",
 		Title: "blat",
 		Shell: "date",
 		Shell: "date",
 	})
 	})
+	cfg.Sanitize()
 
 
 	assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
 	assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
 
 
@@ -226,3 +227,38 @@ func TestUnusedArgumentStillPassesTypeSafetyCheck(t *testing.T) {
 	assert.Equal(t, "", out)
 	assert.Equal(t, "", out)
 	assert.NotNil(t, err)
 	assert.NotNil(t, err)
 }
 }
+
+// https://github.com/OliveTin/OliveTin/issues/564
+func TestMangleInvalidArgumentValues(t *testing.T) {
+	e, cfg := testingExecutor()
+
+	a1 := &config.Action{
+		Title: "Validate my date without seconds because I am from an Android phone",
+		Shell: "echo 'The date is: {{ date }}'",
+		Arguments: []config.ActionArgument{
+			{
+				Name: "date",
+				Type: "datetime",
+			},
+		},
+	}
+
+	cfg.Actions = append(cfg.Actions, a1)
+	cfg.Sanitize()
+
+	req := ExecutionRequest{
+		Action: a1,
+		AuthenticatedUser: acl.UserFromSystem(cfg, "testuser"),
+		Cfg:               cfg,
+		Arguments:         map[string]string{
+			"date":  "1990-01-10T12:00", // Invalid format, should be without seconds
+		},
+	}
+
+	wg, _ := e.ExecRequest(&req)
+	wg.Wait()
+
+	assert.NotNil(t, req.logEntry, "Log entry should not be nil")
+	assert.Equal(t, req.logEntry.Output, "The date is: 1990-01-10T12:00:00\n", "Date should be mangled to a valid format")
+
+}