Skip to content
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

handle invalid sections without segfaulting #19

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func main() {
log.Fatal(err)
}

aclSections := getAllowedSections(allowedAclSections, preDefinedAclSections)
aclSections, err := getAllowedSections(allowedAclSections, preDefinedAclSections)
if err != nil {
log.Fatal(err)
}

err = mergeDocs(aclSections, parentDoc, childDocs)
if err != nil {
log.Fatal(err)
Expand All @@ -114,13 +118,16 @@ func main() {
outputFile(parentDoc.Object)
}

func getAllowedSections(allowedAclSections []string, preDefinedAclSections map[string]SectionHandler) map[string]SectionHandler {
func getAllowedSections(allowedAclSections []string, preDefinedAclSections map[string]SectionHandler) (map[string]SectionHandler, error) {
aclSections := map[string]SectionHandler{}
for _, v := range allowedAclSections {
if preDefinedAclSections[v] == nil {
return nil, fmt.Errorf("unsupported section [%s] specified in [-allow] flag", v)
}
aclSections[v] = preDefinedAclSections[v]
}
logVerbose("allowing ACL sections [%v]\n", aclSections)
return aclSections
return aclSections, nil
}

type SectionHandler func(sectionKey string, parentPath string, parent *jwcc.Object, childPath string, childSection *jwcc.Member)
Expand Down
19 changes: 18 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ func TestGetAllowedSections(t *testing.T) {
"3": actualValue,
}
allowed := []string{"1", "2"}
allowedAclSections := getAllowedSections(allowed, defined)
allowedAclSections, err := getAllowedSections(allowed, defined)
if err != nil {
t.Fatalf("expected no error, got [%v]", err)
}

// should exist
section1 := allowedAclSections["1"]
Expand All @@ -260,6 +263,20 @@ func TestGetAllowedSections(t *testing.T) {
}
}

func TestGetAllowedSectionsInvalidSection(t *testing.T) {
actualValue := handleObject()
defined := map[string]SectionHandler{
"1": actualValue,
"2": actualValue,
"3": actualValue,
}
allowed := []string{"1", "2", "invalid"}
_, err := getAllowedSections(allowed, defined)
if err == nil {
t.Fatalf("expected error, got [%v]", err)
}
}

func TestHandleArray(t *testing.T) {
parent, err := jwcc.Parse(strings.NewReader(ACL_PARENT))
if err != nil {
Expand Down