# URL encoding and other hackery

**URL:** <https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345>\
**Category:** NGINX\
**Created:** [February 17, 2008, 5:55am UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345 "2008-02-17T05:55:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 5:55am UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/1 "2008-02-17T05:55:03Z")

</div>

Hi. First, let me just say that I love nginx. Thanks for creating and  
maintaining it - we appreciate it.

I am using nginx as the front end to a rails cluster. When rails  
generates a page I write the page to disk, where nginx can look for it  
later. I want to use something like this:

if (-f $document\_root/$uri)

But I anticipate a few problems:

1. the uri might include “…” or similar hackery
2. the uri might include query parameters

That leads to my questions:

1. Does nginx validate incoming uris? Will it strip out “…”?
2. Can I URL encode a variable?

Thanks!

Adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/2 "2008-02-17T16:03:43Z")

</div>

On 2/17/08, Adam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using nginx as the front end to a rails cluster. When rails  
> generates a page I write the page to disk, where nginx can look for it  
> later. I want to use something like this:

We’re doing the same thing.

> if (-f $document\_root/$uri)

This works:

```
if (-f $request_filename) {
  break;
}

```

> But I anticipate a few problems:
> 
> 1. the uri might include “..” or similar hackery

It’s up to you to ensure that the saved file corresponds to the file  
name Nginx generates in $request\_filename. Nginx will URL-decode the  
path, which means that

[http://example.com/buttons/a%2F..%2Fbutton.png](http://example.com/buttons/a%2f..%2fbutton.png)

will resolve to

$document\_root/buttons/a/../button.png

which is expanded to

$document\_root/buttons/button.png

…which is the file name that Rails’ cache\_page method will use.

In other words it’s possible to generate URLs which may end up outside  
your designated document root. The risk is not Nginx that could try to  
serve stuff beyond the document root, but that the Rails app might  
write its cache file in unexpected places.

I don’t know if cache\_page validates the file name to ensure it’s  
within $RAILS\_ENV/public. It certainly ought to. There’s also the risk  
that cache\_page could overwrite _other_ files within the public  
directory, of course, such as stuff in public/images.

> 1. the uri might include query parameters

If so you will need to create an Nginx variable to appends the query  
string to the end of the $request\_filename. But a better option is to  
rely on Rails routes. Here’s a typical route we use for rendering  
buttons:

map.connect ‘cache/button/:id’, :controller =\> “theme”, :action =\>  
‘button’,  
:requirements =\> {:id =\> /.\*/}

This will map a URL such as this:

[http://example.com/cache/button/style=green;text=Click+me.png](http://example.com/cache/button/style=green;text=Click+me.png)

to a controller action as well as a nicely readable file name within  
our cache directory. In the controller we parse the file name and do  
the rendering:

def button  
options = Button.parse\_options(params[:id])  
button = Button.new(options)  
…  
data = button.render.to\_blob  
send\_data(data, :type =\> button.content\_type, :disposition =\>  
“inline”)  
cache\_page(data)  
end

Alexander.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/3 "2008-02-17T18:49:36Z")

</div>

On Sat, Feb 16, 2008 at 08:44:00PM -0800, Adam D. wrote:

> Hi. First, let me just say that I love nginx. Thanks for creating and  
> maintaining it - we appreciate it.
> 
> I am using nginx as the front end to a rails cluster. When rails  
> generates a page I write the page to disk, where nginx can look for it  
> later. I want to use something like this:
> 
> if (-f $document\_root/$uri)

You should use $request\_filename instead: - it’s “$document\_root$uri”.  
However, $request\_filename correctly handle “root” as “alias”.

> But I anticipate a few problems:
> 
> 1. the uri might include “..” or similar hackery
> 2. the uri might include query parameters

$uri and $request\_filename does not contains query parameters.  
The query parameters are available via $args or $query\_string (the later  
is for compatibilty with Apache).

> That leads to my questions:
> 
> 1. Does nginx validate incoming uris? Will it strip out “..”?

Yes, nginx processes various /./, /../ in clear and escaped form,  
and does not allow to to below URI’s root.

> 1. Can I URL encode a variable?

I do not understand the question.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/4 "2008-02-17T18:57:51Z")

</div>

On Sun, Feb 17, 2008 at 06:47:20PM +0100, Manlio P. wrote:

> Igor S. ha scritto:
> 
> > […]  
> > $uri and $request\_filename does not contains query parameters.  
> > The query parameters are available via $args or $query\_string (the later  
> > is for compatibilty with Apache).
> 
> By the way, I have noted that the uri fragment is removed? Why?

Do you mean “/uri#fragment” ? It is removed by browser, but not nginx.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/5 "2008-02-17T18:54:19Z")

</div>

Igor S. ha scritto:

> […]  
> $uri and $request\_filename does not contains query parameters.  
> The query parameters are available via $args or $query\_string (the later  
> is for compatibilty with Apache).

By the way, I have noted that the uri fragment is removed? Why?

> […]

Thanks Manlio P.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 17, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/6 "2008-02-17T19:29:03Z")

</div>

Igor S. wrote:

> > later. I want to use something like this:
> > 
> > if (-f $document\_root/$uri)
> 
> You should use $request\_filename instead: - it’s “$document\_root$uri”.  
> However, $request\_filename correctly handle “root” as “alias”.

I’m looking for a flavor of $request\_filename that includes query  
arguments, so that I can cache a version of the page that includes the  
arguments. At the very least, I need to differentiate between a version  
WITH arguments and a version WITHOUT arguments. I don’t want those two  
requests to serve up the same page. This is very important for SEO  
purposes, to avoid duplicate content being shown at two different urls.

I could use this:

if (-f $document\_root/$request\_uri)

But there are escaping issues. Perhaps nginx needs something like this:

$request\_filename\_with\_args

> > 1. Can I URL encode a variable?
> 
> I do not understand the question.

Another solution to my problem would be something like:

set $path url\_escape($request\_uri)  
if (-f $document\_root/$path) {  
…

Adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 18, 2008, 12:56am UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/7 "2008-02-18T00:56:41Z")

</div>

What escaping issues? $request\_uri will already be uri-encoded, so  
you can use it directly on the file system. You just need to make sure  
whatever’s behind nginx making those files have ordered the parameters  
correctly.

for example, given a request $request\_uri = /path/to/some.xml?a=%22&b=  
%23

$document\_root$request\_uri ==\> /path/to/root/path/to/some.xml?a=%22&b=  
%23

this is a valid path in most file systems.

(note, there’s no slash between document\_root and request\_uri)

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [February 19, 2008, 11:29am UTC](https://www.ruby-forum.com/t/url-encoding-and-other-hackery/129345/8 "2008-02-19T11:29:54Z")

</div>

> Perhaps nginx needs something like this:  
> $request\_filename\_with\_args

Of course, you can just use: $request\_filename$is\_args$args  
without if () {} statement. 🙂
