Is any of this legal?
It's unclear. The NFL doesn't allow teams to trade players for cash, defining it as conduct detrimental to the league and subject to disciplinary action. Teams are not allowed to make trades for past, future, or nominal considerations, although future draft selections are obviously allowed.
This trade falls into a gray area. Precedent suggests that NFL teams are allowed to make trades that are lopsided on their face by virtue of the discount many teams place on future selections. The 49ers couldn't trade the 66th pick in this year's draft to the Buccaneers for the 50th selection, as it would obviously look fishy, but nobody would raise an eyebrow if San Francisco traded the 66th pick to the Bucs for a 2018 second-rounder. Tampa's second-rounder obviously can't fall any lower than 64th even if they win the Super Bowl, so the 49ers are guaranteed to get a superior pick, but the 2018 pick is seen as less valuable because the 49ers won't get it for 12 months.
Brock Osweiler is headed to Cleveland ... for now. Troy Taormina/USA TODAY Sports
It's no accident that the Browns picked up a 2018 second-rounder as part of this deal in lieu of a 2017 selection. It gives them ammunition in justifying the trade to the league as a reasonable trade for assets. Trading a fourth-rounder for a sixth-rounder and a future second-rounder is more of a discount than most, but it's way less damaging of a move than trading a future first-round pick for a second-round pick in the present draft, and teams have made that move plenty of times in years past. I don't think the league will prevent this deal from going through, but I wouldn't be surprised if they suggested (publicly or privately) to the Browns and the rest of the NFL that future deals in this vein are likely to be vetoed.
If anything, I suspect that the Browns might have been artificially constrained in terms of how much compensation they could credibly receive from the Texans before the league would have stepped in and declared this to be a pure salary dump. I suggested that they would have needed a first-round pick as part of a package to justify absorbing Osweiler's salary. If there was precedent this would be accepted as a legal move by the NFL, I think the Browns would have insisted upon another mid-round pick to help ease the cost of eating millions of dollars in salary.