The tikz-cd environment actually is a tikzpicture with a matrix inside, which it's itself a node, you could append a style and tell tikz to draw its border.
Moreover, for the same reason (tikz-cd environment is a tikzpicture), be careful to inserting it in another tikzpicture (nesting tikzpictures is not recommended). You could use execute at end picture or remember picture and overlay instead.
If you want to use the same formatting for more than one commutative diagram of yours, you could create a style, possibly with some parameters.
\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{
boxedcd/.style={
every matrix/.append style={
draw=red,
thick,
fill=yellow!50!white,
rounded corners,
#1
},
},
}
\begin{document}
\[
\begin{tikzcd}[every matrix/.append style={draw, inner ysep=4pt}]
A \ar[r, "f"] & B
\end{tikzcd}
\]
Of course, you can set the node as you like:
\[
\begin{tikzcd}[every matrix/.append style={draw=red,
fill=yellow, inner ysep=4pt, rounded corners}]
A \ar[r, "f"] & B
\end{tikzcd}
\]
Example of \texttt{execute at end picture}:
\[
\begin{tikzcd}[every matrix/.append style={name=mymatr},
execute at end picture={
\draw[red,rounded corners] (mymatr.south west) -- (mymatr.south east) -- ([yshift=4pt]mymatr.north east) -- ([yshift=4pt]mymatr.north west) -- cycle;
}]
A \ar[r, "f"] & B
\end{tikzcd}
\]
Example of use of \texttt{overlay}:
\[
\begin{tikzcd}[remember picture,
every matrix/.append style={name=mymatrix}]
A \ar[r, "f"] & B
\end{tikzcd}
\]
\begin{tikzpicture}[remember picture, overlay]
\draw[blue,rounded corners] (mymatrix.south west) -- (mymatrix.south east) -- ([yshift=4pt]mymatrix.north east) -- ([yshift=4pt]mymatrix.north west) -- cycle;
\end{tikzpicture}
\noindent Example of use of a \texttt{stlye}:
\[
\begin{tikzcd}[boxedcd={inner sep=1pt}]
A \arrow[r] & B
\end{tikzcd}\quad
\begin{tikzcd}[boxedcd={inner ysep=4pt}]
A \arrow[r, "f"] & B
\end{tikzcd}\quad
\begin{tikzcd}[boxedcd={inner xsep=4pt, inner ysep=2pt}]
A \arrow[r] & B\\
C \arrow[u, "g"]
\end{tikzcd}
\]
\end{document}

fboxstyle. Thebackgroundslibrary provides more customization. If you want to use a CD as part of a bigger TikZ picture, you can use a small adjustment to thetikzcdenvironment so that it can be used inside a tikzpicture almost as easy as the\matrixit is. – Qrrbrbirlbel Nov 21 '22 at 00:59