Allow open access to file in secure directory

Discussion in 'Apache' started by Matt Crandall, Nov 4, 2021.

  1. #1
    First, my apologies - I am a total beginner in the realm of Apache server configuration files. That said, I've spent days trying to come up with an answer to this problem on my own and have had no luck. So, I'm looking for places to reach out to those wiser than myself in this matter for help.

    SCENARIO: We have a directory that is secure and requires a username and password to access content inside it. For this example it is called 'secure'. Inside that directory is a file (opensecure.html in this example) that we want users to be able to request from the server without being prompted for login credentials. We have attempted to use the <Files> directive inside the <Directory> directive to override the other security directives in <Directory>.

    QUESTIONS:
    1. Is this scenario even plausible? Can you use directives in <Files> to override the directives of it's parent <Directory>?
    2. Is this scenario wise or is there a better way to do this?
    3. Assuming this is how we accomplish our goal, what are we doing wrong?
    Here is a sample of the (most recent) server configuration (anonymized in some places) currently in place:

    <Directory /testsite/secure>
    AllowOverride AuthConfig
    Options None
    AuthType Basic
    AuthName ServerName
    ProfileToken On
    Order Allow,Deny
    Allow From all
    Require valid-user
    UserID %%CLIENT%%
    PasswdFile %%SYSTEM%%
    <Files opensecure.html>
    Allow From all
    Order Allow,Deny
    Require all granted
    </Files>
    </Directory>

    DESIRED BEHAVIOR: End-user can access opensecure.html without need to enter login credentials.
    ACTUAL OUTCOME: Depending on the attempted configuration, user is either 1) still prompted for login credentials or 2) triggers an internal server error.
     
    Matt Crandall, Nov 4, 2021 IP
  2. pavv

    pavv Well-Known Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    3
    Trophy Points:
    120
    #2
    I've tested it now on a server and it works as you wish. Here is the .htaccess file from the protected directory


    AuthType Basic
    AuthUserFile /home/path/.htpasswd
    AuthName "test"
    require valid-user
    ErrorDocument 401 "Unauthorized Access"
    
    <Files "opensecure.html">
    Require all granted
    </Files>
    Code (ApacheConf):
     
    pavv, Nov 4, 2021 IP
  3. Matt Crandall

    Matt Crandall Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thank you for your prompt response. Unfortunately, that particular configuration does not appear to work in my situation. I suspect it might be because of the way we are authenticating users. Our site is being served from an IBM and we use the IBM i OS user profiles for authentication. So there is no AuthUserFile. (Sample documentation and configuration for this authorization can be found here: https://library.roguewave.com/display/SUPPORT/IBM+i+Apache+HTTP+-+Server+Authentication+using+IBM+i+user+profiles) So, what I did attempt was to simplify my <Directory> directives to match your example as closely as possible (see below), but this produced no noticeable change in behavior. The 'open' page in the secure directory is still asking for login credentials.

    <Directory "/testsite/secure">
       AuthType Basic
       AuthName "ServerName"
       Require valid-user
       UserID %%CLIENT%%
       PasswdFile %%SYSTEM%%
       ErrorDocument 401 "Unauthorized Access"
       <Files "opensecure.html">
       Require all granted
       </Files>
    </Directory>
    Code (ApacheConf):
    Perhaps my issue is the method of authentication for an IBMi OS somehow requires a different approach in the configuration?
     
    Matt Crandall, Nov 5, 2021 IP
  4. pavv

    pavv Well-Known Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    3
    Trophy Points:
    120
    #4
    I'm not a .htaccess expert, but try with the <Files> section outside of the <Directory>

    <Files "opensecure.html">
    Require all granted
    </Files>
    <Directory "/testsite/secure">
    AuthType Basic
    AuthName "ServerName"
    Require valid-user
    UserID %%CLIENT%%
    PasswdFile %%SYSTEM%%
    ErrorDocument 401 "Unauthorized Access"
    </Directory>
     
    pavv, Nov 5, 2021 IP
  5. Matt Crandall

    Matt Crandall Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Unfortunately, no change. I'd actually started with it outside the <Directory> until I read in the documentation "that <Files> can be nested inside <Directory> sections to restrict the portion of the filesystem they apply to." (https://httpd.apache.org/docs/2.4/mod/core.html#files) Also, don't know if this makes a difference, but these directives are all in the server configuration file (/www/testsite/conf/httpd.conf) not a .htaccess file.
     
    Matt Crandall, Nov 5, 2021 IP
  6. pavv

    pavv Well-Known Member

    Messages:
    275
    Likes Received:
    8
    Best Answers:
    3
    Trophy Points:
    120
    #6
    I see.. don't know what to say..
    For testing try with a test account/.htaccess file

    Something basic
    <Files "file.html">
    Require ip YOUR-IP
    </Files>
    only your IP should be able to access the file

    Then,
    <Files "file.html">
    Require not ip YOUR-IP
    </Files>
    only your IP should not be able to access the file
     
    pavv, Nov 5, 2021 IP
  7. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #7
    Seems to me that the easiest solution is to move the file to an insecure directory that ONLY contains the file in question.
     
    mmerlinn, Nov 12, 2021 IP
  8. Matt Crandall

    Matt Crandall Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    @mmerlinn , you are correct. That would be an easier solution. However, we are attempting to shore up some areas of a website that's been in place for over a decade. While most of the files should be secure behind a login, there are a handful (the exceptions we're hoping to make with the <Files> directive) that need to remain openly accessible without a login prompt. The URLs pointing to these files are spread through the site and moving them into another folder would break many links and perhaps even functions as the 404s started rolling in. Hunting down all these possible links and updating them is a gargantuan task that we're trying to avoid by seeing if there's a way to lock down a folder but make exceptions for individual files.

    The longer we work on this though, the more the dread sinks in in that I'm going to have to manually update a whole site's worth of links. We could do redirects, but I'm not sure if that would work for all the files in my scenario. Anyhow, thank you for your suggestion. It may end up ultimately having to be our solution.
     
    Matt Crandall, Nov 13, 2021 IP
  9. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #9
    You have one hell of a mess then. It looks to me that whoever developed the site just slopped it together with no consideration of how it would be maintained.

    What I would do is to populate an insecure directory with the secure files in question. Then as you get complaints, find and fix the links to point to the duplicate files. That way your site is still usable to everyone, just with the annoyance of logging in for those who do not complain. Over time most if not all links will be fixed without stopping everything and fixing them all now.

    I would also find a way to log access to the secure files in question so when someone does need to login you have a record and then can fix that link to point to the insecure version.
     
    Last edited: Nov 13, 2021
    mmerlinn, Nov 13, 2021 IP