Creating a seed file for fuzzing a JavaScript engine designed to explore a variety of language features and potential edge cases can be quite intricate due to the diversity of the JavaScript language itself. This file aims to touch upon different aspects such as function calls, prototypes, async behavior, exceptions, and more, to expose the engine to a wide range of scenarios.
Please note, the effectiveness of fuzzing greatly depends on the engine's implementation details, and some constructs might be more interesting than others based on past vulnerabilities or engine-specific quirks.
This example is a starting point and by no means exhaustive. It's highly recommended to tailor your seed files to the particular engine you're testing and evolve them based on your findings.
// Define a complex object to test property handling and prototype chain
let obj = {
a: 10,
b: "text",
c: function() { return this.a; },
d: { e: { f: "deep" } }
};
obj.__proto__ = {g: "proto"};
Object.defineProperty(obj, "hidden", {value: "secret", enumerable: false});
// Exploit Array methods and callback behaviors
let arr = [1,2,3];
arr.map(x => x * 10);
arr.reduce((acc, val) => acc + val, 0);
arr.forEach(v => { if(v === 20) throw new Error("Test"); });
// Async behavior and Promises
async function testAsync() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done"), 1000);
});
result = await promise;
console.log(result);
}
testAsync();
// Generators and Iterators
function* generateSequence() {
yield 1;
yield 2;
return 3;
}
let generator = generateSequence();
for(let value of generator) {
console.log(value);
}
// Exploiting the prototype chain and constructor functions
function Person(name) {
this.name = name;
}
Person.prototype.sayName = function() {
console.log(this.name);
};
let person = new Person("JSFuzz");
person.sayName();
// Dynamic code execution
eval("console.log('Eval test');");
Function("console.log('Function Constructor test');")();
// Handling exceptions and specific edge cases
try {
null.f();
} catch (e) {
console.log("Caught", e);
}
// Exploiting Math and Date objects
console.log(Math.sin(Math.PI / 2));
console.log(new Date().toISOString());
// Regular expressions and string manipulation
"Hello World!".replace(/(\w+)\s(\w+)/, "$2, $1");
"abc123".match(/\d+/)[0];
// Handling JSON
JSON.parse('{"validJson": true}');
JSON.stringify({objectToSerialize: "test"});
// Exploring Maps and Sets
let map = new Map();
map.set(obj, "testObject");
map.get(obj);
let set = new Set([1, 2, obj]);
set.add(function() { return "hello from set"; });
// Symbol usage and property uniqueness
let sym = Symbol("unique");
let o = {[sym]: "uniqueVal"};
console.log(o[sym]);
// Finally, some ES6+ features like spread/rest, template literals
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(`Sum: ${sum(...numbers)}`);
// Template literal with embedded expression
let name = "fuzz";
console.log(`Hello, ${name}!`);
// Proxy for interception and meta programming
let target = {};
let proxy = new Proxy(target, {
get(target, prop, receiver) {
console.log(`get ${prop}`);
return 123;
}
});
proxy.test;
Remember, the goal of fuzzing is to discover unexpected behaviors, crashes, or security vulnerabilities by automatically generating a wide variety of inputs. Continuously evolving your seed files based on the feedback and results from your fuzzing efforts is crucial for success.