Ideally, you only want to keep it in the secondary for a short period of time before bottling or kegging. Of course that comes with exceptions to every rule. My rule of thumb is the higher the alcohol content, the longer you can let it rest. So a pale ale may stay a couple of weeks in the secondary but never more than a month but I have made barley wines that stayed in the secondary for nine months with no problem. Heck, I've had meads in the secondary for almost two years sometimes.
Bigger beers take more time for the flavors to meld. Smaller beers usually ferment quick and then are in their prime much sooner. YOu don't want to leave a beer in so long that it goes stale.
All that said, if you are worried about the beer getting infected, it probably won't even if you let it sit for a few months. As long as you are good about sanitation you are fine. But again, depending on the beer the flavor quality may suffer.
If a beer takes two weeks to ferment, I would bottle after two weeks in the secondary. Three weeks...three weeks and so on. But that is me. I'm sure others have different ways of doing things. Bottom line, if what you are doing gets you the results you are happy with, then keep it up!