Repository navigation
Expand file tree
/
Copy pathflow_definition_limits.go
More file actions
108 lines (102 loc) · 3.84 KB
/
Copy pathflow_definition_limits.go
File metadata and controls
108 lines (102 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2026 Super Durable, Inc.
//
// Licensed under the Sustainable Use License 1.0.
// You may not use this file except in compliance with the License.
// See the LICENSE file in the repository root.
//
// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
package web
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"unicode/utf8"
)
// Limits on one Flow Definition Graph, checked before Dex Web keeps or draws it.
// The browser checks the same node, edge, and string limits in app/design/definitionLimits.ts.
const (
// maxFlowDefinitionNodes bounds the Steps, resources, and RPCs the canvas lays out.
maxFlowDefinitionNodes = 2000
// maxFlowDefinitionEdges bounds the relations the canvas routes.
maxFlowDefinitionEdges = 10000
// maxFlowDefinitionStringCharacters bounds each JSON string, including object keys.
maxFlowDefinitionStringCharacters = 16 << 10
// maxFlowDefinitionDepth matches the nesting limit of the Dex Server upload.
maxFlowDefinitionDepth = 128
// maxFlowDefinitionConnectorSteps bounds the Connector release lookups one definition causes.
maxFlowDefinitionConnectorSteps = 200
// maxFlowDefinitionDiagnostics bounds the diagnostics listed under a graph.
maxFlowDefinitionDiagnostics = 1000
// maxFlowDefinitionGroups bounds the Step groups of a Version 2.0 graph.
maxFlowDefinitionGroups = 1000
// maxFlowDefinitionGroupedSteps bounds the Step IDs of all groups; each Step joins at most one group.
maxFlowDefinitionGroupedSteps = maxFlowDefinitionNodes
// maxFlowDefinitionV2Items bounds each list in the v2 block, such as Actions or Display fields.
maxFlowDefinitionV2Items = 256
// maxFlowDefinitionStartInputItems bounds the fields and enum values of a start input, counted at every depth.
maxFlowDefinitionStartInputItems = 1024
)
// checkFlowDefinitionDocumentLimits walks the JSON tokens iteratively, so deep nesting cannot exhaust the stack.
func checkFlowDefinitionDocumentLimits(data []byte) error {
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
depth := 0
for {
token, err := decoder.Token()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("parse JSON: %w", err)
}
switch value := token.(type) {
case json.Delim:
if value == '{' || value == '[' {
depth++
if depth > maxFlowDefinitionDepth {
return fmt.Errorf("JSON nests deeper than %d levels", maxFlowDefinitionDepth)
}
} else {
depth--
}
case string:
if utf8.RuneCountInString(value) > maxFlowDefinitionStringCharacters {
return fmt.Errorf("a JSON string is longer than %d characters", maxFlowDefinitionStringCharacters)
}
}
}
}
// checkFlowDefinitionGraphLimits bounds the node and edge counts of a parsed graph.
func checkFlowDefinitionGraphLimits(nodes []json.RawMessage, edges []json.RawMessage) error {
if len(nodes) > maxFlowDefinitionNodes {
return fmt.Errorf("it has %d nodes; the limit is %d", len(nodes), maxFlowDefinitionNodes)
}
if len(edges) > maxFlowDefinitionEdges {
return fmt.Errorf("it has %d edges; the limit is %d", len(edges), maxFlowDefinitionEdges)
}
return nil
}
// checkFlowDefinitionConnectorSteps bounds the Connector Steps of nodes that checkFlowDefinitionElements accepted.
func checkFlowDefinitionConnectorSteps(nodes []json.RawMessage) error {
connectorSteps := 0
for _, raw := range nodes {
var node struct {
Kind string `json:"kind"`
Metadata struct {
ConnectorFactory bool `json:"connectorFactory"`
} `json:"metadata"`
}
if err := json.Unmarshal(raw, &node); err != nil {
return fmt.Errorf("malformed node: %w", err)
}
if node.Kind == "step" && node.Metadata.ConnectorFactory {
connectorSteps++
}
}
if connectorSteps > maxFlowDefinitionConnectorSteps {
return fmt.Errorf("it has %d Connector Steps; the limit is %d", connectorSteps, maxFlowDefinitionConnectorSteps)
}
return nil
}