Developers' Forum

Java Recursion solution returns 403 error

 
Picture of Jeffrey Wong
Java Recursion solution returns 403 error
by Jeffrey Wong - Tuesday, 9 October 2018, 2:17 PM
 

Hello,

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:

import java.util.Scanner;
public class Test
{
    public static void main(String [] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(factorial(n));
    }
   
    public static int factorial(int n)
    {
        int f = 1;
        while(n > 1)
        {
            f = f * n;
            n--;
        }
        return f;
    }
}


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);
        }
        else
        {
            return 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?


Thanks

Jeffrey

Picture of Richard Lobb
Re: Java Recursion solution returns 403 error
by Richard Lobb - Friday, 12 October 2018, 9:54 AM
 

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.

Richard

Picture of Jeffrey Wong
Re: Java Recursion solution returns 403 error
by Jeffrey Wong - Friday, 12 October 2018, 1:56 PM
 
Hi Richard,

I have not found a solution yet.
The recursive method for factorial is correct.

I tried writing an infinite recursion loop, saved it, and ran it on coderunner and it returned a stack overflow error as expected.
I've attached a picture below.

However, when I use the recursive method for factorial, I can not even save the program to run it.
I just get a 403 error from moodle which is really strange.

I believe it has something to do with Moodle and the way it saves the program.
I'll take a look at my Moodle settings again.

Thanks for your help and suggestions.
Jeffrey

Picture of Richard Lobb
Re: Java Recursion solution returns 403 error
by Richard Lobb - Friday, 12 October 2018, 3:09 PM
 

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?

Picture of Jeffrey Wong
Re: Java Recursion solution returns 403 error
by Jeffrey Wong - Friday, 12 October 2018, 6:43 PM
 
The answer box in my settings is left empty and Validate on Save is also unchecked.


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.

Picture of Richard Lobb
Re: Java Recursion solution returns 403 error
by Richard Lobb - Friday, 12 October 2018, 9:18 PM
 

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

Richard


Picture of Jeffrey Wong
Re: Java Recursion solution returns 403 error
by Jeffrey Wong - Wednesday, 17 October 2018, 3:29 AM
 
Hi Richard,

Disabling the mod_security module did the trick.
Everything is working perfectly now.

Thank you for all the help.

Cheers
Jeffrey