Skip to content
Closed
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
54 changes: 33 additions & 21 deletions actions/setup/js/runtime_import.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ const ALLOWED_EXPRESSIONS = [
"github.workspace",
];

/**
* Dangerous JavaScript built-in property names that could be used for
* prototype pollution or prototype chain traversal attacks.
* These properties are blocked in all expressions.
*/
const DANGEROUS_PROPS = [
"constructor",
"__proto__",
"prototype",
"__defineGetter__",
"__defineSetter__",
"__lookupGetter__",
"__lookupSetter__",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toString",
"valueOf",
"toLocaleString",
];

/**
* Safely checks if an object has a property without accessing the prototype chain
* @param {any} obj - The object to check
* @param {string} prop - The property name to check
* @returns {boolean} - True if the object has the property (not inherited)
*/
function hasSafeProperty(obj, prop) {
return obj && typeof obj === "object" && Object.prototype.hasOwnProperty.call(obj, prop);
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

hasSafeProperty() can return a non-boolean falsy value due to short-circuiting (e.g., null, undefined, 0, ""). Since the JSDoc declares a boolean return and callers treat it as a predicate, wrap the expression in Boolean(...)/!! so the function always returns a real boolean.

Suggested change
return obj && typeof obj === "object" && Object.prototype.hasOwnProperty.call(obj, prop);
return Boolean(
obj && typeof obj === "object" && Object.prototype.hasOwnProperty.call(obj, prop)
);

Copilot uses AI. Check for mistakes.
}

/**
* Checks if an expression is in the safe list
* @param {string} expr - The expression to check (without ${{ }})
Expand All @@ -116,23 +147,6 @@ const ALLOWED_EXPRESSIONS = [
function isSafeExpression(expr) {
const trimmed = expr.trim();

// Block dangerous JavaScript built-in property names
const DANGEROUS_PROPS = [
"constructor",
"__proto__",
"prototype",
"__defineGetter__",
"__defineSetter__",
"__lookupGetter__",
"__lookupSetter__",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toString",
"valueOf",
"toLocaleString",
];

// Split expression into parts and check each for dangerous properties
// Handle both dot notation (e.g., "github.event.issue") and bracket notation (e.g., "release.assets[0].id")
const parts = trimmed.split(/[.\[\]]+/).filter(p => p && !/^\d+$/.test(p));
Expand Down Expand Up @@ -294,8 +308,7 @@ function evaluateExpression(expr) {
if (arrayMatch) {
const key = arrayMatch[1];
const index = parseInt(arrayMatch[2], 10);
// Use Object.prototype.hasOwnProperty.call() to prevent prototype chain access
if (value && typeof value === "object" && Object.prototype.hasOwnProperty.call(value, key)) {
if (hasSafeProperty(value, key)) {
const arrayValue = value[key];
if (Array.isArray(arrayValue) && index >= 0 && index < arrayValue.length) {
value = arrayValue[index];
Expand All @@ -308,8 +321,7 @@ function evaluateExpression(expr) {
break;
}
} else {
// Use Object.prototype.hasOwnProperty.call() to prevent prototype chain access
if (value && typeof value === "object" && Object.prototype.hasOwnProperty.call(value, part)) {
if (hasSafeProperty(value, part)) {
value = value[part];
} else {
value = undefined;
Expand Down
110 changes: 40 additions & 70 deletions actions/setup/js/runtime_import.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,88 +124,58 @@ describe("runtime_import", () => {
expect(isSafeExpression("vars.SECRET || 'fallback'")).toBe(!1);
});
it("should reject OR with unsafe right side (non-literal)", () => {
expect(isSafeExpression("inputs.value || secrets.TOKEN")).toBe(!1);
expect(isSafeExpression("github.actor || vars.NAME")).toBe(!1);
expect(isSafeExpression("inputs.value || secrets.TOKEN")).toBe(false);
expect(isSafeExpression("github.actor || vars.NAME")).toBe(false);
});
it("should block dangerous property names - constructor", () => {
expect(isSafeExpression("github.constructor")).toBe(!1);
expect(isSafeExpression("inputs.constructor")).toBe(!1);
expect(isSafeExpression("github.event.constructor")).toBe(!1);
expect(isSafeExpression("needs.job.constructor")).toBe(!1);
});
it("should block dangerous property names - __proto__", () => {
expect(isSafeExpression("github.__proto__")).toBe(!1);
expect(isSafeExpression("inputs.__proto__")).toBe(!1);
expect(isSafeExpression("github.event.__proto__")).toBe(!1);
});
it("should block dangerous property names - prototype", () => {
expect(isSafeExpression("github.prototype")).toBe(!1);
expect(isSafeExpression("inputs.prototype")).toBe(!1);
expect(isSafeExpression("github.event.prototype")).toBe(!1);
});
it("should block dangerous property names - __defineGetter__", () => {
expect(isSafeExpression("github.__defineGetter__")).toBe(!1);
expect(isSafeExpression("inputs.__defineGetter__")).toBe(!1);
});
it("should block dangerous property names - __defineSetter__", () => {
expect(isSafeExpression("github.__defineSetter__")).toBe(!1);
expect(isSafeExpression("inputs.__defineSetter__")).toBe(!1);
});
it("should block dangerous property names - __lookupGetter__", () => {
expect(isSafeExpression("github.__lookupGetter__")).toBe(!1);
expect(isSafeExpression("inputs.__lookupGetter__")).toBe(!1);
});
it("should block dangerous property names - __lookupSetter__", () => {
expect(isSafeExpression("github.__lookupSetter__")).toBe(!1);
expect(isSafeExpression("inputs.__lookupSetter__")).toBe(!1);
});
it("should block dangerous property names - hasOwnProperty", () => {
expect(isSafeExpression("github.hasOwnProperty")).toBe(!1);
expect(isSafeExpression("inputs.hasOwnProperty")).toBe(!1);
});
it("should block dangerous property names - isPrototypeOf", () => {
expect(isSafeExpression("github.isPrototypeOf")).toBe(!1);
expect(isSafeExpression("inputs.isPrototypeOf")).toBe(!1);
});
it("should block dangerous property names - propertyIsEnumerable", () => {
expect(isSafeExpression("github.propertyIsEnumerable")).toBe(!1);
expect(isSafeExpression("inputs.propertyIsEnumerable")).toBe(!1);
});
it("should block dangerous property names - toString", () => {
expect(isSafeExpression("github.toString")).toBe(!1);
expect(isSafeExpression("inputs.toString")).toBe(!1);
});
it("should block dangerous property names - valueOf", () => {
expect(isSafeExpression("github.valueOf")).toBe(!1);
expect(isSafeExpression("inputs.valueOf")).toBe(!1);
});
it("should block dangerous property names - toLocaleString", () => {
expect(isSafeExpression("github.toLocaleString")).toBe(!1);
expect(isSafeExpression("inputs.toLocaleString")).toBe(!1);
});
it("should block dangerous properties in array access", () => {
expect(isSafeExpression("github.event.release.assets[0].constructor")).toBe(!1);
expect(isSafeExpression("github.event.release.assets[0].__proto__")).toBe(!1);
it("should block dangerous property names", () => {
const dangerousProps = [
"constructor",
"__proto__",
"prototype",
"__defineGetter__",
"__defineSetter__",
"__lookupGetter__",
"__lookupSetter__",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toString",
"valueOf",
"toLocaleString",
];

for (const prop of dangerousProps) {
expect(isSafeExpression(`github.${prop}`)).toBe(false);
expect(isSafeExpression(`inputs.${prop}`)).toBe(false);
expect(isSafeExpression(`github.event.${prop}`)).toBe(false);
}

// Additional tests for constructor
expect(isSafeExpression("needs.job.constructor")).toBe(false);

// Tests for dangerous properties in array access
expect(isSafeExpression("github.event.release.assets[0].constructor")).toBe(false);
expect(isSafeExpression("github.event.release.assets[0].__proto__")).toBe(false);
});
it("should reject excessive nesting depth (more than 5 levels)", () => {
// Valid: max 5 levels (needs.job.outputs.foo.bar)
expect(isSafeExpression("needs.job.outputs.foo.bar")).toBe(!0);
expect(isSafeExpression("steps.step.outputs.foo.bar")).toBe(!0);
expect(isSafeExpression("needs.job.outputs.foo.bar")).toBe(true);
expect(isSafeExpression("steps.step.outputs.foo.bar")).toBe(true);
// Invalid: 6 levels
expect(isSafeExpression("needs.job.outputs.foo.bar.baz")).toBe(!1);
expect(isSafeExpression("steps.step.outputs.foo.bar.baz")).toBe(!1);
expect(isSafeExpression("needs.job.outputs.foo.bar.baz")).toBe(false);
expect(isSafeExpression("steps.step.outputs.foo.bar.baz")).toBe(false);
// Invalid: 7+ levels
expect(isSafeExpression("needs.job.outputs.foo.bar.baz.qux")).toBe(!1);
expect(isSafeExpression("needs.job.outputs.foo.bar.baz.qux")).toBe(false);
});
it("should allow valid nested expressions within depth limit", () => {
// 2 levels
expect(isSafeExpression("needs.job.outputs")).toBe(!0);
expect(isSafeExpression("needs.job.outputs")).toBe(true);
// 3 levels
expect(isSafeExpression("needs.job.outputs.result")).toBe(!0);
expect(isSafeExpression("needs.job.outputs.result")).toBe(true);
// 4 levels
expect(isSafeExpression("needs.job.outputs.foo.value")).toBe(!0);
expect(isSafeExpression("needs.job.outputs.foo.value")).toBe(true);
// 5 levels (max)
expect(isSafeExpression("needs.job.outputs.foo.bar")).toBe(!0);
expect(isSafeExpression("needs.job.outputs.foo.bar")).toBe(true);
});
}),
describe("evaluateExpression", () => {
Expand Down
Loading