Add bounded sources and strict YAML parsing
This commit is contained in:
66
parse.go
Normal file
66
parse.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package configkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
yamladapter "gitea.maximumdirect.net/eric/ConfigKit/internal/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
maxDocumentDepth = 256
|
||||
maxDocumentNodes = 1_000_000
|
||||
)
|
||||
|
||||
type parseLimits struct {
|
||||
source sourceLimits
|
||||
document yamladapter.Limits
|
||||
}
|
||||
|
||||
func defaultParseLimits() parseLimits {
|
||||
return parseLimits{
|
||||
source: defaultSourceLimits(),
|
||||
document: yamladapter.Limits{
|
||||
Depth: maxDocumentDepth,
|
||||
Nodes: maxDocumentNodes,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type parsedSource struct {
|
||||
document *document.Node
|
||||
source *SourceInfo
|
||||
}
|
||||
|
||||
func parseSource(source Source, sourceID string, limits parseLimits) (parsedSource, []Diagnostic) {
|
||||
read, diagnostics := readSource(source, sourceID, limits.source)
|
||||
if len(diagnostics) != 0 || read.info == nil {
|
||||
return parsedSource{source: read.info}, diagnostics
|
||||
}
|
||||
|
||||
root, issues := yamladapter.Parse(bytes.NewReader(read.data), sourceID, limits.document)
|
||||
if len(issues) != 0 {
|
||||
diagnostics := make([]Diagnostic, len(issues))
|
||||
for i, issue := range issues {
|
||||
diagnostics[i] = Diagnostic{
|
||||
Code: Code(issue.Code),
|
||||
Severity: SeverityError,
|
||||
Message: issue.Message,
|
||||
Primary: &Location{
|
||||
SourceID: sourceID,
|
||||
Line: issue.Line,
|
||||
Column: issue.Column,
|
||||
},
|
||||
}
|
||||
for _, related := range issue.Related {
|
||||
diagnostics[i].Related = append(diagnostics[i].Related, Location{
|
||||
SourceID: related.SourceID,
|
||||
Line: related.Line,
|
||||
Column: related.Column,
|
||||
})
|
||||
}
|
||||
}
|
||||
return parsedSource{source: read.info}, canonicalDiagnostics(diagnostics)
|
||||
}
|
||||
return parsedSource{document: root, source: read.info}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user