fix: parse AI table iframe query links
This commit is contained in:
@@ -204,6 +204,7 @@ func tableBindingInstructions(action string) string {
|
||||
"3. Schema验证成功后会立即激活FAQ写入,并短暂重启DobotAI加载Dispatcher。\n\n" +
|
||||
"如果完整URL被钉钉自动转换为链接卡片,请只发送 alidocs.dingtalk.com/ 后面的部分,例如:\n" +
|
||||
"i/nodes/<BaseID>?sheetId=<SheetID>&viewId=<ViewID>\n\n" +
|
||||
"如果路径中包含 iframeQuery=,请完整保留;系统会自动解码其中的 sheetId/viewId。\n\n" +
|
||||
"发送“取消”退出。"
|
||||
}
|
||||
return "请在10分钟内直接发送问题AI表格中任意一条记录的链接(钉钉显示为链接卡片也可以直接发送)。\n\n" +
|
||||
@@ -213,6 +214,7 @@ func tableBindingInstructions(action string) string {
|
||||
"3. Schema验证成功后会立即激活问题表写入,并短暂重启DobotAI加载Dispatcher。\n\n" +
|
||||
"如果完整URL被钉钉自动转换为链接卡片,请只发送 alidocs.dingtalk.com/ 后面的部分,例如:\n" +
|
||||
"i/nodes/<BaseID>?sheetId=<SheetID>&viewId=<ViewID>\n\n" +
|
||||
"如果路径中包含 iframeQuery=,请完整保留;系统会自动解码其中的 sheetId/viewId。\n\n" +
|
||||
"发送“取消”退出。"
|
||||
}
|
||||
|
||||
|
||||
@@ -184,6 +184,8 @@ func TestControllerFAQBindingPromptExplainsPermissionsSchemaActivationAndLinkCar
|
||||
"短暂重启DobotAI",
|
||||
"只发送 alidocs.dingtalk.com/ 后面的部分",
|
||||
"i/nodes/<BaseID>?sheetId=<SheetID>&viewId=<ViewID>",
|
||||
"iframeQuery",
|
||||
"完整保留",
|
||||
"取消",
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
|
||||
@@ -95,13 +95,62 @@ func ParseNotableLink(raw string) (NotableLink, error) {
|
||||
}
|
||||
base := path.Base(strings.TrimRight(u.Path, "/"))
|
||||
q := u.Query()
|
||||
result := NotableLink{BaseID: base, SheetID: q.Get("sheetId"), ViewID: q.Get("viewId")}
|
||||
sheetID, err := uniqueNotableQueryValue(q, "sheetId")
|
||||
if err != nil {
|
||||
return NotableLink{}, err
|
||||
}
|
||||
viewID, err := uniqueNotableQueryValue(q, "viewId")
|
||||
if err != nil {
|
||||
return NotableLink{}, err
|
||||
}
|
||||
iframeQuery, err := uniqueNotableQueryValue(q, "iframeQuery")
|
||||
if err != nil {
|
||||
return NotableLink{}, err
|
||||
}
|
||||
if iframeQuery != "" {
|
||||
nested, parseErr := url.ParseQuery(iframeQuery)
|
||||
if parseErr != nil {
|
||||
return NotableLink{}, errors.New("invalid AI Table iframeQuery")
|
||||
}
|
||||
nestedSheetID, valueErr := uniqueNotableQueryValue(nested, "sheetId")
|
||||
if valueErr != nil {
|
||||
return NotableLink{}, valueErr
|
||||
}
|
||||
nestedViewID, valueErr := uniqueNotableQueryValue(nested, "viewId")
|
||||
if valueErr != nil {
|
||||
return NotableLink{}, valueErr
|
||||
}
|
||||
if sheetID != "" && nestedSheetID != "" && sheetID != nestedSheetID {
|
||||
return NotableLink{}, errors.New("conflicting AI Table sheet IDs")
|
||||
}
|
||||
if viewID != "" && nestedViewID != "" && viewID != nestedViewID {
|
||||
return NotableLink{}, errors.New("conflicting AI Table view IDs")
|
||||
}
|
||||
if sheetID == "" {
|
||||
sheetID = nestedSheetID
|
||||
}
|
||||
if viewID == "" {
|
||||
viewID = nestedViewID
|
||||
}
|
||||
}
|
||||
result := NotableLink{BaseID: base, SheetID: sheetID, ViewID: viewID}
|
||||
if result.BaseID == "" || result.BaseID == "." || result.BaseID == ".." || result.SheetID == "" {
|
||||
return NotableLink{}, errors.New("AI Table link is missing base or sheet")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func uniqueNotableQueryValue(values url.Values, key string) (string, error) {
|
||||
items := values[key]
|
||||
if len(items) > 1 {
|
||||
return "", errors.New("conflicting AI Table query values")
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
return strings.TrimSpace(items[0]), nil
|
||||
}
|
||||
|
||||
type Runtime interface {
|
||||
Start(context.Context, string) error
|
||||
Stop(context.Context) error
|
||||
|
||||
@@ -3,6 +3,7 @@ package settings
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -70,6 +71,23 @@ func TestParseNotableLinkAcceptsTrustedHostRelativeTail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNotableLinkDecodesSheetAndViewFromIframeQuery(t *testing.T) {
|
||||
got, err := ParseNotableLink("i/nodes/base123?iframeQuery=entrance%3Ddata%26sheetId%3Dsheet456%26viewId%3Dview789")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.BaseID != "base123" || got.SheetID != "sheet456" || got.ViewID != "view789" {
|
||||
t.Fatalf("got=%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNotableLinkRejectsConflictingIframeQueryIDs(t *testing.T) {
|
||||
_, err := ParseNotableLink("i/nodes/base123?sheetId=sheet-top&iframeQuery=sheetId%3Dsheet-nested%26viewId%3Dview789")
|
||||
if err == nil || !strings.Contains(err.Error(), "conflicting") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNotableLinkRejectsUnsafeOrAmbiguousRelativeTail(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"//evil.example/i/nodes/base?sheetId=sheet",
|
||||
|
||||
Reference in New Issue
Block a user