According to the sages, one is forbidden to eat only those worms which have crawled upon the ground, as is written: “These also [shall be] unclean to you among the creeping things that creep upon the ground” (Leviticus 11:29), while worms which grow within fruit which has been picked from the tree, in the guts of an animal, or in any other place which is not on the ground are permissible for consumption. Therefore, one who wishes to know whether a certain worm is permissible for consumption or forbidden must find out if it crawled upon the ground. One of the Babylonian sages, Rav Sheshet the son of Rav Idi, argued: worms found in the lungs or liver of an animal are forbidden for consumption because they crawled upon the ground and the animal ate them. A different sage, Rav Ashi, questioned his argument: “According to your words the animal ate the worms off the ground, and so the worms ought to have been in the digestive system and not in the liver or lungs.”
(Since the sages argued orally before the text was written down and organized, the issues sometimes appear in differing forms, for it is the way of memory to be vague. When the text was edited, both versions were cited.)
There are those who cite the discuss above thusly: One sage, Rav Sheshet the son of Rav Idi, argued that worms found in the lungs and liver of animals are permissible for consumption because they were created and grew in the liver and lungs, and we have already stated that worms which never crawled upon the ground are permissible for consumption. A different sage, Rav Ashi, supported his argument and stated that it certainly was true worms found in the liver and lungs were created in the animal itself, for had the animal eaten up these worms, they would have been found in the digestive system and not in the liver and lungs.
The scholars summarized the debate and ruled: worms which are found in the liver and the lungs are forbidden for consumption, for we must assume they entered the animal’s body after having crawled upon the ground, through the animal’s snout when it slept, and through the windpipe the worms reached the lungs and the liver. They also ruled that worms which are found between the animal’s skin and flesh are forbidden for consumption, while those found between the skin and flesh of fish are permitted.
What is the difference between worms found in animal and those found in fish? Answer: an animal, before it is ritually slaughtered, is forbidden for consumption, and so the animals which grow within it are limbs of the living creature, and ritual slaughter of the animal does not suddenly make them permissible. Worms which grow within fish are permitted because fish are permissible for eating without ritual slaughter; when worms grow within fish, within flesh permissible for consumption, the worms are likewise permitted.
(Babylonian Talmud, Tractate Chulin 67b)