All posts

Iterate and convert an XSS collection to hash in Ruby

I was working with a collection of elements in an XSS payload. Here’s the payload:


In order to work with this payload I first used Nokogiri to turn the payload into an XML document: document = Nokogiri::XML(response_string)

Then I passed that document to a class method that converted the values to a formatted hash that would be easier to work with. I used the css method to iterate ones over each field and then a second time to the entry field name and values for the hash:

def fields_to_hash(document)
  account_hash = {}

  document.css("fields").map do |additional_params|
    additional_params.css("entry").map do |param|
      account_hash[param.css("key").text] = param.css("value").text


I hope this helped you in solving whichever problem brought you here.


I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).