How to avoid theft of potentially patentable IP when trying to obtain a Ph.D?
Let me describe fairly typical practice at reputable US universities. When someone has a patentable idea, the university itself will take over the patent process, paying lawyers and fees. The university will probably be the patent holder of record, but will make arrangements with the actual creator(s) to share any income that results from exploiting the patent. 

This can be good or bad, I suppose, but the good part is that the university covers the cost and also provides backup if anyone challenges the patent at any point. This can be very difficult for an individual to do on their own. The bad part is that the patent is probably in the university's name, though that may vary.  Another point is that the university may take it upon itself to find ways to monetize the patent if it is awarded. Again, this can be a large expense for an individual. 

But one protection for you, a student with an idea, is that if the university is, indeed, reputable, it won't try to steal your idea or let a faculty member steal it.  

But faculty at the (reputable) institutions that do a lot of patentable research normally require this (or a similar) process as part of the contract with the faculty member. Such agreements might also be necessary for a student to participate in such work. And there is usually an office, full of lawyers, dedicated to shepherding such work. So a further expectation is that the university is not only reputable, but also large enough to support that office. 

And the sharing arrangement may be standardized or, possibly, negotiated, so long as costs can be expected to be covered. 

The situation and the rules will also probably vary by a lot in other countries. 



For completeness, I'll note that large companies doing important research also tend to have (and require) some similar arrangements with their own researchers.