Skip to content

Commit

Permalink
Fix bugs related to templates in Description
Browse files Browse the repository at this point in the history
Challenges with {{url}} or {{lookup}} template tags in the Description
field will no longer fail validation.

These templates will also be properly rendered when using `cmgr playtest`.
  • Loading branch information
dmartin committed Jan 29, 2024
1 parent 2eefb47 commit 50de11d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
15 changes: 13 additions & 2 deletions cmd/cmgr/playtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,21 @@ func launchPortal(mgr *cmgr.Manager, iface string, port int, cid cmgr.ChallengeI

w.Write([]byte(fmt.Sprintf(`<h1>%s</h1>`, cMeta.Name)))

w.Write([]byte(fmt.Sprintf(`<h2>Description</h2><p>%s</p>`, cMeta.Description)))
artifactUrl := fmt.Sprintf("http://%s:%d/artifact/$1", iface, port)

description := cMeta.Description
description = urlRe.ReplaceAllString(description, artifactUrl)
for lookupRe.MatchString(description) {
match := lookupRe.FindStringSubmatch(description)
description = strings.ReplaceAll(
description,
match[0],
fmt.Sprintf("%s", bMeta.LookupData[match[1]]))
}

w.Write([]byte(fmt.Sprintf(`<h2>Description</h2><p>%s</p>`, description)))

details := cMeta.Details
artifactUrl := fmt.Sprintf("http://%s:%d/artifact/$1", iface, port)
details = urlRe.ReplaceAllString(details, artifactUrl)
details = serverRe.ReplaceAllString(details, iface)
details = httpBaseRe.ReplaceAllString(details, fmt.Sprintf("http://%s", iface))
Expand Down
17 changes: 13 additions & 4 deletions cmgr/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ func (m *Manager) validateMetadata(md *ChallengeMetadata) error {
return s, err
}

// Validate (& lift) Details
res, err := normalizeAndCheckTemplated(md.Details)
res, err := normalizeAndCheckTemplated(md.Description)
if err != nil {
lastErr = err
}
md.Description = res

res, err = normalizeAndCheckTemplated(md.Details)
if err != nil {
lastErr = err
}
Expand Down Expand Up @@ -447,8 +452,12 @@ func (m *Manager) validateBuild(cMeta *ChallengeMetadata, md *BuildMetadata, fil
return err
}

// Validate (& lift) Details
err := checkTemplated(cMeta.Details)
// Validate templated fields
err := checkTemplated(cMeta.Description)
detailsErr := checkTemplated(cMeta.Details)
if detailsErr != nil {
err = detailsErr
}

for _, hint := range cMeta.Hints {
tmpErr := checkTemplated(hint)
Expand Down

0 comments on commit 50de11d

Please sign in to comment.