varnish-cache icon indicating copy to clipboard operation
varnish-cache copied to clipboard

varnishtest fails with "Condition(*p == '}') not true." when parsing hdr.

Open simonvik opened this issue 10 years ago • 17 comments

Varnishtest 4.1.3 fails with:

Assert error in parse_string(), vtc.c line 358:  Condition(*p == '}') not true.

When the following test runs:

client c1 {
    txreq -hdr "Cookie:() { (sdf)=> "
    rxresp
    expect resp.status == 403
} -run

Expected Behavior

Expect that it ignores validation of matching brackets in hdr-strings.

Current Behavior

Fails with:

Assert error in parse_string(), vtc.c line 358:  Condition(*p == '}') not true.

Steps to Reproduce (for bugs)

Create a varnishtest with

client c1 {
    txreq -hdr "Cookie:() { (sdf)=> "
    rxresp
    expect resp.status == 403
} -run

Your Environment

  • Version used: varnish 4.1.3
  • Operating System and version: Debian wheezy.

simonvik avatar Aug 11 '16 08:08 simonvik

Thanks for reporting! I'll look at it.

gquintard avatar Aug 11 '16 09:08 gquintard

Not a bug, in multiline strings curly braces must be balanced. Not sure whether they can be escaped though.

On Aug 11, 2016 11:26, "gquintard" [email protected] wrote:

Thanks for reporting! I'll look at it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/varnishcache/varnish-cache/issues/2039#issuecomment-239112553, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2bYMKRAAILXUHruOk_vHP3DNc21mXVks5qeuqngaJpZM4Jh5yM .

dridi avatar Aug 15 '16 13:08 dridi

To me it's a bug, quoted strings can be anything. Balancing inner backers doesn't make sense.

gquintard avatar Aug 15 '16 15:08 gquintard

gquintard, +1. It's a test tool, and quoted strings are test data, which should be free to contain whatever needs to be tested. Even if it's nonsense, testing how varnish reacts to nonsense is exactly the point of some test cases.

Even in the case of imbalanced brackets in the non-quoted part of a test case, I'd say that there are more graceful ways to fail than crashing and dumping core with an assertion failure. Just an error message and exit from varnishtest would be enough, since no one needs to run a debugger to figure out what the problem was. (But this is a more of a quibble.)

slimhazard avatar Aug 15 '16 15:08 slimhazard

varnishtest uses assertions in many places where error handling should be used instead. The rationale was probably that it's only for testing purposes, we knows we don't write them bogus tests.

However a multiline string is just a string, regardless of its usage. It's often a string (vcl) or a spec (like here). The contents have no meaning at parsing time.

Did anyone check that escaping works? I don't have a proper computer around.

On Aug 15, 2016 17:35, "slimhazard" [email protected] wrote:

gquintard, +1. It's a test tool, and quoted strings are test data, which should be free to contain whatever needs to be tested. Even if it's nonsense, testing how varnish reacts to nonsense is exactly the point of some test cases.

Even in the case of imbalanced brackets in the non-quoted part of a test case, I'd say that there are more graceful ways to fail than crashing and dumping core with an assertion failure. Just an error message and exit from varnishtest would be enough, since no one needs to run a debugger to figure out what the problem was. (But this is a more of a quibble.)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/varnishcache/varnish-cache/issues/2039#issuecomment-239834902, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2bYPjgJ-pE4HesbC0IToG1MzFDD37vks5qgIc7gaJpZM4Jh5yM .

dridi avatar Aug 15 '16 15:08 dridi

same result when trying ti escape it with \

client c1 {
    txreq -hdr "Host:() \{ (sdf)=> "
    rxresp
    expect resp.status == 403
} -run

result:

---- c1    0.9 Assert error in parse_string(), vtc.c line 358:  Condition(*p == '}') not true.

However it does work just fine if i close the braces in an other string/header:

client c1 {
        txreq -hdr "Host:() { (sdf)=> " -hdr "Foo: }"
        rxresp
        expect resp.status == 403
} -run

result:

#     top  TEST foo.vtc passed (2.008)

sa7dse avatar Aug 15 '16 17:08 sa7dse

same result when trying ti escape it with \

I suggest we fix that by allowing escaping of curly braces and backslash in multiline strings.

However it does work just fine if i close the braces in an other string/header:

I had a different workaround in mind but this is simple enough and if it works for you then you should use it.

dridi avatar Aug 15 '16 21:08 dridi

We should not need to escape it. Multiline strings should not be consider when inside a string. Period. It makes no sense to. For me this is clearly a bug when parsing the input.

fgsch avatar Aug 16 '16 10:08 fgsch

Problem is, how do we treat this:

client c1 {
    txreq -hdr "foo: }"
}

parse_string is pretty dumb, and expect balanced string markers. Escaping seems reasonable to me, the other option is to move a proper syntax definition with lexer/parser.

gquintard avatar Aug 17 '16 08:08 gquintard

That's not the real problem, you can ignore braces within double-quotes. The issue is this:

client c1 {
     txreq -hdr {"}
} 

fgsch avatar Aug 17 '16 09:08 fgsch

That's not the real problem, you can ignore braces within double-quotes.

You're assuming that double quotes inside a multi-line string represent a string that will be parsed as such later. That's not always true.

Here is the workaround I had in mind for @simonvik:

varnishtest "Workaround for #2039"

client c1 {
    txreq -hdr "Cookie:() { (sdf)=> "
    # XXX: a closing curly brace to please varnishtest: }
    rxresp
    expect resp.status == 403
}

# I don't -run the client, this is just showing the parsing workaround.
# Remove the XXX comment and it crashes varnishtest.

Now here is a perfectly valid VCL snippet that crashes varnishtest and doesn't contain a single-line string inside a multi-line string:

varnishtest "We need curly brace escaping"

server s1 "" -start

varnish v1 -vcl+backend {
    /* { */
}

There's no point assuming anything about the contents of a string ahead of time, we don't know what it represents and how it'll be used. Escaping is the only reasonable solution I see.

dridi avatar Aug 17 '16 10:08 dridi

I'm not assuming anything and I disagree. What you mentioned is another bug in parse_string(). Double-quotes denote strings. Everything within a string should be literal (except for macros).

The problem is that everything within braces is parsed as a single long string, and there is no context.

This is the cause of this bug and what you just mentioned. i.e. client c1 { .. } and client c1 { txreq -hdr {..} } are treated equally. The second should be literal, the former should not.

fgsch avatar Aug 17 '16 10:08 fgsch

What you mentioned is another bug in parse_string().

Please define "What".

The problem is that everything within braces is parsed as a single long string, and there is no context.

I don't see that as a problem, it's a string. What we do later with it has nothing to do with token extraction.

The second should be literal, the former should not.

Not trying to be dense but I really don't get it. The second should what?

dridi avatar Aug 17 '16 10:08 dridi

Let's take this example:

server s1 {
        rxreq
        txresp -hdr {foo: /* { */}
} -start

varnish v1 -vcl+backend {
        /* { */
} -start

In the first case we want to pass /* { */ literally, in the second we don't.

fgsch avatar Aug 17 '16 11:08 fgsch

In the first case we want to pass /* { */ literally, in the second we don't.

I'm out of this discussion, all I read is "in the first case we make assumptions, in the second we make different assumptions". I can't seem to see the rationale so there's no point arguing further.

dridi avatar Aug 17 '16 12:08 dridi

The only magic in {...} strings is that we allow them to nest by counting and matching '{'...'}' sets

This is a deliberate choice, I wanted a string format that could carry (almost) anything without too much trouble, and it works quite well.

However, you can still do what the OP tries todo using "..." strings, which has the full set of -escapes:

client c1 {
    txreq -hdr "Cookie:() \x7b (sdf)=> "
    rxresp
    expect resp.status == 200
} -run

bsdphk avatar Sep 12 '16 05:09 bsdphk

Backport review: Nothing commited, nothing to backport

hermunn avatar Sep 13 '16 09:09 hermunn