Generating collection documentation for PRs with github-docs-build / antsibull-docs

If you use the shared workflows from the github-docs-build project to generate documentation for PRs in your collection, you should be aware of two important things:

  1. The shared workflows for PR generation are ususally used in collection workflows using the pull_request_target trigger. This trigger comes with some risk, and GitHub is very soon (December 2nd) introducing new restrictions for running such workflows. If you don’t do anything, your PR docs generation workflows will likely stop working then. You can find more details here: pull_request_target blocked by default December 2 - GitHub Changelog

    One way to prevent this is to create an Actions Policy like this one:

    To see what might get disabled, you can check the “Policy Insights” section of your “Actions” repository settings. There you can see workflow runs that would get blocked with the new rules. You can also see warnings in current workflow runs, like this one (there they warn about an upcoming change on November 2nd; apparently it was pushed back to December 2nd).

  2. You should make sure that you are using antsibull-docs 2.29.x or newer when generating collection docs - in particular in sensitive workflows that use pull_request_target! - since that’s the first version that no longer uses ansible-doc to fetch collection documentation. (ansible-doc is only used to fetch ansible-core documentation.) The problem with using ansible-doc is that it will execute code in certain plugins when retrieving documentation! So if you run ansible-doc to extract documentation in a workflow running with pull_request_target, you are running untrusted code in such a workflow, which is exactly what you should NOT do in pull_reuqest_target workflows.

    I wasn’t aware that ansible-doc will execute collection code until recently (and I guess most people aren’t aware of this either, since most plugin documentation can be read without executing code), and I spent some time in the last weeks to make sure that antsibull-docs now by default uses a method which does not rely on ansible-doc. Please make sure that if you use antsibull-docs in a workflow, that you don’t pin to an older version or explicitly configure the docs parsing backend to the previous default.

4 Likes

How come did we end up in a situation where reading docs and linting will result in untrusted code execution?
I do not expect answer to this question, it just comes to mind, nothing I can do about it.

1 Like

This happened in two places:

  • When doc fragments were introduced. For some reason, the doc fragments are imported as Python code instead of using the ast package to extract documentation (as it is done for documentation in plugins and modules).
  • When filter and test documentation was added. The list of filters and tests in a Python file was always returned by code, and adding documentation required to run that code also when extracting documentation.

Both alai-doc and antsibull-docs-loader use ast (resp. some Rust analogue in case of alai-doc) to extract the documentation staticallly in these cases, which works for 99.9% of all documentation. (All other cases I’m aware of got issues in their issue tracker :slight_smile: )

Regarding linting: ansible-lint doesn’t use ansible-doc, but has several other ways to run collection code (by evaluating Jinja2 expressions which acutally runs tests and filters, and by importing modules to extract their argument spec). I remember reporting that some years ago, but apparently it wasn’t deemed important enough to be addressed. (Maybe it has in the meantime, I haven’t checked for some time.)

2 Likes

kks, that question is the interesting half of the post, and the answer is a bit uncomfortable: for filters and tests the documentation list has always come back from running the Python file, so a docs build is a code execution by design, not an accident of the workflow. Modules avoided it because they could be read statically, fragments landed on the wrong side of that line and nobody went back to change it.

Where it really bites is the pull request target trigger with a checkout of the head, because then the code that runs during a docs build is whatever the contributor pushed. I moved my doc builds out of the PR workflow for that reason, they now run after merge in a job with no secrets and no write scope, and I sleep better for it.

Do you plan to get the filter and test docs rendered from a parsed file eventually, or keep the execution and just tighten the workflow side?