26 Commits

Author SHA1 Message Date
Irtaza Akram
d29b0473f4 fix: align pylint fixes in edx-platform Problem XBlock with extracted xblocks-contrib/problem (#37758)
* fix: pylint issues for problem xblock
2026-01-07 16:39:11 +05:00
Irtaza Akram
1945b12769 Autoformat Problem XBlock Source Files for Consistency (1/2) (#37476)
* fix: run formatter black and isort

* fix: linting, pydocstyle, xsslint and security issues
2025-12-05 18:31:45 +05:00
Tim McCormack
447fd0b6cb feat: Upgrade to codejail 4.0.0 (#36916)
This brings an important security improvement -- codejail won't default to
running in unsafe mode, which can happen if certain configuration errors
are present.

Properly configured installations shouldn't be affected. We just need to
adjust some unit tests to opt into unsafe mode.

Changes:

- Update `edx-codejail` dependency to [version 4.0.0](https://github.com/openedx/codejail/blob/master/CHANGELOG.rst#400---2025-06-13)
- Define a `use_unsafe_codejail` decorator that allows running a unit test (or entire TestCase class) in unsafe mode
- Use that decorator as needed, based on which tests started failing
2025-06-17 09:33:52 -04:00
Tim McCormack
d5a273ce2f feat!: Expand codejail darklaunch normalizers; append by default (#36682)
For darklaunch comparisons where the two sides have different Python
versions, we'll want a more comprehensive list of normalizers.

- Expand the default list to include patterns discovered during a Python
  3.8 vs. 3.12 comparison.
- Append the setting value by default, rather than replacing (but still
  allow replacing).
- Use default normalizers if custom ones can't be loaded.
- Add log message when loading normalizers fails.
- Validate the replacement pattern, not just the search pattern.
2025-05-08 13:43:53 -04:00
Tim McCormack
5bc05810dc feat: Improve codejail darklaunch logging (#36671)
This is intended to make logs more or less a standalone source for
analyzing mismatches.

- Only log mismatches or exceptions
- Merge local and remote log messages into one so they can be correlated
  more easily
- Different log messages for mismatch vs. unexpected exceptions
- Include course ID (as limit overrides context) in log message
2025-05-06 16:42:55 -04:00
Tim McCormack
ca335b3c3a feat: Fix character range for codejail darklaunch tmpdir normalization (#36615)
Python's tempfile naming also includes underscore:
https://github.com/python/cpython/blob/3.12/Lib/tempfile.py#L140

This omission would cause normalization to fail about 1/6 of the time.

Also expand test to exercise more of character range.
2025-04-28 12:35:41 -04:00
Tim McCormack
3a9b4367e6 fix: Call json_safe on globals in codejail remote_exec (#36542)
We need to make globals JSON-friendly before sending them across the
network.

Addresses https://github.com/edx/edx-arch-experiments/issues/1016
2025-04-18 11:22:43 -04:00
Tim McCormack
a960cdff8d fix: Add much more codejail darklaunch info; fix remote error bug (#36534)
- Fix bug where we were overwriting `remote_emsg` with None, and add test
  that would have caught it.
- Suppress differences due solely to the codejail sandbox directory name
  differing (in stack traces), and add test for this. Configurable because
  we'll need to add an additional search/replace pair for the sandbox venv
  paths.
- Add a variety of custom attributes, replacing existing ones. The attrs
  now have a prefixed naming scheme to simplify searching.
- Add slug to log output so we can more readily correlate traces and logs,
  as well as logs across services.
- Fix typo in error message.
- Fix existing import sort order lint.
2025-04-17 15:39:47 -04:00
Tim McCormack
bf2f8c3705 fix: Don't let local codejail exec pollute darklaunched remote globals
We were running local exec before making the copy of globals_dict for
remote_exec, so remote exec has been getting a polluted version of the
globals.
2025-04-15 15:50:12 -04:00
Tim McCormack
fd66048a49 test: Improve codejail darklaunch tests
- Separate test for misconfiguration
- Add helper method for generic dark launch testing
- Test two darklaunch scenarios: Globals interference, and error that would
  previously have caused the remote side not to run
- Rename mocks to have our usual `mock_` prefix
2025-04-15 15:50:12 -04:00
Tim McCormack
3f2271ab69 feat: Catch all exceptions from codejail dark launch
- Catch all exceptions, not just Exception, to better prevent errors from
  interfering with mainline responses.
- Introduce a separate try block around the monitoring code so that bugs
  there don't cause issues.
- Print exception information as well for both sides (but only if not a
  SafeExecException, which is redundant with emsg).

Some formatting changes to log messages as well.

Example outputs:

For `1/0`:

```
2025-04-14 17:26:34,239 INFO 10232 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:240 - Remote execution in darklaunch mode produces globals={'expect': None, 'ans': '1/0'}, emsg=None, exception=None
2025-04-14 17:26:34,239 INFO 10232 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:245 - Local execution in darklaunch mode produces globals={'expect': None, 'ans': '1/0'}, emsg='ZeroDivisionError: division by zero', exception=None
```

For `raise BaseException("hi")`:

```
2025-04-14 17:26:13,359 INFO 10232 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:240 - Remote execution in darklaunch mode produces globals={'expect': None, 'ans': 'raise BaseException("hi")'}, emsg=None, exception=None
2025-04-14 17:26:13,359 INFO 10232 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:245 - Local execution in darklaunch mode produces globals={'expect': None, 'ans': 'raise BaseException("hi")'}, emsg='hi', exception=BaseException('hi')
```

With codejail-service down, and `out = 1 + 2`:

```
2025-04-14 17:30:28,597 INFO 12484 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:241 - Remote execution in darklaunch mode produces globals={'expect': None, 'ans': 'out = 1 + 2', 'out': 3, 'cfn_return': {'input_list': [{'ok': True, 'msg': 'Output:\n3', 'grade_decimal': 1}]}}, emsg=None, exception=CodejailServiceUnavailable('Codejail API Service is unavailable. Please try again in a few minutes.')
2025-04-14 17:30:28,597 INFO 12484 [xmodule.capa.safe_exec.safe_exec] [user 3] [ip 172.18.0.1] safe_exec.py:246 - Local execution in darklaunch mode produces globals={'expect': None, 'ans': 'out = 1 + 2', 'out': 3, 'cfn_return': {'input_list': [{'ok': True, 'msg': 'Output:\n3', 'grade_decimal': 1}]}}, emsg=None, exception=None
```
2025-04-15 15:50:12 -04:00
Tim McCormack
45a96e2430 feat: Run remote codejail even if unexpected exception in local safe_exec
During dark launch of remote codejail, we want to ensure we always run both
local and remote execution -- otherwise we're missing data for the remote
side in an important situation.

This will help answer the question of whether the unexpected exception
happens on both sides, even though it may not look exactly the same due to
differences in how unexpected errors are handled.

An example input that provokes this in unsafe execution mode is
`raise BaseException("hi")`; in safe execution mode, printing to
`sys.__stdout__` should also produce an appropriate error.
2025-04-15 15:50:12 -04:00
Tim McCormack
1303965a29 feat: Also set MPLCONFIGDIR to make matplotlib complain less (#36456)
Example output from running `import matplotlib; 1/0`, before and after the change:

```diff
--- tmp/before	2025-03-28 03:34:06.633689552 +0000
+++ tmp/after	2025-03-28 03:34:37.268688891 +0000
@@ -1,6 +1,5 @@
-Matplotlib created a temporary cache directory at /tmp/codejail-hveq16ah/tmp/matplotlib-tv0c_vzt because the default path (/home/sandbox/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
 Traceback (most recent call last):
   File "jailed_code", line 19, in <module>
     exec(code, g_dict)
   File "<string>", line 1, in <module>
 ZeroDivisionError: division by zero
```
2025-03-28 07:31:50 -04:00
Tim McCormack
2009e8972c feat: Set TMPDIR for codejail executions (#36412)
This makes it easier to run matplotlib in codejail, and should prevent a
number of other issues in the future with other packages that need to
create tempfiles.

No change is required for existing codejail installations, but after this
change operators may be able to tighten their apparmor configuration to
prevent write access to global temp or cache dirs.

Manual testing instructions: Create a codejail problem that runs
`import matplotlib` and confirm that it runs without error. (Unit tests
aren't feasible here because this requires a fully configured codejail in
order for the tmp subdirectory to exist.)

Also: Add comment for `OPENBLAS_NUM_THREADS` and numpy support.
2025-03-20 15:41:43 -04:00
Diana Huang
8935af7ab4 feat: Add observability to codejail darklaunch.
Sets up tracing in code jail calls to track remote
and local execution, and adds custom attributes
to track error messages from the calls.

https://github.com/edx/edx-arch-experiments/issues/895
2025-03-07 11:19:40 -05:00
Diana Huang
ef49090c31 feat: Add codejail darklaunch toggle.
This adds a toggle for running codejail in both remote
and local configurations for testing purposes.

https://github.com/edx/edx-arch-experiments/issues/895
2025-03-05 17:56:40 -05:00
Irtaza Akram
ec2a698604 cleanup references of python 2 & <3.11 (#35799)
* chore: cleanup of old python references
2024-11-15 16:58:20 +05:00
Kyle McCormick
9c6e765bf6 test: run ./xmodule/ tests with CMS settings (#33534)
Currently, ./xmodule/ unit tests are only run with LMS settings. However,
./common/ and ./xmodule/ are run twice: once with LMS settings and once with
CMS settings.

Just like ./common/ and ./openedx/, the unit tests in ./xmodule/ validate
behavior in both LMS and CMS. So, order to fully test ./xmodule/, we should to
run its tests with CMS settings too.

This will enable us to better validate certain LibraryContentBlocks behaviors
being touched by https://github.com/openedx/edx-platform/pull/33263 which can't
be expressed under LMS settings.

Also in this commit:

* refactor: rename the shards to be clear whether they're running under LMS or CMS
* docs: correct comments regarding conditions under which codejail's
   test_cant_do_something_forbidden is skipped.
* test: update a unit test which was using the now-deleted library_sourced block to use
   library_content block instead.
2023-10-19 10:19:28 -04:00
Babatunde Olusola
6e2c43e4e0 refactor: update codejail test condition (#32955)
Skip the test for codejail if the codejailservice api is not enabled.
It fixes running this test case in Tutor.
2023-09-07 17:08:30 +02:00
Usama Sadiq
7710e60328 fix: fix middleware get_response parameter deprecation warning (#33067) 2023-08-22 15:52:30 +05:00
Braden MacDonald
9b9b88df52 chore: remove some usages of six (Python2 compat) (#32554)
* get rid of six.text_type(s)
* get rid of six.b()
* get rid of six.string_types
* get rid of six.PY2/six.PY3
* get rid of six.iteritems() and six.viewvalues()
2023-07-17 12:18:43 -07:00
Sarina Canelake
4a2f231302 fix: fix github url strings (org edx -> openedx) 2022-09-15 14:52:28 -04:00
Mohammad Ahtasham ul Hassan
bd90ebde7b fix: unpin numpy (#30761) 2022-07-28 15:56:03 +05:00
Muhammad Soban Javed
d053bba952 Revert "Revert "refactor: move common/lib/capa/capa to xmodule/capa" (#30762)"
This reverts commit 4463ee751d.
2022-07-27 15:36:08 +05:00
Muhammad Soban Javed
4463ee751d Revert "refactor: move common/lib/capa/capa to xmodule/capa" (#30762) 2022-07-21 18:22:15 +05:00
Soban Javed
9eba9f983a refactor!: move common/lib/capa/capa to xmodule/capa
As part of dissolving our sub-projects in edx-platform, we are moving this package under the xmodule directory.
We have fixed all the occurences of import of this package and also fixed all documents related references.
This might break your platform if you have any reference of `import capa` or `from capa import` in your codebase or in any Xblock.

Ref: https://openedx.atlassian.net/browse/BOM-2582
2022-07-19 12:20:04 +05:00