1

Gone around the houses on this one tonight. All I want to do is pull out the csrf-token in the following script however it returns nil

local html = '<meta content="authenticity_token" name="csrf-param" /><meta content="ndcZ+Vp8MuM/hF6LizdrvJqgcRh22zF8w/DnIX0DvR0=" name="csrf-token" />'

local csrf_token = string.match(html, 'content="(.*)" name="csrf-token"')

If I modify the script and take off the "-token" part it matches something, but not the right thing of course.

I know it is the hyphen because if I modify the string to be "csrftoken" and the match it finds works as expected.

I attempted to escape the - like so \- but that threw an error...

elp...

johnwards
  • 1,901
  • 3
  • 18
  • 27

2 Answers2

2

There are two problems:

  1. The - does need to be escaped, but Lua uses % instead of \.

  2. Further, the reason why you get something odd is due to the fact the . can match anything, including across tags (or attributes) and tries to take as much as possible (since the engine will return the left-most possible match, ungreedy quantifiers wouldn't help either). What you should do is restrict the allowed characters, so that the captured thing cannot go outside of the attribute quotes - like [^"] (any character except quotes):

Taking all of that together:

local csrf_token = string.match(html, 'content="([^"]*)" name="csrf%-token"')

In any case, you shouldn't actually be matching HTML with regular expressions.

Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • Ah ha! It was the % escaping I was needing! I now have another problem, which is that I'm using regex for matching HTML but as this is a throw away loadimpact test, I can cope with that. – johnwards Jun 11 '13 at 21:12
  • @johnwards yeah, probably... I also don't know about an established DOM parser in Lua... – Martin Ender Jun 11 '13 at 21:13
  • @m.buettner you probably wanted to put in a different link at the end – dualed Jun 13 '13 at 00:43
0
name="csrf-token'"

You have an extra apostrophe at the end of this line.

I would also escape " = and the hyphen, though this may not be necessary for all these characters.

Andy G
  • 19,232
  • 5
  • 47
  • 69