Who said anything about being upheld by the courts? This is contentious because news organizations have long held that it undermines the functioning of the free press when government agents pose as journalists. But I don't think anyone (including the AP) is suggesting it's illegal.