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
11 changes: 9 additions & 2 deletions examples/shared/datafile-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@
/**
* Simple HTTP server to serve the datafile for testing polling project config manager
* Runs at http://localhost:PORT
*
* It listens on the loopback interface only, so it can't be reached from other machines on the network.
*/

const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 8910;
// Loopback only. Without a host, Node listens on every network interface.
const HOST = 'localhost';
const datafilePath = path.join(__dirname, 'datafile.json');

// Plain HTTP is intended here: this is a local test server for the examples. It only listens on localhost
// (see HOST) and serves the sample datafile.json, so nothing sensitive crosses a network.
// nosemgrep: problem-based-packs.insecure-transport.js-node.using-http-server.using-http-server
const server = http.createServer((req, res) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);

Expand All @@ -52,9 +59,9 @@ const server = http.createServer((req, res) => {
});
});

server.listen(PORT, () => {
server.listen(PORT, HOST, () => {
console.log(`\n=== Datafile Server Started ===`);
console.log(`Server running at http://localhost:${PORT}`);
console.log(`Server running at http://${HOST}:${PORT}`);
console.log(`Serving datafile from: ${datafilePath}`);
console.log(`\nPress Ctrl+C to stop the server\n`);
});
Expand Down
63 changes: 0 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"chai": "^4.2.0",
"coveralls-next": "^5.0.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-local-rules": "^3.0.2",
"eslint-plugin-prettier": "^3.1.2",
"happy-dom": "^20.0.11",
"minimatch": "^9.0.5",
"mocha": "^10.2.0",
"mocha-lcov-reporter": "^1.3.0",
"nise": "^6.1.1",
"nock": "11.9.1",
"nyc": "^15.0.1",
Expand Down
5 changes: 5 additions & 0 deletions rolldown.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const RN_IMPORT_SHIM_DIR = 'import.react_native';
// Shim paths relative to the shim directory, without extension. Shims are named after their package, so
// a scoped package's shim is in a subdirectory, e.g. '@react-native-async-storage/async-storage'.
const listShims = (subdir = '') =>
// subdir only comes from readdirSync of this directory in the repo (a directory entry name never contains a
// path separator or '..'), not from user input, so it can't leave lib/import.react_native.
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
readdirSync(join(__dirname, 'lib', RN_IMPORT_SHIM_DIR, subdir), { withFileTypes: true }).flatMap(entry => {
const path = subdir ? `${subdir}/${entry.name}` : entry.name;
if (entry.isDirectory()) return listShims(path);
Expand Down Expand Up @@ -126,6 +129,8 @@ const rnImportShimCheckPlugin = {

const rnImportShimsBundle = {
input: Object.fromEntries(
// name comes from listShims() above (file names in lib/import.react_native), not from user input.
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
RN_IMPORT_SHIMS.map(name => [name, join(BUILD_DIR, RN_IMPORT_SHIM_DIR, `${name}.js`)])
),
output: {
Expand Down
16 changes: 16 additions & 0 deletions scripts/generate-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,25 @@ function buildOutputAst(exports) {
return factory.createSourceFile(statements, factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None);
}

// The input is a command line argument. Only a TypeScript source is accepted, and never a generated file, so the
// script only reads a .ts file and only writes <name>.gen.ts next to it.
function assertTypeScriptSource(filePath) {
const { name, ext } = path.parse(filePath);
if (ext !== '.ts' || name.endsWith('.gen')) {
console.error(`generate-messages.js: expected a .ts source file that is not a generated .gen.ts file, got: ${filePath}`);
process.exit(1);
}
}

function generate(filePath) {
assertTypeScriptSource(filePath);

// The path is checked above and chosen by whoever runs this script on their own machine, not by remote input.
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
const absPath = path.resolve(filePath);
const parsed = path.parse(absPath);
// parsed.dir and parsed.name come from the checked path above, and <name>.gen.ts is written next to the input.
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
const genPath = path.join(parsed.dir, `${parsed.name}.gen${parsed.ext}`);

console.log('generating messages for:', absPath);
Expand Down
3 changes: 3 additions & 0 deletions scripts/platform-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ function findSourceFiles() {
const entries = fs.readdirSync(dir, { withFileTypes: true });

for (const entry of entries) {
// entry.name is a file name from readdirSync of a folder in the repo. It never contains a path separator
// or '..', so fullPath stays inside the folder being walked.
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
const fullPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
Expand Down
34 changes: 22 additions & 12 deletions scripts/run-cjs-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,46 @@
* limitations under the License.
*/

const { execSync } = require('child_process');
const { execFileSync } = require('child_process');
const path = require('path');
const fs = require('fs');

const rootDir = path.join(__dirname, '..');
const exampleDir = path.join(rootDir, 'examples', 'node-commonjs');

function run(command, cwd) {
// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows,
// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the
// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg).
const isWindows = process.platform === 'win32';
const NPM = isWindows ? 'npm.cmd' : 'npm';

function run(args, cwd) {
const command = `npm ${args.join(' ')}`;
console.log(`\n> ${command}`);
console.log(` (in ${cwd})\n`);
try {
execSync(command, {
execFileSync(NPM, args, {
cwd,
stdio: 'inherit',
shell: true
shell: isWindows
});
} catch (error) {
console.error(`\nError executing: ${command}`);
process.exit(1);
}
}

function runQuiet(command, cwd) {
function runQuiet(args, cwd) {
try {
const output = execSync(command, {
const output = execFileSync(NPM, args, {
cwd,
encoding: 'utf8',
shell: true
shell: isWindows
}).trim();
const lines = output.split('\n');
return lines[lines.length - 1].trim();
} catch (error) {
console.error(`\nError executing: ${command}`);
console.error(`\nError executing: npm ${args.join(' ')}`);
process.exit(1);
}
}
Expand Down Expand Up @@ -84,20 +91,23 @@ function main() {
tarballPath = findTarball();
} else {
console.log('Installing SDK dependencies...');
run('npm install', rootDir);
run(['install'], rootDir);

console.log('\nPacking SDK tarball...');
const packOutput = runQuiet('npm pack', rootDir);
const packOutput = runQuiet(['pack'], rootDir);
tarballPath = path.join(rootDir, packOutput);
console.log(`Created: ${packOutput}`);
}

console.log('\nInstalling SDK tarball in cjs-example...');
run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir);
// Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe)
// checkout path is not part of the command line.
const tarballArg = path.relative(exampleDir, tarballPath);
run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir);

if (!skipRun) {
console.log('\nRunning cjs-example...');
run('npm start', exampleDir);
run(['start'], exampleDir);
}

console.log('\nCleaning up tarball...');
Expand Down
38 changes: 24 additions & 14 deletions scripts/run-ts-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

const { execSync, spawn } = require('child_process');
const { execFileSync, spawn } = require('child_process');
const path = require('path');
const fs = require('fs');

Expand All @@ -25,14 +25,21 @@ const exampleDir = path.join(rootDir, 'examples', 'typescript');

let datafileServer = null;

function run(command, cwd) {
// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows,
// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the
// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg).
const isWindows = process.platform === 'win32';
const NPM = isWindows ? 'npm.cmd' : 'npm';

function run(args, cwd) {
const command = `npm ${args.join(' ')}`;
console.log(`\n> ${command}`);
console.log(` (in ${cwd})\n`);
try {
execSync(command, {
execFileSync(NPM, args, {
cwd,
stdio: 'inherit',
shell: true
shell: isWindows
Comment thread
esrakartalOpt marked this conversation as resolved.
Resolved
});
} catch (error) {
console.error(`\nError executing: ${command}`);
Expand All @@ -41,18 +48,18 @@ function run(command, cwd) {
}
}

function runQuiet(command, cwd) {
function runQuiet(args, cwd) {
try {
const output = execSync(command, {
const output = execFileSync(NPM, args, {
cwd,
encoding: 'utf8',
shell: true
shell: isWindows
}).trim();
// npm pack outputs the tarball filename on the last line
const lines = output.split('\n');
return lines[lines.length - 1].trim();
} catch (error) {
console.error(`\nError executing: ${command}`);
console.error(`\nError executing: npm ${args.join(' ')}`);
cleanup();
process.exit(1);
}
Expand Down Expand Up @@ -103,26 +110,29 @@ async function main() {
console.log('=== Building SDK and Running TypeScript Example ===\n');

console.log('Installing SDK dependencies...');
run('npm install', rootDir);
run(['install'], rootDir);

console.log('\nPacking SDK tarball...');
const packOutput = runQuiet('npm pack', rootDir);
const packOutput = runQuiet(['pack'], rootDir);
const tarballPath = path.join(rootDir, packOutput);
console.log(`Created: ${packOutput}`);

console.log('\nInstalling ts-example devDependencies...');
run('npm ci', exampleDir);
run(['ci'], exampleDir);

console.log('\nInstalling SDK tarball in ts-example...');
run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir);
// Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe)
// checkout path is not part of the command line.
const tarballArg = path.relative(exampleDir, tarballPath);
run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir);

console.log('\nBuilding ts-example...');
run('npm run build', exampleDir);
run(['run', 'build'], exampleDir);

await startDatafileServer();

console.log('\nRunning ts-example...');
run('npm start', exampleDir);
run(['start'], exampleDir);

console.log('\nCleaning up tarball...');
fs.unlinkSync(tarballPath);
Expand Down
Loading
Loading