You hit on it here. They key is that, in both cases, their alignment is not a function of their actions. Both beings lack agency, and thus their alignment is not a fuction of their actions: in terms of morality, they are essentially objects.
But in D&D, objects can be Evil. Evil in this case becomes a question of history or design, rather than actions: an artifact is designed to be powered by the torment of the innocent, it can be Evil even before it is ever used. If a place has horrors beyond horrors committed at it, it can be Evil even if it never does more than make people in the area uncomfortable.
These patterns reflect, essentially, Contagion Thinking. While this is something we tend to avoid in modern philosophical discussions of morality, is something that is deeply ingrained in our thinking process as humans. It's the same congnative element that makes us uncomfortable with reclaiming wastewater, even when it can be made far cleaner than the water we take from the ground.
It's also heavily shaped our thinking about morality throughout history, even if they ways in which it did so aren't actually valid.