# SSI include infinite loop

**URL:** <https://www.ruby-forum.com/t/ssi-include-infinite-loop/207329>\
**Category:** NGINX\
**Created:** [May 24, 2011, 4:50am UTC](https://www.ruby-forum.com/t/ssi-include-infinite-loop/207329 "2011-05-24T04:50:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 24, 2011, 4:50am UTC](https://www.ruby-forum.com/t/ssi-include-infinite-loop/207329/1 "2011-05-24T04:50:27Z")

</div>

Hello,

I’ve encountered a problem about “SSI include infinite loop”.

We have a system that our editors can compose a html page from some  
templates using SSI. Sometimes unwarily combination will cause infinite  
loop, such as ‘A include A’, ‘A include B, B include A’, or even ‘A  
include  
B, B include C, C include A’. And this page will cause nginx eating up  
all  
memories, and certainly won’t serve any more unless we restart nginx.

I’m wondering is there any solution for this problem? Or other ways to  
avoid  
this?

I’ve googled apache and lighttpd, and there also no official solution  
for  
this, unless lightptd has a patch which will limit the whole ‘include’  
count, and I think it’s just a tricky way to fix the infinite loop.

Thanks a lot for any help!

---

<div class="post-metadata">

**Author:** ![eagle\_sbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/919ad9/32.png) [@eagle\_sbc](https://www.ruby-forum.com/u/eagle_sbc)\
**Post date:** [May 24, 2011, 2:59pm UTC](https://www.ruby-forum.com/t/ssi-include-infinite-loop/207329/2 "2011-05-24T14:59:34Z")

</div>

Hello!

On Tue, May 24, 2011 at 10:49:45AM +0800, eagle sbc wrote:

> I’m wondering is there any solution for this problem? Or other ways to avoid  
> this?
> 
> I’ve googled apache and lighttpd, and there also no official solution for  
> this, unless lightptd has a patch which will limit the whole ‘include’  
> count, and I think it’s just a tricky way to fix the infinite loop.
> 
> Thanks a lot for any help!

Number of in-flight ssi includes used to be limited in nginx,  
though this limit was broken somewhere near 0.7.25.

This patch brings it back:

> **[\[PATCH 27 of 31\] Core: protect from subrequest loops](http://mailman.nginx.org/pipermail/nginx-devel/2011-February/000746.html)**

Maxim D.

---

<div class="post-metadata">

**Author:** ![eagle\_sbc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/919ad9/32.png) [@eagle\_sbc](https://www.ruby-forum.com/u/eagle_sbc)\
**Post date:** [May 25, 2011, 5:39am UTC](https://www.ruby-forum.com/t/ssi-include-infinite-loop/207329/3 "2011-05-25T05:39:05Z")

</div>

Hello,

Thanks for your reply! And I think the limit on the number is proper for  
most of the conditions.

However in our case, the index page may have hundreds of SSI ‘include’  
(combined from lots of our sub sites), and the number limitation may not  
be  
so proper.

So here I made a patch: For every ‘include’, a ‘parents uri check’ is  
made.  
The patch is like this:

diff -crB  
[nginx-0.7.65.org/src/http/modules/ngx\_http\_ssi\_filter\_module.cnginx-0.7.65.patch/src/http/modules/ngx\_http\_ssi\_filter\_module.c](http://nginx-0.7.65.org/src/http/modules/ngx_http_ssi_filter_module.cnginx-0.7.65.patch/src/http/modules/ngx_http_ssi_filter_module.c)  
\*\*\* [nginx-0.7.65.org/src/http/modules/ngx\_http\_ssi\_filter\_module.c](http://nginx-0.7.65.org/src/http/modules/ngx_http_ssi_filter_module.c)  
2011-05-24 18:46:53.676370002 +0800  
— nginx-0.7.65.patch/src/http/modules/ngx\_http\_ssi\_filter\_module.c  
2011-05-25 11:19:20.372370002 +0800

* * *

\*\*\* 2024,2029 \*\*\*\*  
— 2024,2044 ----  
flags |=  
NGX\_HTTP\_SUBREQUEST\_IN\_MEMORY|NGX\_HTTP\_SUBREQUEST\_WAITED;  
}

- 

```
ngx_http_request_t *ir = r;

```

- 

```
for(;ir != NULL;ir = ir->parent) {

```

- 

```
    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,

```

- 

```
                   " include path: %V, args: %V; parent path: %V,

```

args: %V",

- 

```
                   uri, &args, &ir->uri, &ir->args);

```

- 

```
    if (uri->len == ir->uri.len

```

- 

```
        && args.len == ir->args.len

```

- 

```
        && ngx_strncmp(uri->data, ir->uri.data, uri->len) == 0

```

- 

```
        && ngx_strncmp(args.data, ir->args.data, args.len) == 0) {

```

- 

```
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,

```

- 

```
                      "A loop include \"%V\" is found", uri);

```

- 

```
        return NGX_HTTP_SSI_ERROR;

```

- 

```
    }

```

- 

```
}

```

- 

```
if (ngx_http_subrequest(r, uri, &args, &sr, psr, flags) != NGX_OK) 

```

{  
return NGX\_HTTP\_SSI\_ERROR;  
}

Hope for any reply of my patch !

And thanks to my fellow Ҧΰ for reminding me on the ‘args’ check!
