Finally some much needed input validation :o
i started this task by documenting and restructuring the pile of sphagetti which was the user authentication and hackvertisements code. this is my first ever ruby on rails project, so the user authentication is quite literally the first controller i ever wrote for this language.
i added some checks for the authentication to among other things allow the server to end an authentication process early if required information is missing.
the hardest part was the hackvertisement upload authentication. first of all, i had to find a library that could parse many different file formats and get metadata like width and height. it was kinda hard to find though since all image libraries were quite large and featured image manipulation, while i only needed to read the metadata. i ended up going with “fastimage” though which works lovely!
i also had to detect animated PNGs. most people expect PNGs to be still, but they can be animated, and many programs and websites dont detect this. i would like all hackvertisements to be non-animated to not be too distracting, so i had to figure out how to detect this. i ended up finding a JS function for this, and translated it myself to Ruby, and it looks like this:
def isPngAnimated(data)
idat_pos = data.index('IDAT')
idat_pos != nil and data[0..idat_pos].index('acTL') != nil
end