Skip to content

Commit 4b4fc6c

Browse files
committed
draft
1 parent 1d69acc commit 4b4fc6c

File tree

1 file changed

+76
-71
lines changed

1 file changed

+76
-71
lines changed

docs/pipelines/security/templates.md

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
---
2-
title: Use templates for security
3-
description: Learn about using template features to improve pipeline security.
2+
title: Template use to improve security
3+
description: Learn about using template features to help improve pipeline security.
44
ms.assetid: 73d26125-e3ab-4e18-9bcd-387fb21d3568
5-
ms.date: 07/18/2024
5+
ms.date: 09/12/2025
66
ms.topic: conceptual
77
monikerRange: '>= azure-devops-2020'
88
---
99

10-
# Use templates for security
10+
# Use templates to help improve security
1111

1212
[!INCLUDE [version-gt-eq-2020](../../includes/version-gt-eq-2020.md)]
1313

14-
This article describes how templates can streamline security for Azure Pipelines. Templates can define the outer structure of your pipeline and help prevent malicious code infiltration. Templates can also automatically include steps to do tasks such as credential scanning. If multiple pipelines within your team or organization share the same structure, consider using templates.
14+
If multiple pipelines within your team or organization share the same structure, consider using [templates](../process/templates.md). This article describes how templates can streamline security for Azure Pipelines.
1515

16-
[Checks on protected resources](resources.md) form the fundamental security framework for Azure Pipelines. These checks apply regardless of pipeline structure, stages, and jobs. You can use templates to help enforce these checks.
16+
Pipeline templates can:
17+
18+
- Define the outer structure of your pipeline to help prevent malicious code infiltration.
19+
- Automatically include steps to do tasks such as credential scanning.
20+
- Help enforce [checks on protected resources](resources.md). These checks form the fundamental security framework for Azure Pipelines and apply to all pipeline structures, stages, and jobs.
1721

1822
[!INCLUDE [security-prerequisites](includes/security-prerequisites.md)]
1923

2024
## Includes and extends templates
2125

2226
Azure Pipelines provides *includes* and *extends* templates.
2327

24-
- Includes templates include the template's code directly in the outer file that references it, similar to `#include` in C++. The following example pipeline inserts the *include-npm-steps.yml* template into the `steps` section.
28+
- An `includes` templates includes the template's code directly in the outer file that references the template, similar to `#include` in C++. The following example pipeline inserts the *include-npm-steps.yml* template into the `steps` section.
2529

2630
```yaml
2731
steps:
2832
- template: templates/include-npm-steps.yml
2933
```
3034
31-
- Extends templates define the outer structure of the pipeline and offer specific points for targeted customizations. In the context of C++, `extends` templates resemble inheritance.
35+
- An `extends` template defines the outer structure of the pipeline and offers specific points for targeted customizations. In the context of C++, `extends` templates resemble inheritance.
3236

33-
When you use `extends` templates, you can also use `includes` in both the template and the final pipeline to do common configuration pieces. For a complete reference, see the [Template usage reference](../process/templates.md).
37+
When you use `extends` templates, you can also use `includes` to do common configuration pieces in both the template and the final pipeline. For more information, see [Use YAML templates in pipelines for reusable and secure processes](../process/templates.md).
3438

3539
<a name="use-extends-templates"></a>
36-
## Extends templates
40+
### Extends templates
3741

38-
For the most secure pipelines, start by using extends templates. These templates define the outer structure of the pipeline and prevent malicious code from infiltrating the pipeline.
42+
For the most secure pipelines, start by using `extends` templates. These templates define the outer structure of the pipeline and help prevent malicious code infiltration.
3943

40-
For example, the following template file is named *template.yml*.
44+
The following example shows a template file named *template.yml*.
4145

4246
```yaml
4347
parameters:
@@ -49,7 +53,7 @@ steps:
4953
- ${{ step }}
5054
```
5155

52-
The following pipeline extends the *template.yml* template.
56+
The following pipeline code extends the *template.yml* template.
5357

5458
```yaml
5559
# azure-pipelines.yml
@@ -69,19 +73,19 @@ extends:
6973
```
7074

7175
>[!TIP]
72-
>When you set up `extends` templates, consider anchoring them to a particular Git branch or tag so if there are breaking changes, existing pipelines aren't affected. The preceding example uses this feature.
76+
>When you set up `extends` templates, consider anchoring them to a particular Git branch or tag so any breaking changes don't affect existing pipelines. The preceding example uses this feature.
7377

74-
## YAML pipeline security features
78+
## Pipeline security features
7579

76-
The YAML pipeline syntax includes several built-in protections. Extends template can enforce their use. To enhance pipeline security, you can implement any of the following restrictions.
80+
The YAML pipeline syntax includes several built-in protections. `Extends` template can enforce their use to enhance pipeline security. You can implement any of the following restrictions.
7781

7882
### Step targets
7983

8084
You can restrict certain steps to run in a container rather than on the host. Steps in containers don't have access to the agent's host, preventing these steps from modifying agent configuration or leaving malicious code for later execution.
8185

82-
For example, consider limiting network access. Without open network access, user steps can't retrieve packages from unauthorized sources or upload code and secrets to external network locations.
86+
For example, you can limit network access. Without open network access, user steps can't retrieve packages from unauthorized sources or upload code and secrets to external network locations.
8387

84-
The following example pipeline runs steps on the agent host before running steps inside a container.
88+
The following example pipeline runs a step on the agent host before running a step inside a container.
8589

8690
```yaml
8791
resources:
@@ -94,13 +98,45 @@ steps:
9498
target: builder
9599
```
96100

101+
### Type-safe parameters
102+
103+
Before a pipeline runs, templates and their parameters transform into constants. [Template parameters](../process/template-parameters.md) can enhance type safety for input parameters.
104+
105+
In the following example template, the parameters restrict the available pipeline pool options by enumerating specific choices instead of allowing freeform strings.
106+
107+
```yaml
108+
# template.yml
109+
parameters:
110+
- name: userpool
111+
type: string
112+
default: Azure Pipelines
113+
values:
114+
- Azure Pipelines
115+
- private-pool-1
116+
- private-pool-2
117+
118+
pool: ${{ parameters.userpool }}
119+
steps:
120+
- script: echo Hello world
121+
```
122+
123+
When it extends the template, the pipeline must specify one of the available pool choices.
124+
125+
```yaml
126+
# azure-pipelines.yml
127+
extends:
128+
template: template.yml
129+
parameters:
130+
userpool: private-pool-1
131+
```
132+
97133
::: moniker range=">=azure-devops-2022"
98134

99135
### Agent logging command restrictions
100136

101-
You can restrict the services the Azure Pipelines agent provides to user steps. User steps request services by using *logging commands*, which are specially formatted strings printed to standard output. In restricted mode, most of the agent's services, such as uploading artifacts and attaching test results, are unavailable.
137+
User steps request services by using *logging commands*, which are specially formatted strings printed to standard output. You can restrict the services that the Azure Pipelines agent provides to user steps. In restricted mode, most of the agent's services, such as uploading artifacts and attaching test results, are unavailable.
102138

103-
The following example task fails because its `target` property instructs the agent not to allow publishing artifacts.
139+
In the following example, the `target` property instructs the agent not to allow publishing artifacts, so the task fails.
104140

105141
```yaml
106142
- task: PublishBuildArtifacts@1
@@ -110,11 +146,11 @@ The following example task fails because its `target` property instructs the age
110146
commands: restricted
111147
```
112148

113-
In `restricted` mode, the `setvariable` command remains permissible, so caution is necessary because pipeline variables are exported as environment variables to subsequent tasks. If tasks output user-provided data, such as open issues retrieved via a REST API, they might be vulnerable to injection attacks. Malicious user content could set environment variables that might be exploited to compromise the agent host.
149+
The `setvariable` command remains permissible in `restricted` mode, and pipeline variables can be exported as environment variables to subsequent tasks. If tasks output user-provided data, such as open issues retrieved via a REST API, they might be vulnerable to injection attacks. Malicious user content could set environment variables that might be exploited to compromise the agent host.
114150

115-
To mitigate this risk, pipeline authors can explicitly declare which variables are settable by using the `setvariable` logging command. When you specify an empty list, all variable setting is disallowed.
151+
To mitigate this risk, you can explicitly declare which variables are settable by using the `setvariable` logging command. If you specify an empty list, all variable setting is disallowed.
116152

117-
The following example task fails because the task is only allowed to set the `expectedVar` variable or a variable prefixed with `ok`.
153+
The following example restricts the `settableVariables` to `expectedVar` or a variable prefixed with `ok`. The task fails because it attempts to set a different variable.
118154

119155
```yaml
120156
- task: PowerShell@2
@@ -131,7 +167,7 @@ The following example task fails because the task is only allowed to set the `ex
131167

132168
### Conditional stage or job execution
133169

134-
You can restrict stages and jobs to run only under specific conditions. In the following example, the condition ensures that restricted code builds only for the main branch.
170+
You can restrict stages and jobs to run only under specific conditions. The following example ensures that restricted code builds only for the `main` branch.
135171

136172
```yaml
137173
jobs:
@@ -150,7 +186,7 @@ Azure Pipelines templates have the flexibility to iterate over and modify YAML s
150186

151187
A template can also rewrite user steps, allowing only approved tasks to run. For example, you can prevent inline script execution.
152188

153-
The following example template prevents the step types `bash`, `powershell`, `pwsh`, and `script` from running. For complete lockdown of ad-hoc scripts, you could also block `BatchScript` and `ShellScript`.
189+
The following example template prevents the script step types `bash`, `powershell`, `pwsh`, and `script` from running. For complete lockdown of scripts, you could also block `BatchScript` and `ShellScript`.
154190

155191
```yaml
156192
# template.yml
@@ -187,57 +223,24 @@ extends:
187223
parameters:
188224
usersteps:
189225
- task: MyTask@1
190-
- script: echo This step will be stripped out and not run!
191-
- bash: echo This step will be stripped out and not run!
192-
- powershell: echo "This step will be stripped out and not run!"
193-
- pwsh: echo "This step will be stripped out and not run!"
194-
- script: echo This step will be stripped out and not run!
226+
- script: echo This step is stripped out and not run
227+
- bash: echo This step is stripped out and not run
228+
- powershell: echo "This step is stripped out and not run"
229+
- pwsh: echo "This step is stripped out and not run"
230+
- script: echo This step is stripped out and not run
195231
- task: CmdLine@2
196-
displayName: Test - Will be stripped out
232+
displayName: Test - stripped out
197233
inputs:
198-
script: echo This step will be stripped out and not run!
234+
script: echo This step is stripped out and not run
199235
- task: MyOtherTask@2
200236
```
201237

202238
:::moniker-end
203239

204-
### Type-safe parameters
205-
206-
Before a pipeline runs, templates and their parameters are transformed into constants. [Template parameters](../process/template-parameters.md) can enhance type safety for input parameters.
207-
208-
In the following example template, the parameters restrict the available pipeline pool options by providing an enumeration of specific choices instead of allowing freeform strings.
209-
210-
```yaml
211-
# template.yml
212-
parameters:
213-
- name: userpool
214-
type: string
215-
default: Azure Pipelines
216-
values:
217-
- Azure Pipelines
218-
- private-pool-1
219-
- private-pool-2
220-
221-
pool: ${{ parameters.userpool }}
222-
steps:
223-
- script: # ... removed for clarity
224-
```
225-
226-
When the pipeline extends the template, it has to specify one of the available pool choices.
227-
228-
```yaml
229-
# azure-pipelines.yml
230-
extends:
231-
template: template.yml
232-
parameters:
233-
userpool: private-pool-1
234-
```
235-
236240
::: moniker range=">=azure-devops"
237-
238241
### Template steps
239242

240-
A template can automatically include steps in a pipeline. These steps can do tasks such as credential scanning or static code checks. The following template inserts steps before and after the user steps in every job.
243+
A template can automatically include steps in a pipeline, for example to do credential scanning or static code checks. The following template inserts steps before and after the user steps in every job.
241244

242245
```yaml
243246
parameters:
@@ -258,12 +261,14 @@ jobs:
258261

259262
## Template enforcement
260263

261-
Templates are a valuable security mechanism, but their effectiveness relies on enforcement. The key control points for enforcing template usage are [protected resources](resources.md). You can configure approvals and checks for your agent pool or other protected resources such as repositories. For an example, see [Add a repository resource check](../process/repository-resource.md#add-a-repository-resource-check).
264+
The effectiveness of templates as a security mechanism relies on enforcement. The key control points for enforcing template usage are [protected resources](resources.md).
265+
266+
You can configure [approvals and checks](../process/approvals.md) for your agent pool or other protected resources such as repositories. For an example, see [Add a repository resource check](../process/repository-resource.md#add-a-repository-resource-check).
262267

263268
<a name="set-required-templates"></a>
264269
### Required templates
265270

266-
To enforce the use of a specific template, configure the [required template check](../process/approvals.md#required-template) for a resource. This check applies only when the pipeline extends from a template.
271+
To enforce the use of a specific template, configure the [required template](../process/approvals.md#required-template) check for a resource. This check applies only when the pipeline extends from a template.
267272

268273
When you view the pipeline job, you can monitor the check's status. If the pipeline doesn't extend from the required template, the check fails. The run stops and notifies you of the failed check.
269274

@@ -273,7 +278,7 @@ When you use the required template, the check passes.
273278

274279
:::image type="content" source="../process/media/approval-pass.png" alt-text="Screenshot showing a passed approval check.":::
275280

276-
The following *params.yml* template must be referenced in any pipeline that extends it.
281+
For example, the following *params.yml* template must be referenced in any pipeline that extends it.
277282

278283
```yaml
279284
# params.yml
@@ -315,7 +320,7 @@ extends:
315320

316321
## Related content
317322

318-
- [Template usage reference](../process/templates.md)
319-
- [Secure variables and parameters in pipelines](inputs.md)
323+
- [Template usage](../process/templates.md)
324+
- [Template parameters](../process/template-parameters.md)
320325
- [Resource security](resources.md)
321326
- [Approvals and checks](../process/approvals.md)

0 commit comments

Comments
 (0)