Skip to content

Commit 0160499

Browse files
committed
fix: support symlink attribute checking on RewriteCond
Addresses e404#24
1 parent 27c46c5 commit 0160499

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
FROM fabiocicerchia/nginx-lua:1.23.2-almalinux8.7-20221201
44

5+
# The luajit package expects Lua 5.1, but this container has 5.3 so remove that.
6+
# Then build Lua 5.1 and luarocks from source so you can install the luafilesystem module
7+
RUN dnf remove -y lua \
8+
&& dnf install -y cmake luajit readline-devel \
9+
&& cd /tmp \
10+
&& curl https://www.lua.org/ftp/lua-5.1.5.tar.gz -L -o lua-5.1.5.tar.gz \
11+
&& tar -zxvf lua-5.1.5.tar.gz \
12+
&& cd lua-5.1.5 \
13+
&& CFLAGS=-DLUA_USE_LINUX make linux \
14+
&& make install \
15+
&& cd .. \
16+
&& curl https://luarocks.org/releases/luarocks-3.8.0.tar.gz -L -o luarocks-3.8.0.tar.gz \
17+
&& tar -zxvf luarocks-3.8.0.tar.gz \
18+
&& cd luarocks-3.8.0 \
19+
&& ./configure --with-lua-include=/usr/local/include \
20+
&& make install \
21+
&& cd .. \
22+
&& luarocks install luafilesystem
23+
524
# Build the EmmyLuaDebugger from source for debugging Lua via IntelliJ IDEA
625
RUN curl https://github.com/EmmyLua/EmmyLuaDebugger/archive/refs/tags/1.0.16.tar.gz \
726
-L -o EmmyLuaDebugger-1.0.16.tar.gz && \

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ When you request a URL that triggers the Lua script, it will pause on the `dbg.b
6565

6666
### Manual installation
6767

68-
1. Install nginx with the [Lua module](https://github.com/openresty/lua-nginx-module) `libnginx-mod-http-lua` and the `luajit` package.
69-
1. Debian: `apt-get install nginx libnginx-mod-http-lua luajit`
70-
2. Fedora: `yum install nginx libnginx-mod-http-lua luajit`
71-
68+
1. Install nginx with the [Lua module](https://github.com/openresty/lua-nginx-module) `libnginx-mod-http-lua` and the `luajit` and `luarocks` packages.
69+
1. Debian: `apt-get install nginx libnginx-mod-http-lua luajit luarocks`
70+
2. Fedora: `dnf install nginx libnginx-mod-http-lua luajit luarocks`
71+
1. Install the [LuaFileSystem](https://lunarmodules.github.io/luafilesystem/) module via `luarocks install luafilesystem`
7272
1. Verify that the Lua module is properly installed by running:
7373
```bash
7474
nginx -V 2>&1 | tr ' ' '\n' | grep lua
@@ -266,7 +266,7 @@ mod_negotiation | `ForceLanguagePriority` | No |
266266
mod_negotiation | `LanguagePriority` | No |
267267
mod_reflector | `*` | Never | Security reasons
268268
mod_rewrite | `RewriteBase` | Yes |
269-
mod_rewrite | `RewriteCond` | Partial | Environment (E=) flag is unsupported, as are *CondPattern* integer comparisons and some file attribute tests listed in the [documentation](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html)
269+
mod_rewrite | `RewriteCond` | Partial | Environment (E=) flag is unsupported, as are *CondPattern* integer comparisons and some file attribute tests listed in the [documentation](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html).
270270
mod_rewrite | `RewriteEngine` | Yes |
271271
mod_rewrite | `RewriteOptions` | No |
272272
mod_rewrite | `RewriteRule` | Yes |

htaccess.lua

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ local path_exists = function(filepath, soft_fail)
150150
return ok
151151
end
152152

153+
-- Get the type of a file system object
154+
-- @param filepath .... the filename
155+
-- @return file_type .. One of (directory|link|file), or nil if the path is invalid
156+
local get_file_type = function(filepath)
157+
local lfs = require "lfs"
158+
local file_type = nil
159+
if (lfs.symlinkattributes (filepath) ~= nil) then
160+
local attr = lfs.symlinkattributes (filepath);
161+
assert (type(attr) == "table")
162+
if attr.mode == "directory" then
163+
file_type = 'directory'
164+
elseif attr['target'] ~= nil then
165+
-- print ("*** symlink found "..attr['target'])
166+
file_type = 'link'
167+
else
168+
file_type = 'file'
169+
end
170+
end
171+
return file_type
172+
end
173+
153174
-- Read contents of any file
154175
local get_file_contents = function(name)
155176
ensure_doc_root(name) -- Security: enforce document root
@@ -1101,10 +1122,16 @@ if get_cdir('rewrite') and #parsed_rewriterules > 0 then
11011122
fail('RewriteCond expressions ("expr ...") are unsupported') -- We don't support expr style conditions due to their weird complexity and redundancy
11021123
elseif cond_pattern:sub(1,1) == '-' then -- File attribute tests or integer comparisons (case sensitive)
11031124
local filepath = cond_test:gsub('/$','',1)
1125+
local file_type = get_file_type(filepath)
1126+
1127+
cond_matches = false
1128+
11041129
if cond_pattern == '-d' then -- is directory
1105-
cond_matches = path_exists(filepath..'/')
1130+
cond_matches = file_type == 'directory'
11061131
elseif cond_pattern == '-f' or cond_pattern == '-F' then -- is file
1107-
cond_matches = path_exists(filepath) and not path_exists(filepath..'/')
1132+
cond_matches = file_type == 'file'
1133+
elseif cond_pattern == '-l' or cond_pattern == '-L' then -- is symlink
1134+
cond_matches = file_type == 'link'
11081135
else
11091136
fail('RewriteCond pattern unsupported: '..cond_pattern)
11101137
end

0 commit comments

Comments
 (0)