I'm testing CodeRunner on my server.
I created a Java Program question as follows:
Write a method named factorial that takes a positive integer n and returns n! where n! = n * (n-1) * (n-2) * ... * 2 * 1
Example: factorial(3) will return 6
A possible solution to the problem is:
public class Test
public static void main(String  args)
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
public static int factorial(int n)
int f = 1;
while(n > 1)
f = f * n;
Moodle saves the program and Coderunner can execute the program with no problems.
However when a recursive solution is given for the factorial method like below:
public static int factorial(int n)
if(n > 1)
return n * factorial(n-1);
moodle returns a 403 error and the program fails to save.
I've pasted the above code into some of the sample questions and coderunner is able to compile and run the code.
I'm using moodle 3.5 and coderunner 3.5.1
Any thoughts of what might be causing the problem?
Did you find a solution?
An error with the recursive solution but not with the iterative one suggests you have a stack overflow occurring with the recursion. Did you check with small values of n?
Why you get a 403 is a mystery. Wrong file permissions, perhaps only on file(s) needed during error reporting? Have you looked in the Apache error log to see what's being reported there? You might want to run a command like
sudo chmod -R a+rX <moodlehome>
to ensure the web server can access everything. However, see https://docs.moodle.org/35/en/Security_recommendations before making any such changes permanent.
I missed the fact that it wasn't even saving it. This sounds like an issue with validation. Is Validate On Save checked? If so what happens when you uncheck it?
I tried checking it and it doesn't make a difference whether it's checked or not.
Another strange thing I found is when I am editing the question in the quiz module and put the iterative version of the program in the answer box, the question saves with no problems. However, when I put the recursive version into the answer box, I end up with the 403 error again and the settings are not saved to the question type in the quiz module.
The question type I'm using is Java_Program question type with the default testing script. The same error occurs with the Java_Method question type as well.
OK, so not validation. And the code behaves fine when I set up and run your question on my server. It's looking like this is nothing to do with CodeRunner but is a Moodle or Apache issue.
Have you looked in the Apache error log?
I've found several sites reporting 406 errors from the Apache mod_security module rejecting content in POSTs because the content is identified as being potentially malicious. To quote from https://webhostingmedia.net/http-error-406-not-acceptable/
The other scenario in which you may be met with a 406 status code is when a mod_security rule is enabled on an Apache server. It’s a type of web application firewall (WAF) program enabled by default on the hosting accounts of some web hosting providers. For example, InMotion Hosting has mod_security enabled on all their Apache based hosting accounts to protect web applications from common attacks like XSS or SQL Injections, as these types of security holes can easily creep into web applications. The mod_security continuously scans the server and the incoming request and the outgoing response for violations of rules it has set. If an action that violates the rules set in the mod_security occurs (a violation caused by a site, page or function), the server will send a 406 Not Acceptable error.
That would seem to fit with your symptoms. Are you using a web hosting provider for your Moodle? If so you might be able to turn mod_security off via cpanel (if your provider permits) as explained in the above website. Or you might be able to disable it with .htaccess as explained here.