checkSensitivePredicates checks whether a span uses sensitive fields in predicates. Returns an error message if so, or "" if clean. This is used post-execution to blank individual statement results (matching SQL/CosmosDB behavior) rather than blocking the entire request.
( ctx context.Context, stores *store.Store, database *store.DatabaseMessage, span *parserbase.QuerySpan, )
| 139 | // post-execution to blank individual statement results (matching SQL/CosmosDB |
| 140 | // behavior) rather than blocking the entire request. |
| 141 | func checkSensitivePredicates( |
| 142 | ctx context.Context, |
| 143 | stores *store.Store, |
| 144 | database *store.DatabaseMessage, |
| 145 | span *parserbase.QuerySpan, |
| 146 | ) (string, error) { |
| 147 | if len(span.PredicatePaths) == 0 { |
| 148 | return "", nil |
| 149 | } |
| 150 | |
| 151 | var objectSchema *storepb.ObjectSchema |
| 152 | var err error |
| 153 | |
| 154 | if analysis := span.MongoDBAnalysis; analysis != nil && analysis.Collection != "" { |
| 155 | objectSchema, err = getTableObjectSchema(ctx, stores, database.InstanceID, database.DatabaseName, analysis.Collection) |
| 156 | if err != nil { |
| 157 | return "", errors.Wrapf(err, "failed to get object schema for collection %q", analysis.Collection) |
| 158 | } |
| 159 | } else if analysis := span.ElasticsearchAnalysis; analysis != nil && analysis.Index != "" { |
| 160 | objectSchema, err = getTableObjectSchema(ctx, stores, database.InstanceID, database.DatabaseName, analysis.Index) |
| 161 | if err != nil { |
| 162 | return "", errors.Wrapf(err, "failed to get object schema for index %q", analysis.Index) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if objectSchema == nil { |
| 167 | return "", nil |
| 168 | } |
| 169 | |
| 170 | for pathStr := range span.PredicatePaths { |
| 171 | semanticType := lookupSemanticTypeByDotPath(pathStr, objectSchema) |
| 172 | if semanticType != "" { |
| 173 | return fmt.Sprintf("using field %q tagged by semantic type %q in query predicate is not allowed", pathStr, semanticType), nil |
| 174 | } |
| 175 | } |
| 176 | return "", nil |
| 177 | } |
| 178 | |
| 179 | func getFirstSemanticTypeInPath(ast *parserbase.PathAST, objectSchema *storepb.ObjectSchema) string { |
| 180 | if ast == nil || ast.Root == nil || objectSchema == nil { |
no test coverage detected