mirror of
https://github.com/rohitg00/skillkit.git
synced 2026-11-20 20:34:46 +00:00
fix: security hardening and code quality fixes
* Security hardening and code quality fixes - Replace execSync with execFileSync to prevent shell injection (providers, executor, fetcher, publish) - Add security headers: X-Content-Type-Options, X-Frame-Options, Content-Security-Policy, Referrer-Policy - Add input validation for save route name field to prevent path traversal - Fix rate limiter IP extraction (x-forwarded-for + x-real-ip fallback) - Default server bind to 127.0.0.1 instead of 0.0.0.0 - CORS fallback to * (configurable via options.corsOrigin) - Use execFileSync in publish submit (avoid shell injection when opening browser) - TLS-aware health checks and HTTPS support in RemoteTransport - Conditional rejectUnauthorized for TLS (self-signed configurable) - Add **/.env.local patterns to .gitignore - Formatting: double quotes, trailing commas (Prettier) * fix: address CodeRabbit and Devin review findings from PR #74 - TLS: default allowSelfSigned=true to preserve self-signed cert workflow; enforce rejectUnauthorized=true when trustedCAs are supplied in client context - Signature verification: verifySecureMessage now cryptographically verifies signatures using PeerIdentity.verifyHex instead of only checking fingerprints - WebSocket: fix timeout handle leak in performClientHandshake; add error logging to empty catch blocks; add per-socket error handler; pre-compute signature once in broadcast(); add send error callbacks; reject startup if TLS configured but certInfo missing - Command parsing: replace broken regex tokenizer with shared splitCommand() utility that correctly handles --flag="value with spaces" - Rate limiter: prefer x-real-ip, fall back to rightmost x-forwarded-for entry - Save route: scope IPv6 prefix checks to actual IPv6 addresses; stop leaking internal error messages to clients - Publish: extract shared parseSkillFrontmatter(); fix getRepoInfo to use async execFileSync; fix path containment checks with separator - Remote transport: add rejectUnauthorized option; bracket IPv6 in URLs; fix host:port splitting for IPv6 with lastIndexOf - Health checks: add tls/tlsAllowSelfSigned to Host type; pass https options - Fetcher: validate owner/repo; derive indexDir from INDEX_PATH - .gitignore: add recursive **/.env pattern, remove redundant entries * fix(executor): restore shell feature support in shellExecutor The previous security hardening replaced execSync with execFileSync, which broke commands using pipes, redirects, and subshells. Now detects shell metacharacters and falls back to sh -c for those commands while keeping execFileSync for simple commands.
This commit is contained in:
+49
-40
@@ -1,36 +1,41 @@
|
||||
import { execSync } from 'node:child_process';
|
||||
import { existsSync, rmSync } from 'node:fs';
|
||||
import { join, basename } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { GitProviderAdapter, CloneOptions } from './base.js';
|
||||
import { parseShorthand } from './base.js';
|
||||
import type { GitProvider, CloneResult } from '../core/types.js';
|
||||
import { discoverSkills } from '../core/skills.js';
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync, rmSync } from "node:fs";
|
||||
import { join, basename } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { GitProviderAdapter, CloneOptions } from "./base.js";
|
||||
import { parseShorthand } from "./base.js";
|
||||
import type { GitProvider, CloneResult } from "../core/types.js";
|
||||
import { discoverSkills } from "../core/skills.js";
|
||||
|
||||
export class BitbucketProvider implements GitProviderAdapter {
|
||||
readonly type: GitProvider = 'bitbucket';
|
||||
readonly name = 'Bitbucket';
|
||||
readonly baseUrl = 'https://bitbucket.org';
|
||||
readonly type: GitProvider = "bitbucket";
|
||||
readonly name = "Bitbucket";
|
||||
readonly baseUrl = "https://bitbucket.org";
|
||||
|
||||
parseSource(source: string): { owner: string; repo: string; subpath?: string } | null {
|
||||
|
||||
if (source.startsWith('https://bitbucket.org/')) {
|
||||
const path = source.replace('https://bitbucket.org/', '').replace(/\.git$/, '');
|
||||
parseSource(
|
||||
source: string,
|
||||
): { owner: string; repo: string; subpath?: string } | null {
|
||||
if (source.startsWith("https://bitbucket.org/")) {
|
||||
const path = source
|
||||
.replace("https://bitbucket.org/", "")
|
||||
.replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (source.startsWith('git@bitbucket.org:')) {
|
||||
const path = source.replace('git@bitbucket.org:', '').replace(/\.git$/, '');
|
||||
if (source.startsWith("git@bitbucket.org:")) {
|
||||
const path = source
|
||||
.replace("git@bitbucket.org:", "")
|
||||
.replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (source.startsWith('bitbucket:')) {
|
||||
return parseShorthand(source.replace('bitbucket:', ''));
|
||||
if (source.startsWith("bitbucket:")) {
|
||||
return parseShorthand(source.replace("bitbucket:", ""));
|
||||
}
|
||||
|
||||
if (source.startsWith('bitbucket.org/')) {
|
||||
return parseShorthand(source.replace('bitbucket.org/', ''));
|
||||
if (source.startsWith("bitbucket.org/")) {
|
||||
return parseShorthand(source.replace("bitbucket.org/", ""));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -38,10 +43,10 @@ export class BitbucketProvider implements GitProviderAdapter {
|
||||
|
||||
matches(source: string): boolean {
|
||||
return (
|
||||
source.startsWith('https://bitbucket.org/') ||
|
||||
source.startsWith('git@bitbucket.org:') ||
|
||||
source.startsWith('bitbucket:') ||
|
||||
source.startsWith('bitbucket.org/')
|
||||
source.startsWith("https://bitbucket.org/") ||
|
||||
source.startsWith("git@bitbucket.org:") ||
|
||||
source.startsWith("bitbucket:") ||
|
||||
source.startsWith("bitbucket.org/")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -53,31 +58,36 @@ export class BitbucketProvider implements GitProviderAdapter {
|
||||
return `git@bitbucket.org:${owner}/${repo}.git`;
|
||||
}
|
||||
|
||||
async clone(source: string, _targetDir: string, options: CloneOptions = {}): Promise<CloneResult> {
|
||||
async clone(
|
||||
source: string,
|
||||
_targetDir: string,
|
||||
options: CloneOptions = {},
|
||||
): Promise<CloneResult> {
|
||||
const parsed = this.parseSource(source);
|
||||
if (!parsed) {
|
||||
return { success: false, error: `Invalid Bitbucket source: ${source}` };
|
||||
}
|
||||
|
||||
const { owner, repo, subpath } = parsed;
|
||||
const cloneUrl = options.ssh ? this.getSshUrl(owner, repo) : this.getCloneUrl(owner, repo);
|
||||
const cloneUrl = options.ssh
|
||||
? this.getSshUrl(owner, repo)
|
||||
: this.getCloneUrl(owner, repo);
|
||||
|
||||
const tempDir = join(tmpdir(), `skillkit-${randomUUID()}`);
|
||||
|
||||
try {
|
||||
|
||||
const args = ['clone'];
|
||||
const args = ["clone"];
|
||||
if (options.depth) {
|
||||
args.push('--depth', String(options.depth));
|
||||
args.push("--depth", String(options.depth));
|
||||
}
|
||||
if (options.branch) {
|
||||
args.push('--branch', options.branch);
|
||||
args.push("--branch", options.branch);
|
||||
}
|
||||
args.push(cloneUrl, tempDir);
|
||||
|
||||
execSync(`git ${args.join(' ')}`, {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
encoding: 'utf-8',
|
||||
execFileSync("git", args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const searchDir = subpath ? join(tempDir, subpath) : tempDir;
|
||||
@@ -85,17 +95,16 @@ export class BitbucketProvider implements GitProviderAdapter {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map(s => s.name),
|
||||
discoveredSkills: skills.map(s => ({
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map((s) => s.name),
|
||||
discoveredSkills: skills.map((s) => ({
|
||||
name: s.name,
|
||||
dirName: basename(s.path),
|
||||
path: s.path,
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
if (existsSync(tempDir)) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
+43
-37
@@ -1,31 +1,34 @@
|
||||
import { execSync } from 'node:child_process';
|
||||
import { existsSync, rmSync } from 'node:fs';
|
||||
import { join, basename } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { GitProviderAdapter, CloneOptions } from './base.js';
|
||||
import { parseShorthand, isGitUrl } from './base.js';
|
||||
import type { GitProvider, CloneResult } from '../core/types.js';
|
||||
import { discoverSkills } from '../core/skills.js';
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync, rmSync } from "node:fs";
|
||||
import { join, basename } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { GitProviderAdapter, CloneOptions } from "./base.js";
|
||||
import { parseShorthand, isGitUrl } from "./base.js";
|
||||
import type { GitProvider, CloneResult } from "../core/types.js";
|
||||
import { discoverSkills } from "../core/skills.js";
|
||||
|
||||
export class GitHubProvider implements GitProviderAdapter {
|
||||
readonly type: GitProvider = 'github';
|
||||
readonly name = 'GitHub';
|
||||
readonly baseUrl = 'https://github.com';
|
||||
readonly type: GitProvider = "github";
|
||||
readonly name = "GitHub";
|
||||
readonly baseUrl = "https://github.com";
|
||||
|
||||
parseSource(source: string): { owner: string; repo: string; subpath?: string } | null {
|
||||
|
||||
if (source.startsWith('https://github.com/')) {
|
||||
const path = source.replace('https://github.com/', '').replace(/\.git$/, '');
|
||||
parseSource(
|
||||
source: string,
|
||||
): { owner: string; repo: string; subpath?: string } | null {
|
||||
if (source.startsWith("https://github.com/")) {
|
||||
const path = source
|
||||
.replace("https://github.com/", "")
|
||||
.replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (source.startsWith('git@github.com:')) {
|
||||
const path = source.replace('git@github.com:', '').replace(/\.git$/, '');
|
||||
if (source.startsWith("git@github.com:")) {
|
||||
const path = source.replace("git@github.com:", "").replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (!isGitUrl(source) && !source.includes(':')) {
|
||||
if (!isGitUrl(source) && !source.includes(":")) {
|
||||
return parseShorthand(source);
|
||||
}
|
||||
|
||||
@@ -34,10 +37,9 @@ export class GitHubProvider implements GitProviderAdapter {
|
||||
|
||||
matches(source: string): boolean {
|
||||
return (
|
||||
source.startsWith('https://github.com/') ||
|
||||
source.startsWith('git@github.com:') ||
|
||||
|
||||
(!isGitUrl(source) && !source.includes(':') && source.includes('/'))
|
||||
source.startsWith("https://github.com/") ||
|
||||
source.startsWith("git@github.com:") ||
|
||||
(!isGitUrl(source) && !source.includes(":") && source.includes("/"))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -49,31 +51,36 @@ export class GitHubProvider implements GitProviderAdapter {
|
||||
return `git@github.com:${owner}/${repo}.git`;
|
||||
}
|
||||
|
||||
async clone(source: string, _targetDir: string, options: CloneOptions = {}): Promise<CloneResult> {
|
||||
async clone(
|
||||
source: string,
|
||||
_targetDir: string,
|
||||
options: CloneOptions = {},
|
||||
): Promise<CloneResult> {
|
||||
const parsed = this.parseSource(source);
|
||||
if (!parsed) {
|
||||
return { success: false, error: `Invalid GitHub source: ${source}` };
|
||||
}
|
||||
|
||||
const { owner, repo, subpath } = parsed;
|
||||
const cloneUrl = options.ssh ? this.getSshUrl(owner, repo) : this.getCloneUrl(owner, repo);
|
||||
const cloneUrl = options.ssh
|
||||
? this.getSshUrl(owner, repo)
|
||||
: this.getCloneUrl(owner, repo);
|
||||
|
||||
const tempDir = join(tmpdir(), `skillkit-${randomUUID()}`);
|
||||
|
||||
try {
|
||||
|
||||
const args = ['clone'];
|
||||
const args = ["clone"];
|
||||
if (options.depth) {
|
||||
args.push('--depth', String(options.depth));
|
||||
args.push("--depth", String(options.depth));
|
||||
}
|
||||
if (options.branch) {
|
||||
args.push('--branch', options.branch);
|
||||
args.push("--branch", options.branch);
|
||||
}
|
||||
args.push(cloneUrl, tempDir);
|
||||
|
||||
execSync(`git ${args.join(' ')}`, {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
encoding: 'utf-8',
|
||||
execFileSync("git", args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const searchDir = subpath ? join(tempDir, subpath) : tempDir;
|
||||
@@ -81,17 +88,16 @@ export class GitHubProvider implements GitProviderAdapter {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map(s => s.name),
|
||||
discoveredSkills: skills.map(s => ({
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map((s) => s.name),
|
||||
discoveredSkills: skills.map((s) => ({
|
||||
name: s.name,
|
||||
dirName: basename(s.path),
|
||||
path: s.path,
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
if (existsSync(tempDir)) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
+47
-40
@@ -1,36 +1,39 @@
|
||||
import { execSync } from 'node:child_process';
|
||||
import { existsSync, rmSync } from 'node:fs';
|
||||
import { join, basename } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { GitProviderAdapter, CloneOptions } from './base.js';
|
||||
import { parseShorthand } from './base.js';
|
||||
import type { GitProvider, CloneResult } from '../core/types.js';
|
||||
import { discoverSkills } from '../core/skills.js';
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync, rmSync } from "node:fs";
|
||||
import { join, basename } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { GitProviderAdapter, CloneOptions } from "./base.js";
|
||||
import { parseShorthand } from "./base.js";
|
||||
import type { GitProvider, CloneResult } from "../core/types.js";
|
||||
import { discoverSkills } from "../core/skills.js";
|
||||
|
||||
export class GitLabProvider implements GitProviderAdapter {
|
||||
readonly type: GitProvider = 'gitlab';
|
||||
readonly name = 'GitLab';
|
||||
readonly baseUrl = 'https://gitlab.com';
|
||||
readonly type: GitProvider = "gitlab";
|
||||
readonly name = "GitLab";
|
||||
readonly baseUrl = "https://gitlab.com";
|
||||
|
||||
parseSource(source: string): { owner: string; repo: string; subpath?: string } | null {
|
||||
|
||||
if (source.startsWith('https://gitlab.com/')) {
|
||||
const path = source.replace('https://gitlab.com/', '').replace(/\.git$/, '');
|
||||
parseSource(
|
||||
source: string,
|
||||
): { owner: string; repo: string; subpath?: string } | null {
|
||||
if (source.startsWith("https://gitlab.com/")) {
|
||||
const path = source
|
||||
.replace("https://gitlab.com/", "")
|
||||
.replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (source.startsWith('git@gitlab.com:')) {
|
||||
const path = source.replace('git@gitlab.com:', '').replace(/\.git$/, '');
|
||||
if (source.startsWith("git@gitlab.com:")) {
|
||||
const path = source.replace("git@gitlab.com:", "").replace(/\.git$/, "");
|
||||
return parseShorthand(path);
|
||||
}
|
||||
|
||||
if (source.startsWith('gitlab:')) {
|
||||
return parseShorthand(source.replace('gitlab:', ''));
|
||||
if (source.startsWith("gitlab:")) {
|
||||
return parseShorthand(source.replace("gitlab:", ""));
|
||||
}
|
||||
|
||||
if (source.startsWith('gitlab.com/')) {
|
||||
return parseShorthand(source.replace('gitlab.com/', ''));
|
||||
if (source.startsWith("gitlab.com/")) {
|
||||
return parseShorthand(source.replace("gitlab.com/", ""));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -38,10 +41,10 @@ export class GitLabProvider implements GitProviderAdapter {
|
||||
|
||||
matches(source: string): boolean {
|
||||
return (
|
||||
source.startsWith('https://gitlab.com/') ||
|
||||
source.startsWith('git@gitlab.com:') ||
|
||||
source.startsWith('gitlab:') ||
|
||||
source.startsWith('gitlab.com/')
|
||||
source.startsWith("https://gitlab.com/") ||
|
||||
source.startsWith("git@gitlab.com:") ||
|
||||
source.startsWith("gitlab:") ||
|
||||
source.startsWith("gitlab.com/")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -53,31 +56,36 @@ export class GitLabProvider implements GitProviderAdapter {
|
||||
return `git@gitlab.com:${owner}/${repo}.git`;
|
||||
}
|
||||
|
||||
async clone(source: string, _targetDir: string, options: CloneOptions = {}): Promise<CloneResult> {
|
||||
async clone(
|
||||
source: string,
|
||||
_targetDir: string,
|
||||
options: CloneOptions = {},
|
||||
): Promise<CloneResult> {
|
||||
const parsed = this.parseSource(source);
|
||||
if (!parsed) {
|
||||
return { success: false, error: `Invalid GitLab source: ${source}` };
|
||||
}
|
||||
|
||||
const { owner, repo, subpath } = parsed;
|
||||
const cloneUrl = options.ssh ? this.getSshUrl(owner, repo) : this.getCloneUrl(owner, repo);
|
||||
const cloneUrl = options.ssh
|
||||
? this.getSshUrl(owner, repo)
|
||||
: this.getCloneUrl(owner, repo);
|
||||
|
||||
const tempDir = join(tmpdir(), `skillkit-${randomUUID()}`);
|
||||
|
||||
try {
|
||||
|
||||
const args = ['clone'];
|
||||
const args = ["clone"];
|
||||
if (options.depth) {
|
||||
args.push('--depth', String(options.depth));
|
||||
args.push("--depth", String(options.depth));
|
||||
}
|
||||
if (options.branch) {
|
||||
args.push('--branch', options.branch);
|
||||
args.push("--branch", options.branch);
|
||||
}
|
||||
args.push(cloneUrl, tempDir);
|
||||
|
||||
execSync(`git ${args.join(' ')}`, {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
encoding: 'utf-8',
|
||||
execFileSync("git", args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const searchDir = subpath ? join(tempDir, subpath) : tempDir;
|
||||
@@ -85,17 +93,16 @@ export class GitLabProvider implements GitProviderAdapter {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map(s => s.name),
|
||||
discoveredSkills: skills.map(s => ({
|
||||
path: searchDir,
|
||||
tempRoot: tempDir,
|
||||
skills: skills.map((s) => s.name),
|
||||
discoveredSkills: skills.map((s) => ({
|
||||
name: s.name,
|
||||
dirName: basename(s.path),
|
||||
path: s.path,
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
if (existsSync(tempDir)) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user