It's not illegal for undercover cops/agents to do illegal activities, but it does significantly reduce their credibility in court when the day comes when they have to testify against the criminals they have been working on.
This is a quote from the book of a legit undercover agent Joseph D. Pistone a.k.a Donnie Brasco, who was undercover in the mafia for 6 years and his work led to over 100 convictions of mobsters:
"I didn’t do these things to be a tough guy. But with things like drinking and drugs, you can’t be a fence-sitter around these guys. If you smoke a joint or take a snort the first time—maybe just to show that you’re a regular guy—or if you say, “Maybe later,” it gives the impression that you do drugs. If you’re a fence sitter, then you’re in a bind. You just invite people to keep offering it to you. And if you say, “Not now,” and then keep refusing and refusing and putting it off, they begin to wonder: What’s up with this guy? But if you draw the line right in the beginning—I don’t do it; I ain’t ever gonna do it—then that’s it, nobody cares anymore......
The thing is, even though it’s a fake world for you as an undercover agent, it’s a real world for the people that you’re dealing with. And you have to abide by the rules in that world. And those rules include how you establish your own standards, credibility, and individuality. I know one or two guys that drank or did drugs while they were undercover just because they thought they had to do that to blend in or show they were tough guys. It was an enormous mistake. You can’t compromise your own standards and personality. Smart wiseguys will see right through your act. You look like somebody that has no mind of his own, hence no strength. I don’t use drugs, and I wasn’t going to start using them then just for an undercover role. How could I tell my kids not to use dope if I was out there sniffing coke just for the job?
And there’s another reason, very practical. As an FBI agent, someday down the line I was going to be in court testifying on all these cases we were making. I was not going to lie. And I was not going to tarnish my credibility and risk failing on convictions by taking drugs or getting drunk or doing anything that would suggest I lacked commitment or character."
"I faced two major problems. One was that as an agent, I couldn’t actually participate in a hit—in fact, it was our duty to prevent the hit if possible — yet as a badguy I couldn’t turn down the invitation without losing credibility. What would I do if and when that situation came up? As an agent, I can’t allow a hit to go through, can’t condone it, certainly can’t participate in it, if I know it’s going to happen. But I could find myself in the situation all of a sudden"
So, while it's not forbidden for agents to participate in illegal activities, but it risks tarnishing the mission and losing the conviction for a number of reasons. Like for example how are you going to convict a guy you've been working on for murder if you're an accomplice and actually participated in it?
So if Lily is an actual undercover cop, then simply she's bad at her job or it's another bad writing by DPC, trying to come up with something he knows nothing about (like the Maya's bank and loan story for example).