<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection">
<div dir="auto">If it matters, I’m using Nginx 1.23.3 and NJS 0.7.10</div>
</div>
<div name="messageSignatureSection"><br />
<div class="matchFont"><br />
--<br />
Lance Dockins<br />
<br /></div>
</div>
<div name="messageReplySection">On Feb 10, 2023 at 8:24 PM -0600, Lance Dockins <lance@wordkeeper.com>, wrote:<br />
<blockquote type="cite" style="border-left-color: grey; border-left-width: thin; border-left-style: solid; margin: 5px 5px;padding-left: 10px;">
<div dir="ltr">NJS seems to be throwing an error when trying to access the temp body file created for POST bodies that are larger than the client body buffer size.
<div><br /></div>
<div>I've verified that Nginx and NJS have access to this folder location. As a test, I placed a permanent file in the body temp folder location and can read from that file without an issue in NJS code. But if I grab the full path to the temp body file from Nginx and then try to read that file's contents, NJS throws an error indicating that the file doesn't exist.</div>
<div><br /></div>
<div>Here's a code sample:</div>
<div>let body = '';<br />
if(!r.variables.request_body_file){<br /></div>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>body = r.requestText;</div>
</blockquote>
<div>}<br />
else{<br /></div>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>log.write(logpath, r.variables.request_body_file, r);</div>
<div>let fs = require('fs');</div>
<div>body = fs.readFileSync(r.variables.request_body_file);</div>
</blockquote>
<div>}<br /></div>
<div><br /></div>
<div>The log.write call is a utility function that just allows me to log the contents of the variable in the sample. In this code, r.variables.request_body_file logs a full file path to the temp file for the POST body. Something like this:</div>
<div>/var/lib/nginx/body/0000000128<br /></div>
<div><br /></div>
<div>But trying to access and read that file immediately following that yields an error:</div>
<div>2023/02/10 20:14:05 [error] 609307#609307: *1555 js exception: Error: No such file or directory<br />
at fs.readFileSync (native)<br /></div>
<div><br /></div>
<div>Again, it's not a permissions issue as a permanent test file exists in the same folder and is readable in the same code block without an issue.</div>
<div><br /></div>
<div>This code is currently inside of a js_content filter. Since js_content runs very late in the process, the temp file seems like it "should" still be in place. After all, reading r.requestText works just fine. The only time the problem surfaces is when the client body exceeds the buffer size (even if the buffer size is high) as that's where file access has to occur. Since the POST bodies can sometimes be very large in this particular route, it's not really a viable option to try to match the client body buffer size to the client body max size. So being able to access and read the POST body temp file is important since there are bound to be situations where a request crosses beyond the buffer size regardless of how high it goes (not that higher is necessarily always a good idea).</div>
<div><br /></div>
<div>This sort of NJS behavior "seems" like some sort of race condition where NJS is trying to access the file after Nginx has already disposed of it. Since this is a js_content directive, it should be blocking and it seems to be one of the few stages where access to the POST body is even possible. So it's unclear why the client body buffer file wouldn't exist at the time that this code runs.</div>
<div><br /></div>
<div>Is this a bug in NJS? Or is there some sort of alternative solution (e.g. settings adjustment in Nginx itself) that might resolve the problem?</div>
<div><br /></div>
<div>Thanks in advance for any insights.</div>
<div><br clear="all" />
<div>
<div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr"><br />
<div>
<div style="color:rgb(0,0,0);font-size:14px;font-family:helvetica,arial">-- <br />
Lance Dockins</div>
<div style="color:rgb(0,0,0);font-size:14px;font-family:helvetica,arial"><br /></div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</body>
</html>