I am not an astronomer but I would guess they know this the same way they know what anything in distant space (that does not give off light of its own) is made-up of: they observe the light shining through it and see which part of the spectrum is/is not filtered by it.