From the Shepherd the flock book: (Bolding and formatting theirs)
Was the Wrongdoer’s Baptism Valid?
46. When dealing with wrongdoers, the elders should not raise questions about the validity of the individual’s baptism. If the individual raises the issue, the elders may refer him to the February 15, 2010, Watchtower, page 22. Further information on rebaptism can be found in the February 15, 1964, Watchtower, pages 123-126, and the March 1, 1960, Watchtower, pages 159-160. Rebaptism is a personal matter.
47. At times a wrongdoer will claim that his baptism is not valid and that he feels he is not accountable to a judicial committee because he secretly engaged in wrongdoing shortly before he was baptized. If the elders had been aware of his serious wrongdoing committed just before baptism, likely they would not have approved him for baptism. However, this does not necessarily mean that he did not make a valid dedication. Some individuals make a dedication long before the baptism; others have made a dedication shortly before. The elders are not in a position to read the heart and know for a certainty how Jehovah viewed the person at the time he was baptized. If the elders learn that a baptized individual secretly engaged in serious wrongdoing while he was an unbaptized publisher but the wrongdoing ceased before baptism, they should give counsel and encouragement. A judicial committee should not be formed for prebaptism wrongdoing. (1 Cor. 6:9-11) However, if the individual resumed serious wrongdoing after baptism, the elders would generally deal with him on the basis of what he has professed to be, a dedicated and baptized Christian, and would meet with him judicially. Thereafter, if he feels he should be rebaptized, this would be a personal decision. However, a person should not get rebaptized simply because he gains improved understanding or appreciation of the truth or some Scriptural doctrine. —Prov. 4:18.
48. There are rare occasions when it is obvious that the baptism was invalid because serious wrongdoing did not cease before baptism, even for a brief period of time. For example, it may be that at the time of baptism, the individual was living immorally with a member of the opposite or the same sex, was a member of a non-neutral organization, or something similar. If there are questions, the branch office should be consulted.