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

Feat community templates #68

Merged
merged 6 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add +trust option for templates
  • Loading branch information
lowlighter committed Jan 19, 2021
commit f580972441e8dd865feb19912fec0a7c470741ac
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,19 @@ For example, to use the `super-metrics` template from `github-user`'s fork, add
setup_community_templates: github-user/metrics@latest:classic
```

To create a community template, just fork this repository and create a new folder in `/source/templates` with the same structure as current templates.
Then, it's just as simple as HTML and CSS with a bit of JavaScript!
By default, community templates have their `template.mjs` removed and fallback to the one used by `classic` template.
It means that they're restricted to common and plugins data, to prevent malicious code injection and token leaks.

If you really trust a template, it is possible to bypass this behaviour by appending `+trust` at the end of their source like below:
```yaml
- uses: lowlighter/metrics@master
with:
# ... other options
setup_community_templates: github-user/metrics@latest:classic+trust
```

⚠️ Community templates are restricted to common and plugins data.
Their "template.mjs" is automatically removed to prevent malicious code injection.
To create a new community template, just fork this repository and create a folder in `/source/templates` with the same structure as current templates.
Then, it's just as simple as HTML and CSS with a bit of JavaScript!

</details>

Expand Down
13 changes: 11 additions & 2 deletions source/app/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
try {
//Parse community template
logger(`metrics/setup > load community template ${template}`)
const {repo, branch, name} = template.match(/^(?<repo>[\s\S]+?)@(?<branch>[\s\S]+?):(?<name>[\s\S]+?)$/)?.groups
const {repo, branch, name, trust = false} = template.match(/^(?<repo>[\s\S]+?)@(?<branch>[\s\S]+?):(?<name>[\s\S]+?)(?<trust>[+]trust)?$/)?.groups
const command = `git clone --single-branch --branch ${branch} https://github.com/${repo}.git ${path.join(__templates, ".community")}`
logger(`metrics/setup > run ${command}`)
//Clone remote repository
Expand All @@ -80,6 +80,15 @@
logger(`metrics/setup > extract ${name} from ${repo}@${branch}`)
await fs.promises.rmdir(path.join(__templates, `@${name}`), {recursive:true})
await fs.promises.rename(path.join(__templates, ".community/source/templates", name), path.join(__templates, `@${name}`))
//JavaScript file
if (trust)
logger(`metrics/setup > keeping @${name}/template.mjs (unsafe mode is enabled)`)
else if (fs.existsSync(path.join(__templates, `@${name}`, "template.mjs"))) {
logger(`metrics/setup > removing @${name}/template.mjs`)
await fs.promises.unlink(path.join(__templates, `@${name}`, "template.mjs"))
}
else
logger(`metrics/setup > @${name}/template.mjs does not exist`)
//Clean remote repository
logger(`metrics/setup > clean ${repo}@${branch}`)
await fs.promises.rmdir(path.join(__templates, ".community"), {recursive:true})
Expand Down Expand Up @@ -107,7 +116,7 @@
conf.templates[name] = {image, style, fonts, partials, views:[directory]}

//Cache templates scripts
Templates[name] = (await import(url.pathToFileURL(path.join(directory, "template.mjs")).href)).default
Templates[name] = (await import(url.pathToFileURL(path.join(fs.existsSync(path.join(directory, "templates.mjs")) ? directory : path.join(__templates, "classic"), "template.mjs")).href)).default
logger(`metrics/setup > load template [${name}] > success`)
//Debug
if (conf.settings.debug) {
Expand Down