If you've ever rubbed your eyes and seen faint sparkles of light, you have demonstrated a surprising fact of sensory physiology. Depending on how hard you rub–and you should always be gentle–you may see glowing patches or even faint colors. Since your eyes are closed and your fingers are in them, it's clear there isn't really any light getting in. So where do the flashes come from?
You can understand this situation if you stop to think that technically we never see light itself; what we "see" is the firing of the visual cells in our retina. These cells are extremely sensitive and are normally triggered by light rays focused on them by the eye. But they can be temporarily fooled into firing just by pushing on them–say with your knuckles when you are sleepy–although the firing this causes will be more or less random, resulting in a sensation of vague glowing or flashing lights.
This may seem like evidence that the eyeball is a rather imprecise mechanism, but quite to the contrary: its just because the retina in the eye is able to respond to such tiny inputs that it's possible to make them misfire a little by applying pressure. An eye that couldn't be fooled this way probably would be of little use in registering light.
So if you wonder where those tiny sparkles are coming from the next time you're up late and rub your eyes, just remember: it's all inside your head.