# GHSA-fm76-w8jw-xf8m: When creating a new plugin using the `git` source, the user-controlled value `req.body.name` is used to build the plugin directo

> An admin can execute arbitrary commands via command injection in the plugin name during git-based plugin installation.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** An administrator
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 3 Oct 2024

## What is GHSA-fm76-w8jw-xf8m?

GHSA-fm76-w8jw-xf8m is a command injection vulnerability in @saltcorn/plugins-loader, published on 3 Oct 2024. An attacker needs administrator access to exploit it, with no action from the victim.

### Official description

### Summary

When creating a new plugin using the `git` source, the user-controlled value `req.body.name` is used to build the plugin directory where the location will be cloned. The API used to execute the `git clone` command with the user-controlled data is `child_process.execSync`. Since the user-controlled data is not validated, a user with admin permission can add escaping characters and execute arbitrary commands, leading to a command injection vulnerability.

### Details

Relevant code from source (`req.body`) to sink (`child_process.execSync`).

- file: https://github.com/saltcorn/saltcorn/blob/v1.0.0-beta.13/packages/server/routes/plugins.js#L1400

```js
router.post(
  "/",
  isAdmin,
  error_catcher(async (req, res) => {
    const plugin = new Plugin(req.body); // [1] 
      [...]
      try {
        await load_plugins.loadAndSaveNewPlugin( // [3] 
          plugin,
          schema === db.connectObj.default_schema || plugin.source === "github"
        );
        [...]
    }
  })
);
```

- file: https://github.com/saltcorn/saltcorn/blob/v1.0.0-beta.13/packages/saltcorn-data/models/plugin.ts#L44
```js
class Plugin {
  [...]
  constructor(o: PluginCfg | PluginPack | Plugin) {
    [...]
    this.name = o.name; // [2] 
    [...]
}
```

- file: https://github.com/saltcorn/saltcorn/blob/v1.0.0-beta.13/packages/server/load_plugins.js#L64-L65
```js
const loadAndSaveNewPlugin = async (plugin, force, noSignalOrDB) => {
  [...]
  const loader = new PluginInstaller(plugin); // [4] 
  const res = await loader.install(force); // [7] 
  [...]
};
```

- file: https://github.com/saltcorn/saltcorn/blob/v1.0.0-beta.13/packages/plugins-loader/plugin_installer.js#L41-L61
```js
class PluginInstaller {
  constructor(plugin, opts = {}) {
    [...]
    const tokens =
      plugin.source === "npm"
        ? plugin.location.split("/")
        : plugin.name.split("/"); // [5] 
    [...]
    this.tempDir = join(this.tempRootFolder, "temp_install", ...tokens); // [6] 
    [...]
  }

  
  async install(force) {
    [...]
    if (await this.prepPluginsFolder(force, pckJSON)) { // [8] 
    [...]
  }

  async prepPluginsFolder(force, pckJSON) {
    [...]
    switch (this.plugin.source) {
      [...]
      case "git":
        if (force || !(await pathExists(this.pluginDir))) { 
          await gitPullOrClone(this.plugin, this.tempDir); // [9] 
	  [...]
  }
```

- file: https://github.com/saltcorn/saltcorn/blob/v1.0.0-beta.13/packages/plugins-loader/download_utils.js#L112
```js
const gitPullOrClone = async (plugin, pluginDir) => {
  [...]
  if (fs.existsSync(pluginDir)) {
    execSync(`git ${setKey} -C ${pluginDir} pull`);
  } else {
    execSync(`git ${setKey} clone ${plugin.location} ${pluginDir}`); // [10] 
  }
  [...]
};
```

### PoC

- check that the file will be created by the command `echo "hello">/tmp/HACKED` does not exists:
```
cat /tmp/HACKED
cat: /tmp/HACKED: No such file or directory
```
- login with an admin account
- visit `http://localhost:3000/plugins/new`
- enter the following fields:
	- Name: `;echo "hello">/tmp/HACKED`
	- Source: `git`
	- other fields blank
- click `Create`
- you will get an error saying `ENOENT: no such file or directory,  ....` but the command `touch /tmp/HACKED` will be executed
- to verify:
```
cat /tmp/HACKED
hello
```

### Impact

Remote code execution

### Recommended Mitigation

Sanitize the `pluginDir` value before passing to `execSync`. Alternatively, use `child_process. execFileSync` API (docs: https://nodejs.org/api/child_process.html#child_processexecfilesyncfile-args-options)

## Which versions are affected by GHSA-fm76-w8jw-xf8m?

2 open source packages ship an affected version, including @saltcorn/plugins-loader.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `@saltcorn/plugins-loader` | npm | All versions | 1.0.0-beta.14 |
| `@saltcorn/plugins-loader` | npm | All versions | 1.0.0-beta.14 |

## How do I fix GHSA-fm76-w8jw-xf8m?

Upgrade @saltcorn/plugins-loader to 1.0.0-beta.14 or later.

## Is GHSA-fm76-w8jw-xf8m being exploited?

There is no known exploitation of GHSA-fm76-w8jw-xf8m in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-fm76-w8jw-xf8m and fix it if it is affected.

What it is: GHSA-fm76-w8jw-xf8m is a command injection vulnerability in @saltcorn/plugins-loader, published on 3 Oct 2024. An attacker needs administrator access to exploit it, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-fm76-w8jw-xf8m in the wild and no public exploit code.

Affected packages and fixed versions:
- @saltcorn/plugins-loader (npm): affected all versions; fixed in 1.0.0-beta.14
- @saltcorn/plugins-loader (npm): affected all versions; fixed in 1.0.0-beta.14

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-fm76-w8jw-xf8m/ (Markdown: https://insights.sbomify.com/GHSA-fm76-w8jw-xf8m/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-fm76-w8jw-xf8m

---
From sbomify Insights: https://insights.sbomify.com/GHSA-fm76-w8jw-xf8m/
Summaries are generated and may contain mistakes; the official description above is the record.
