-
Notifications
You must be signed in to change notification settings - Fork 413
fix(base): reject wrapped JSON payloads in +record-upsert #305
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,7 +222,7 @@ func TestBaseRecordExecuteUpsertUpdate(t *testing.T) { | |
| "data": map[string]interface{}{"record_id": "rec_x", "fields": map[string]interface{}{"Name": "Alice"}}, | ||
| }, | ||
| }) | ||
| if err := runShortcut(t, BaseRecordUpsert, []string{"+record-upsert", "--base-token", "app_x", "--table-id", "tbl_x", "--record-id", "rec_x", "--json", `{"fields":{"Name":"Alice"}}`}, factory, stdout); err != nil { | ||
| if err := runShortcut(t, BaseRecordUpsert, []string{"+record-upsert", "--base-token", "app_x", "--table-id", "tbl_x", "--record-id", "rec_x", "--json", `{"Name":"Alice"}`}, factory, stdout); err != nil { | ||
| t.Fatalf("err=%v", err) | ||
| } | ||
| if got := stdout.String(); !strings.Contains(got, `"updated": true`) || !strings.Contains(got, `"rec_x"`) { | ||
|
|
@@ -544,14 +544,25 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| "data": map[string]interface{}{"record_id": "rec_new", "fields": map[string]interface{}{"Name": "Alice"}}, | ||
| }, | ||
| }) | ||
| if err := runShortcut(t, BaseRecordUpsert, []string{"+record-upsert", "--base-token", "app_x", "--table-id", "tbl_x", "--json", `{"fields":{"Name":"Alice"}}`}, factory, stdout); err != nil { | ||
| if err := runShortcut(t, BaseRecordUpsert, []string{"+record-upsert", "--base-token", "app_x", "--table-id", "tbl_x", "--json", `{"Name":"Alice"}`}, factory, stdout); err != nil { | ||
| t.Fatalf("err=%v", err) | ||
| } | ||
| if got := stdout.String(); !strings.Contains(got, `"created": true`) || !strings.Contains(got, `"rec_new"`) { | ||
| t.Fatalf("stdout=%s", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("reject top-level fields wrapper", func(t *testing.T) { | ||
| factory, stdout, _ := newExecuteFactory(t) | ||
| err := runShortcut(t, BaseRecordUpsert, []string{"+record-upsert", "--base-token", "app_x", "--table-id", "tbl_x", "--json", `{"fields":{"Name":"Alice"}}`}, factory, stdout) | ||
| if err == nil || !strings.Contains(err.Error(), "direct record object") { | ||
| t.Fatalf("err=%v", err) | ||
| } | ||
| if got := stdout.String(); got != "" { | ||
| t.Fatalf("stdout=%s", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("delete", func(t *testing.T) { | ||
| factory, stdout, reg := newExecuteFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
|
|
@@ -587,7 +598,7 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/fields/fld_att", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"id": "fld_att", "name": "附件", "type": "attachment"}, | ||
| "data": map[string]interface{}{"id": "fld_att", "name": "éä»¶", "type": "attachment"}, | ||
| }, | ||
| }) | ||
| reg.Register(&httpmock.Stub{ | ||
|
|
@@ -598,7 +609,7 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| "data": map[string]interface{}{ | ||
| "record_id": "rec_x", | ||
| "fields": map[string]interface{}{ | ||
| "附件": []interface{}{ | ||
| "éä»¶": []interface{}{ | ||
| map[string]interface{}{ | ||
| "file_token": "existing_tok", | ||
| "name": "existing.pdf", | ||
|
|
@@ -629,7 +640,7 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| "data": map[string]interface{}{ | ||
| "record_id": "rec_x", | ||
| "fields": map[string]interface{}{ | ||
| "附件": []interface{}{ | ||
| "éä»¶": []interface{}{ | ||
| map[string]interface{}{ | ||
| "file_token": "existing_tok", | ||
| "name": "existing.pdf", | ||
|
|
@@ -671,7 +682,7 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| } | ||
|
|
||
| updateBody := string(updateStub.CapturedBody) | ||
| if !strings.Contains(updateBody, `"附件"`) || | ||
| if !strings.Contains(updateBody, `"éä»¶"`) || | ||
| !strings.Contains(updateBody, `"file_token":"existing_tok"`) || | ||
| !strings.Contains(updateBody, `"name":"existing.pdf"`) || | ||
| !strings.Contains(updateBody, `"size":2048`) || | ||
|
|
@@ -704,7 +715,7 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) { | |
| URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/fields/fld_status", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"id": "fld_status", "name": "状态", "type": "text"}, | ||
| "data": map[string]interface{}{"id": "fld_status", "name": "ç¶æ", "type": "text"}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same UTF-8 encoding corruption — The field name 🤖 Prompt for AI Agents |
||
| }, | ||
| }) | ||
|
|
||
|
|
@@ -899,13 +910,13 @@ func TestBaseFieldExecuteSearchOptions(t *testing.T) { | |
| URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/fields/fld_amount/options", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"options": []interface{}{map[string]interface{}{"id": "opt_1", "name": "已完成"}}, "total": 1}, | ||
| "data": map[string]interface{}{"options": []interface{}{map[string]interface{}{"id": "opt_1", "name": "已宿"}}, "total": 1}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same UTF-8 encoding corruption in search options test. The keyword and option names have similar corruption:
These should be restored to the original Chinese characters unless this is intentional test data for encoding handling. Also applies to: 916-916, 919-919 🤖 Prompt for AI Agents |
||
| }, | ||
| }) | ||
| if err := runShortcut(t, BaseFieldSearchOptions, []string{"+field-search-options", "--base-token", "app_x", "--table-id", "tbl_x", "--field-id", "fld_amount", "--keyword", "已", "--limit", "10"}, factory, stdout); err != nil { | ||
| if err := runShortcut(t, BaseFieldSearchOptions, []string{"+field-search-options", "--base-token", "app_x", "--table-id", "tbl_x", "--field-id", "fld_amount", "--keyword", "å·²", "--limit", "10"}, factory, stdout); err != nil { | ||
| t.Fatalf("err=%v", err) | ||
| } | ||
| if got := stdout.String(); !strings.Contains(got, `"options"`) || !strings.Contains(got, `"已完成"`) { | ||
| if got := stdout.String(); !strings.Contains(got, `"options"`) || !strings.Contains(got, `"已宿"`) { | ||
| t.Fatalf("stdout=%s", got) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -75,6 +75,17 @@ func dryRunRecordHistoryList(_ context.Context, runtime *common.RuntimeContext) | |||||
| } | ||||||
|
|
||||||
| func validateRecordJSON(runtime *common.RuntimeContext) error { | ||||||
| body, err := parseJSONObject(runtime.Str("json"), "json") | ||||||
| if err != nil { | ||||||
| // Keep invalid JSON handling on the execution path unchanged; only | ||||||
| // intercept the common top-level shape mistake here. | ||||||
| return nil | ||||||
| } | ||||||
| if len(body) == 1 { | ||||||
| if fields, ok := body["fields"].(map[string]interface{}); ok && len(fields) > 0 { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The
Suggested change
|
||||||
| return common.FlagErrorf("--json for +record-upsert must be a direct record object, not a top-level \"fields\" wrapper; use '{\"Name\":\"Alice\"}' instead of '{\"fields\":{\"Name\":\"Alice\"}}'. If your real field name is literally \"fields\", use the field ID as the key.") | ||||||
| } | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR replaces valid UTF-8 Chinese characters with Latin-1 mojibake across multiple unrelated stubs and assertions:
"附件"(attachment) →"éä»¶"at lines 601, 612, 643, and 685;"状态"(status) →"ç¶æ"at line 718;"已完成"→"已宿"and keyword"已"→"å·²"at lines 913–919. This appears to be an editor encoding bug where the UTF-8 source was re-saved as Latin-1. The tests may still pass because the corruption is symmetric between stubs and assertions, but the data no longer represents realistic API responses and will mislead future maintainers. These lines were not part of the intended change — please revert to the original Chinese characters.