Skip to content
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
101 changes: 101 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL Advanced"

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
schedule:
- cron: "35 16 * * 1"

jobs:
analyze:
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners (GitHub.com only)
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
runs-on: "ubuntu-latest"
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read

strategy:
fail-fast: false
matrix:
include:
- language: actions
build-mode: none
- language: javascript-typescript
build-mode: none
# CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@v6

# Add any setup steps before running the `github/codeql-action/init` action.
# This includes steps like installing compilers or runtimes (`actions/setup-node`
# or others). This is typically only required for manual builds.
# - name: Setup runtime (example)
# uses: actions/setup-example@v1

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- name: Run manual build steps
if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{matrix.language}}"
26 changes: 18 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Push Code
name: Release Pipeline

on:
pull_request_review:
Expand All @@ -12,10 +12,18 @@ permissions:
packages: write
id-token: write

concurrency:
group: release-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
NODE_VERSION: "25"

jobs:
check:
name: Validate
runs-on: ubuntu-latest
if: |
if: >
github.event_name == 'push' ||
(github.event_name == 'pull_request_review' &&
github.event.review.state == 'approved')
Expand All @@ -27,19 +35,20 @@ jobs:
- name: Setup Node.js (with npm cache)
uses: actions/setup-node@v4.0.2
with:
node-version: "25"
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
registry-url: "https://npm.pkg.github.com"
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: npm install
run: npm ci

- name: Check Compatibility
run: npm run check

release_git:
name: Publish GitHub Release
needs: check
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
Expand All @@ -53,14 +62,14 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4.0.2
with:
node-version: "25"
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
registry-url: "https://npm.pkg.github.com"
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: npm install
run: npm ci

- name: Install git-cliff
uses: taiki-e/install-action@git-cliff
Expand All @@ -73,6 +82,7 @@ jobs:
GITHUB_SHA: ${{ github.sha }}

release_npm:
name: Publish npm Package
needs: check
environment: npm
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
Expand All @@ -87,12 +97,12 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4.0.2
with:
node-version: "25"
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
registry-url: "https://registry.npmjs.org"

- name: Install dependencies
run: npm install
run: npm ci

- name: Run npm release script
run: node scripts/actions/npm.js
Expand Down
34 changes: 19 additions & 15 deletions scripts/actions/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ import {
tagExists,
createRelease,
} from "../utils/github.js";
import { checkVersionExists, GITHUB_URL, getNpmDistTag } from "../utils/npm.js";
import { exec, generateChangelog, isMain } from "../utils/util.js";
import {
checkVersionExists,
GITHUB_URL,
publishTarball,
} from "../utils/npm.js";
import { generateChangelog, isMain } from "../utils/util.js";

async function buildProject() {
const github_token = process.env.GITHUB_TOKEN;
if (!github_token) {
async function runGitHubRelease() {
const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
throw new Error("Github Token Not Found");
}

const { owner, repo } = getRepoInfo();
const sha = getSha();

const tempjson = {
const releasePackageJson = {
...packageJson,
name: `@${owner}/${packageJson.name}`,
};

const version = tempjson.version;
const version = releasePackageJson.version;

if (!version) {
throw new Error("package.json version not found");
Expand All @@ -34,12 +38,16 @@ async function buildProject() {
console.log(`Version: ${version}`);
console.log(`Current commit: ${sha.slice(0, 7)}`);

const npmVerExists = checkVersionExists(tempjson.name, version, GITHUB_URL);
const npmVerExists = checkVersionExists(
releasePackageJson.name,
version,
GITHUB_URL
);
Comment on lines +41 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The checkVersionExists function (defined in scripts/utils/npm.js) is vulnerable to command injection because it interpolates its arguments directly into a shell command string. In this call, releasePackageJson.name and version are passed as arguments. These values are derived from package.json, which is an untrusted source in the context of CI/CD. An attacker could modify package.json to include shell metacharacters (e.g., ; touch /tmp/pwned), leading to arbitrary command execution in the CI environment. To remediate this, the checkVersionExists function should be updated to use execFileSync or spawnSync to pass arguments safely without shell interpolation.

const githubTagExists = tagExists(version);

let buildPath = null;
const ensureBuildPath = async () => {
buildPath ??= await build(tempjson);
buildPath ??= await build(releasePackageJson);
return buildPath;
};

Expand Down Expand Up @@ -67,11 +75,7 @@ async function buildProject() {

const tarballPath = await ensureBuildPath();

const distTag = getNpmDistTag(version);
const tagArg = distTag === "latest" ? "" : ` --tag ${distTag}`;
exec(`npm publish "${tarballPath}" --registry=${GITHUB_URL}${tagArg}`, {
stdio: "inherit",
});
publishTarball(tarballPath, GITHUB_URL, version);
} catch (error) {
console.log(error);
throw new Error("Failed to publish package to GitHub Packages");
Expand All @@ -80,7 +84,7 @@ async function buildProject() {
}

if (isMain(import.meta.url)) {
buildProject().catch((err) => {
runGitHubRelease().catch((err) => {
console.error("Patch failed:", err);
process.exit(1);
});
Expand Down
29 changes: 13 additions & 16 deletions scripts/actions/npm.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import packageJson from "../../package.json" with { type: "json" };
import build from "../utils/build.js";
import { getSha } from "../utils/github.js";
import { checkVersionExists, NPM_URL, getNpmDistTag } from "../utils/npm.js";
import { exec, isMain } from "../utils/util.js";
import { checkVersionExists, NPM_URL, publishTarball } from "../utils/npm.js";
import { isMain } from "../utils/util.js";

async function buildProject() {
async function runNpmRelease() {
const owner = process.env.NPM_ORG;
const sha = getSha();

const tempjson = {
const releasePackageJson = {
...packageJson,
name: owner ? `@${owner}/${packageJson.name}` : packageJson.name,
};

const version = tempjson.version;
const version = releasePackageJson.version;

if (!version) {
throw new Error("package.json version not found");
Expand All @@ -23,11 +23,15 @@ async function buildProject() {
console.log(`Version: ${version}`);
console.log(`Current commit: ${sha.slice(0, 7)}`);

const npmVerExists = checkVersionExists(tempjson.name, version, NPM_URL);
const npmVerExists = checkVersionExists(
releasePackageJson.name,
version,
NPM_URL
);
Comment on lines +26 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The checkVersionExists function (defined in scripts/utils/npm.js) is vulnerable to command injection because it interpolates its arguments directly into a shell command string. In this call, releasePackageJson.name and version are passed as arguments. These values are derived from package.json, which is an untrusted source in the context of CI/CD. An attacker could modify package.json to include shell metacharacters, leading to arbitrary command execution in the CI environment. To remediate this, the checkVersionExists function should be updated to use execFileSync or spawnSync to pass arguments safely without shell interpolation.


let buildPath = null;
const ensureBuildPath = async () => {
buildPath ??= await build(tempjson);
buildPath ??= await build(releasePackageJson);
return buildPath;
};

Expand All @@ -39,14 +43,7 @@ async function buildProject() {

const tarballPath = await ensureBuildPath();

const distTag = getNpmDistTag(version);
const tagArg = distTag === "latest" ? "" : ` --tag ${distTag}`;
exec(
`npm publish "${tarballPath}" --provenance --registry=${NPM_URL}${tagArg}`,
{
stdio: "inherit",
}
);
publishTarball(tarballPath, NPM_URL, version, { provenance: true });
} catch (error) {
console.log(error);
throw new Error("Failed to publish package to npm");
Expand All @@ -55,7 +52,7 @@ async function buildProject() {
}

if (isMain(import.meta.url)) {
buildProject().catch((err) => {
runNpmRelease().catch((err) => {
console.error("Patch failed:", err);
process.exit(1);
});
Expand Down
27 changes: 23 additions & 4 deletions scripts/utils/npm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from "./util.js";
import { execFileSync } from "node:child_process";

export const NPM_URL = "https://registry.npmjs.org";
export const GITHUB_URL = "https://npm.pkg.github.com";
Expand All @@ -10,11 +10,30 @@ export function getNpmDistTag(version) {

export function checkVersionExists(packageName, version, registry) {
try {
exec(`npm view ${packageName}@${version} --registry=${registry}`, {
stdio: "ignore",
});
execFileSync(
"npm",
["view", `${packageName}@${version}`, `--registry=${registry}`],
{
stdio: "ignore",
}
);
return true;
} catch {
return false;
}
}

export function publishTarball(tarballPath, registry, version, options = {}) {
const { provenance = false } = options;
const distTag = getNpmDistTag(version);
const args = ["publish", tarballPath];
if (provenance) {
args.push("--provenance");
}
args.push(`--registry=${registry}`);
if (distTag !== "latest") {
args.push("--tag", distTag);
}

execFileSync("npm", args, { stdio: "inherit" });
}
7 changes: 2 additions & 5 deletions src/structures/builder/Builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
SlashCommandBuilder,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from "discord.js";
import { SlashCommandBuilder } from "discord.js";
import { Client } from "@structures/core/index.js";
import { normalizeArray } from "@utils/normalizeArray.js";

import type { RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord.js";
export interface ApplicationJSONBody extends RESTPostAPIChatInputApplicationCommandsJSONBody {
prefix_support: boolean;
slash_support: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/builder/Event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientEvents } from "discord.js";
import type { ClientEvents } from "discord.js";
import type { MaybePromise } from "#types/extra.js";
import { currentClient } from "@context";
import { Client } from "@structures/index.js";
Expand Down
Loading