-
Notifications
You must be signed in to change notification settings - Fork 596
feat(vql): add reformat function #4675
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
Merged
scudette
merged 10 commits into
Velocidex:master
from
nullifysecurity:feat/reformat-function
Feb 23, 2026
+267
−47
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3aa8b9e
feat(vql): add reformat function
nullifysecurity aa0909f
test(vql): add reformat function tests
nullifysecurity d71a510
refactor(cli): modify reformat command to wrap vql
nullifysecurity d71a559
docs(vql): add docs for reformat function
nullifysecurity 0001486
fix(vql): prefer return ordereddict over custom type
nullifysecurity 969cc23
test(vql): split test output for readability
nullifysecurity 162e7d9
fix(vql): return unformatted artifact on error
nullifysecurity f34a40c
fix(cli): change to pure vql approach
nullifysecurity b6d7635
Refactored VQL
scudette 62466d8
Clean up VQL
scudette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # This file contains the same tests that are in services/repository/reformat_test.go | ||
|
|
||
| Parameters: | ||
| Simple: | | ||
| name: Foo | ||
| sources: | ||
| - query: | | ||
| SELECT A,B,C | ||
| FROM info(arg1=Foobar, arg2="XXXXX") | ||
| - query: |- | ||
| SELECT A,B,C FROM info() | ||
| - description: Foo bar | ||
|
|
||
| SingleLine: | | ||
| name: Single line | ||
| sources: | ||
| - query: SELECT * FROM info() | ||
| - query: | | ||
| SELECT A FROM scope() | ||
|
|
||
| Exported: | | ||
| name: Exported | ||
| export: | | ||
| SELECT * FROM scope() | ||
|
|
||
| Preconditions: | | ||
| name: Preconditions | ||
| precondition: SELECT * FROM info() | ||
| sources: | ||
| - precondition: | | ||
| SELECT * FROM info() | ||
|
|
||
| Notebook: | | ||
| name: Notebook | ||
| sources: | ||
| - query: | | ||
| SELECT A,B,C | ||
| FROM scope() | ||
| notebook: | ||
| - name: Test | ||
| type: vql_suggestion | ||
| template: | | ||
| SELECT * FROM scope() | ||
|
|
||
| ColumnTypes: | | ||
| name: Column Types | ||
| sources: | ||
| - query: | | ||
| SELECT A,B,C | ||
| FROM scope() | ||
| column_types: | ||
| - name: A | ||
| type: string | ||
|
|
||
| Queries: | ||
| - SELECT | ||
| split(string=reformat(artifact=Simple).Artifact, sep="\\n") AS Simple, | ||
| split(string=reformat(artifact=SingleLine).Artifact, sep="\\n") AS SingleLine, | ||
| split(string=reformat(artifact=Exported).Artifact, sep="\\n") AS Exported, | ||
| split(string=reformat(artifact=Preconditions).Artifact, sep="\\n") AS Preconditions, | ||
| split(string=reformat(artifact=Notebook).Artifact, sep="\\n") AS Notebook, | ||
| split(string=reformat(artifact=ColumnTypes).Artifact, sep="\\n") AS ColumnTypes | ||
| FROM scope() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| Query: SELECT split(string=reformat(artifact=Simple).Artifact, sep="\\n") AS Simple, split(string=reformat(artifact=SingleLine).Artifact, sep="\\n") AS SingleLine, split(string=reformat(artifact=Exported).Artifact, sep="\\n") AS Exported, split(string=reformat(artifact=Preconditions).Artifact, sep="\\n") AS Preconditions, split(string=reformat(artifact=Notebook).Artifact, sep="\\n") AS Notebook, split(string=reformat(artifact=ColumnTypes).Artifact, sep="\\n") AS ColumnTypes FROM scope() | ||
| Output: [ | ||
| { | ||
| "Simple": [ | ||
| "name: Foo", | ||
| "sources:", | ||
| "- query: |", | ||
| " SELECT A,", | ||
| " B,", | ||
| " C", | ||
| " FROM info(arg1=Foobar, arg2=\"XXXXX\")", | ||
| "- query: |-", | ||
| " SELECT A,", | ||
| " B,", | ||
| " C", | ||
| " FROM info()", | ||
| "- description: Foo bar" | ||
| ], | ||
| "SingleLine": [ | ||
| "name: Single line", | ||
| "sources:", | ||
| "- query: SELECT * FROM info()", | ||
| "- query: |", | ||
| " SELECT A", | ||
| " FROM scope()" | ||
| ], | ||
| "Exported": [ | ||
| "name: Exported", | ||
| "export: |", | ||
| " SELECT *", | ||
| " FROM scope()" | ||
| ], | ||
| "Preconditions": [ | ||
| "name: Preconditions", | ||
| "precondition: SELECT * FROM info()", | ||
| "sources:", | ||
| "- precondition: |", | ||
| " SELECT *", | ||
| " FROM info()" | ||
| ], | ||
| "Notebook": [ | ||
| "name: Notebook", | ||
| "sources:", | ||
| "- query: |", | ||
| " SELECT A,", | ||
| " B,", | ||
| " C", | ||
| " FROM scope()", | ||
| " notebook:", | ||
| " - name: Test", | ||
| " type: vql_suggestion", | ||
| " template: |", | ||
| " SELECT * FROM scope()" | ||
| ], | ||
| "ColumnTypes": [ | ||
| "name: Column Types", | ||
| "sources:", | ||
| "- query: |", | ||
| " SELECT A,", | ||
| " B,", | ||
| " C", | ||
| " FROM scope()", | ||
| "column_types:", | ||
| " - name: A", | ||
| " type: string" | ||
| ] | ||
| } | ||
| ] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package functions | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "github.com/Velocidex/ordereddict" | ||
| "www.velocidex.com/golang/velociraptor/services" | ||
| vql_subsystem "www.velocidex.com/golang/velociraptor/vql" | ||
| "www.velocidex.com/golang/vfilter" | ||
| "www.velocidex.com/golang/vfilter/arg_parser" | ||
| ) | ||
|
|
||
| type ReformatFunctionArgs struct { | ||
| Artifact string `vfilter:"required,field=artifact,doc=The artifact VQL to reformat."` | ||
| } | ||
|
|
||
| type ReformatFunction struct{} | ||
|
|
||
| type ReformatFunctionResult struct { | ||
nullifysecurity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Artifact string | ||
| Error string | ||
| } | ||
|
|
||
| func (self *ReformatFunctionResult) ToDict() *ordereddict.Dict { | ||
| return ordereddict.NewDict(). | ||
| Set("Artifact", self.Artifact). | ||
| Set("Error", self.Error) | ||
| } | ||
|
|
||
| func (self *ReformatFunction) Call(ctx context.Context, scope vfilter.Scope, args *ordereddict.Dict) vfilter.Any { | ||
| defer vql_subsystem.RegisterMonitor(ctx, "reformat", args)() | ||
|
|
||
| result := &ReformatFunctionResult{} | ||
|
|
||
| arg := &ReformatFunctionArgs{} | ||
| err := arg_parser.ExtractArgsWithContext(ctx, scope, args, arg) | ||
| if err != nil { | ||
| result.Artifact = arg.Artifact | ||
| result.Error = err.Error() | ||
| return result.ToDict() | ||
| } | ||
|
|
||
| config_obj, ok := vql_subsystem.GetServerConfig(scope) | ||
| if !ok { | ||
| scope.Log("reformat: Must be run on the server") | ||
| return vfilter.Null{} | ||
| } | ||
|
|
||
| manager, err := services.GetRepositoryManager(config_obj) | ||
| if err != nil { | ||
| result.Artifact = arg.Artifact | ||
| result.Error = err.Error() | ||
| return result.ToDict() | ||
| } | ||
|
|
||
| reformatted, err := manager.ReformatVQL(ctx, arg.Artifact) | ||
| if err != nil { | ||
| result.Artifact = arg.Artifact | ||
| result.Error = err.Error() | ||
| return result.ToDict() | ||
| } | ||
|
|
||
| result.Artifact = strings.Trim(reformatted, "\n") | ||
| result.Error = "" | ||
|
|
||
| return result.ToDict() | ||
| } | ||
|
|
||
| func (self ReformatFunction) Info(scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.FunctionInfo { | ||
| return &vfilter.FunctionInfo{ | ||
| Name: "reformat", | ||
| Doc: `Reformat VQL | ||
|
|
||
| This function will reformat the artifact provided and return the reformatted content.`, | ||
| ArgType: type_map.AddType(scope, &ReformatFunctionArgs{}), | ||
| } | ||
| } | ||
|
|
||
| func init() { | ||
| vql_subsystem.RegisterFunction(&ReformatFunction{}) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.