codeflash/tests
mohammed ahmed e0a0671cf3 Fix CRITICAL JavaScript code injection vulnerability in Jest config generation
**Issue #17:** Unsanitized file paths in f-string interpolation can inject
arbitrary JavaScript code into the generated Jest config file.

**Severity:** CRITICAL

**Root Cause:**
File: /opt/codeflash/codeflash/languages/javascript/test_runner.py:516, 524, 565

Three locations used f-string interpolation to embed paths into JavaScript code
without escaping:

1. Line 516: `test_dirs_js = ", ".join(f"'{d}'" for d in sorted(test_dirs))`
2. Line 524: `f"moduleDirectories: [..., '{monorepo_node_modules}'],"`
3. Line 565: `f"roots: ['{project_root}', {test_dirs_js}],"`

If any path contains a single quote (`'`), it breaks out of the string and
executes arbitrary JavaScript. Example:

**Malicious path:** `/tmp/test']; console.log('INJECTED'); roots=['`

**Vulnerable output:**
```javascript
roots: ['/project', '/tmp/test']; console.log('INJECTED'); roots=[''],
                                ^-- breaks string, executes code
```

**Impact:**
- **Code injection:** Arbitrary JavaScript execution when Jest loads config
- **Attack vector:** User-controlled paths (test directories, monorepo paths)
- **Scope:** Any project where test dirs or project root contains quote char
- **Risk:** HIGH - While uncommon, paths can be influenced via symlinks,
  mount points, or malicious repository names

**Fix:**
Use `json.dumps()` to properly escape all paths before embedding in JavaScript:

1. Line 516: `json.dumps(d)` instead of `f"'{d}'"`
2. Line 524: `json.dumps(monorepo_node_modules)` instead of f-string
3. Line 565: `json.dumps(str(project_root))` instead of f-string

`json.dumps()` wraps strings in double quotes and properly escapes special
characters, preventing injection.

**After fix:**
```javascript
roots: ["/project", "/tmp/test']; console.log('INJECTED'); roots=['"],
                   ^-- double quoted, single quotes are string content (safe)
```

**Files Changed:**
- codeflash/languages/javascript/test_runner.py (3 injection points fixed)
- tests/test_languages/test_javascript_injection_bug.py (new test file, 2 tests)

**Testing:**
- 2 new tests specifically for injection vulnerability (both pass)
- 2 existing test_runner tests pass
- All tests verify paths are JSON-escaped (double-quoted)

**Security Note:**
This vulnerability was found through proactive code review (autoresearch:debug).
No known exploits in the wild. Fixed before public disclosure.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-06 16:32:36 +00:00
..
benchmarks refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
code_utils fix: replace remaining uv tool install refs with uv pip install 2026-03-30 15:26:07 -07:00
languages Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/false-positive-test-discovery 2026-04-03 13:32:34 +02:00
scripts fix(test): move --no-pr before optimize subcommand in e2e tracer test 2026-04-01 06:56:27 +00:00
test_languages Fix CRITICAL JavaScript code injection vulnerability in Jest config generation 2026-04-06 16:32:36 +00:00
test_setup refactor: remove zero-config logic from java-config-redesign branch 2026-03-26 07:39:29 +00:00
__init__.py
conftest.py format and lint all 2026-01-29 01:39:48 -08:00
mymodule.py format and lint all 2026-01-29 01:39:48 -08:00
test_add_language_metadata.py Remove python_version from API payloads 2026-03-27 02:53:22 -05:00
test_add_needed_imports_from_module.py fix: normalize module fallback formatting for import merge 2026-03-17 18:16:54 -06:00
test_add_runtime_comments.py merge to main 2026-02-21 01:49:31 +02:00
test_async_concurrency_decorator.py format and lint all 2026-01-29 01:39:48 -08:00
test_async_function_discovery.py format and lint all 2026-01-29 01:39:48 -08:00
test_async_run_and_parse_tests.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_async_wrapper_sqlite_validation.py format and lint all 2026-01-29 01:39:48 -08:00
test_call_graph.py fix ranking boost ordering and statement helper extraction 2026-03-15 23:29:35 -06:00
test_cleanup_instrumented_files.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_cmd_auth.py fix: remove invalid get_shell_rc_path patches from auth tests 2026-03-25 00:55:39 +00:00
test_cmd_init.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_code_context_extractor.py fix: skip attrs classes in __init__ instrumentation; add attrs support to code_context_extractor 2026-03-18 01:33:40 -06:00
test_code_deduplication.py [Fix] Normalizer and expand its scope 2026-03-06 21:31:24 +05:30
test_code_extractor_none_aliases_exact.py merge to main 2026-02-21 01:49:31 +02:00
test_code_replacement.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_code_replacer_matching.py Merge branch 'main' into omni-java 2026-03-13 00:15:19 +00:00
test_code_utils.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_codeflash_capture.py Merge branch 'main' into omni-java 2026-03-13 00:15:19 +00:00
test_codeflash_checkpoint.py format and lint all 2026-01-29 01:39:48 -08:00
test_codeflash_trace_decorator.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_comparator.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_compare.py feat: add --script mode to codeflash compare 2026-04-02 11:36:54 -05:00
test_critic.py format and lint all 2026-01-29 01:39:48 -08:00
test_early_dedup.py feat: early dedup of optimization candidates before benchmark loop 2026-03-22 05:19:15 -05:00
test_existing_tests_source_for.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_file_to_no_of_tests.py format and lint all 2026-01-29 01:39:48 -08:00
test_formatter.py fix: pass language to format_generated_code for correct temp file extension 2026-03-25 18:05:28 +02:00
test_function_dependencies.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_function_discovery.py merge: incorporate omni-main-java sync work 2026-03-03 23:42:39 -05:00
test_function_ranker.py format and lint all 2026-01-29 01:39:48 -08:00
test_get_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_helper_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_read_only_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_read_writable_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_testgen_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_git_utils.py test: update git_diff tests to match new all-extensions behavior 2026-03-19 18:07:19 +00:00
test_humanize_time.py format and lint all 2026-01-29 01:39:48 -08:00
test_init_javascript.py test: add unit tests for detect_project_language 2026-03-12 04:01:49 +00:00
test_inject_profiling_used_frameworks.py fix: resolve remaining test failures from main sync 2026-03-04 00:13:27 -05:00
test_instrument_all_and_run.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_instrument_async_tests.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrument_codeflash_capture.py fix: address review feedback for attrs init instrumentation 2026-03-18 03:34:44 -06:00
test_instrument_codeflash_trace.py format and lint all 2026-01-29 01:39:48 -08:00
test_instrument_line_profiler.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrument_tests.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrumentation_run_results_aiservice.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_is_numerical_code.py merge to main 2026-02-21 01:49:31 +02:00
test_java_assertion_removal.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_java_multimodule_deps_install.py refactor: replace find_gradle_executable and find_maven_executable with find_executable method in Maven and Gradle strategies 2026-03-10 08:27:10 +02:00
test_java_test_discovery.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_java_test_filter_validation.py refactor: replace find_gradle_executable and find_maven_executable with find_executable method in Maven and Gradle strategies 2026-03-10 08:27:10 +02:00
test_java_tests_project_rootdir.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_javascript_assertion_removal.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_javascript_function_discovery.py merge to main 2026-02-21 01:49:31 +02:00
test_lru_cache_clear.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_merge_test_results.py organized imports 2025-03-28 15:26:27 -07:00
test_merge_tests.py
test_mock_candidate_replacement.py fix failing unit tests with recent refactoring 2026-03-07 01:48:41 +05:30
test_multi_file_code_replacement.py test: sync dual-changed test files from main with omni-java fixes 2026-03-02 15:30:16 -05:00
test_parse_line_profile_test_output.py refactor: remove redundant try/finally; rely on conftest autouse fixture for language cleanup 2026-03-18 18:23:49 +00:00
test_parse_pytest_test_failures.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_parse_test_output_regex.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_pickle_patcher.py fix: update tests for multi-round benchmark plugin 2026-04-02 07:24:55 -05:00
test_property_getter_exclusion.py Fix: Exclude property getters/setters from function discovery 2026-04-01 01:58:33 +00:00
test_pytest_plugin_deterministic_patches.py update tests 2025-10-28 23:05:26 -07:00
test_ranking_boost.py fix: deduplicate test count calls, guard None, and log effort escalation 2026-03-16 14:41:55 -06:00
test_reference_graph.py merge to main 2026-02-21 01:49:31 +02:00
test_remove_functions_from_generated_tests.py merge to main 2026-02-21 01:49:31 +02:00
test_remove_test_functions.py fix: address PR review — recurse _expr_name for call-style decorators, guard empty-set superset 2026-03-07 03:03:23 -05:00
test_remove_unused_definitions.py perf: eliminate redundant CST parsing in get_code_optimization_context 2026-03-16 10:11:58 -06:00
test_shell_utils.py format and lint all 2026-01-29 01:39:48 -08:00
test_static_analysis.py merge to main 2026-02-21 01:49:31 +02:00
test_test_runner.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00
test_trace_benchmarks.py fix: update test expectations for multi-round benchmark plugin 2026-04-02 11:18:33 -05:00
test_tracer.py format and lint all 2026-01-29 01:39:48 -08:00
test_unit_test_discovery.py format and lint all 2026-01-29 01:39:48 -08:00
test_unused_helper_revert.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00
test_validate_javascript_code.py merge to main 2026-02-21 01:49:31 +02:00
test_validate_python_code.py format and lint all 2026-01-29 01:39:48 -08:00
test_version_check.py format and lint all 2026-01-29 01:39:48 -08:00
test_worktree.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00