Sailing within the Wind
or Sailing close to the Wind. Going to the very verge of propriety, or acting so as just to escape the letter of the law. The phrase, of course, is nautical.
“The jokes [of our predecessors] might have been broader than modern manners allow, but ... the masher sails nearer the wind than did his ruder forefathers.” —Nineteenth Century, November, 1892, p. 795.
“Ea defended himself by declaring that he did not tell Hasisadra anything; he only sent her a dream. This was undoubtedly sailing very near the wind.” —Nineteenth Century, June, 1891, p. 911.
Source: Dictionary of Phrase and Fable, E. Cobham Brewer, 1894