I remember very clearly when I was little (about 6 years old). I had some Hershey's kisses hidden away in my room, leftover from whatever holiday had most recently passed. My parents didn't know I had them. That is until I bit into one and saw a worm wiggling around (actually, I think it was half a worm
) and screamed. My mother came running, saw the kisses and the worm. She said that sometimes happens with chocolate and threw them out.
Don't know how they get in there, but I suspect it is as you said--eggs or larvae are already present and if the conditions are right, they grow.