Skip to content

Commit 60073fc

Browse files
Merge pull request #8311 from v-thepet/pipelines9-6
Freshness: Pipelines 6
2 parents e42ad9c + 662ff44 commit 60073fc

File tree

2 files changed

+83
-77
lines changed

2 files changed

+83
-77
lines changed
9.31 KB
Loading

docs/pipelines/security/templates.md

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
---
2-
title: Use templates for security
3-
description: Learn about using template features to improve pipeline security.
2+
title: Templates for 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'
8+
#customer intent: As an Azure DevOps user, I want to understand how pipeline templates can help increase security, so I can use templates to do security tasks and help prevent malicious code infiltration and execution.
89
---
910

10-
# Use templates for security
11+
# Templates for security
1112

1213
[!INCLUDE [version-gt-eq-2020](../../includes/version-gt-eq-2020.md)]
1314

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.
15+
Azure Pipelines [templates](../process/templates.md) let you define reusable content, logic, and parameters in YAML pipelines. This article describes how templates can help enhance pipeline security by:
1516

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.
17+
- Defining the outer structure of a pipeline to help prevent malicious code infiltration.
18+
- Automatically including steps to do tasks such as credential scanning.
19+
- Helping enforce [checks on protected resources](resources.md), which form the fundamental security framework for Azure Pipelines and apply to all pipeline structures and components.
1720

1821
[!INCLUDE [security-prerequisites](includes/security-prerequisites.md)]
1922

2023
## Includes and extends templates
2124

2225
Azure Pipelines provides *includes* and *extends* templates.
2326

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.
27+
- An `includes` template 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.
2528

2629
```yaml
2730
steps:
2831
- template: templates/include-npm-steps.yml
2932
```
3033
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.
34+
- 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.
3235

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).
36+
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).
3437

3538
<a name="use-extends-templates"></a>
36-
## Extends templates
39+
### Extends templates
3740

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.
41+
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.
3942

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

4245
```yaml
4346
parameters:
@@ -49,7 +52,7 @@ steps:
4952
- ${{ step }}
5053
```
5154

52-
The following pipeline extends the *template.yml* template.
55+
The following example pipeline extends the *template.yml* template.
5356

5457
```yaml
5558
# azure-pipelines.yml
@@ -69,38 +72,70 @@ extends:
6972
```
7073

7174
>[!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.
75+
>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.
7376

74-
## YAML pipeline security features
77+
## Pipeline security features
7578

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.
79+
YAML pipeline syntax includes several built-in protections. `Extends` templates can enforce their use to enhance pipeline security. You can implement any of the following restrictions.
7780

7881
### Step targets
7982

80-
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.
83+
You can restrict specified steps to run in a container rather than on the host. Steps in containers can't access the agent host, so they can't modify agent configuration or leave malicious code for later execution.
8184

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.
85+
For example, you can run user steps in a container to prevent them from accessing the network, so they can't retrieve packages from unauthorized sources or upload code and secrets to external locations.
8386

84-
The following example pipeline runs steps on the agent host before running steps inside a container.
87+
The following example pipeline runs a step on the agent host that could potentially alter the host network, followed by a step inside a container that limits network access.
8588

8689
```yaml
8790
resources:
8891
containers:
8992
- container: builder
9093
image: mysecurebuildcontainer:latest
9194
steps:
92-
- script: echo This step runs on the agent host, and it could use Docker commands to tear down or limit the container's network
95+
- script: echo This step runs on the agent host
9396
- script: echo This step runs inside the builder container
9497
target: builder
9598
```
9699

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

99134
### Agent logging command restrictions
100135

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.
136+
User steps request services by using *logging commands*, which are specially formatted strings printed to standard output. You can restrict the services that logging commands provide for user steps. In `restricted` mode, most agent services such as uploading artifacts and attaching test results are unavailable for logging commands.
102137

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

105140
```yaml
106141
- task: PublishBuildArtifacts@1
@@ -110,11 +145,13 @@ The following example task fails because its `target` property instructs the age
110145
commands: restricted
111146
```
112147

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.
148+
#### Variables in logging commands
149+
150+
The `setvariable` command remains permissible in `restricted` mode, so tasks that output user-provided data, such as open issues retrieved via a REST API, might be vulnerable to injection attacks. Malicious user content could set variables that export to subsequent tasks as environment variables and could compromise the agent host.
114151

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.
152+
To mitigate this risk, you can explicitly declare the variables that are settable by using the `setvariable` logging command. If you specify an empty list in `settableVariables`, all variable setting is disallowed.
116153

117-
The following example task fails because the task is only allowed to set the `expectedVar` variable or a variable prefixed with `ok`.
154+
The following example restricts the `settableVariables` to `expectedVar` and any variable prefixed with `ok`. The task fails because it attempts to set a different variable called `BadVar`.
118155

119156
```yaml
120157
- task: PowerShell@2
@@ -131,7 +168,7 @@ The following example task fails because the task is only allowed to set the `ex
131168

132169
### Conditional stage or job execution
133170

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.
171+
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.
135172

136173
```yaml
137174
jobs:
@@ -148,9 +185,9 @@ jobs:
148185

149186
Azure Pipelines templates have the flexibility to iterate over and modify YAML syntax. By using iteration, you can enforce specific YAML security features.
150187

151-
A template can also rewrite user steps, allowing only approved tasks to run. For example, you can prevent inline script execution.
188+
A template can also rewrite user steps, allowing only approved tasks to run. For example, the template can prevent inline script execution.
152189

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`.
190+
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`.
154191

155192
```yaml
156193
# template.yml
@@ -178,7 +215,7 @@ steps:
178215
displayName: 'Disabled by template: ${{ step.displayName }}'
179216
```
180217

181-
In the following pipeline that extends this template, the script steps are stripped out and not run.
218+
In the following example pipeline that extends the preceding template, the script steps are stripped out and not run.
182219

183220
```yaml
184221
# azure-pipelines.yml
@@ -187,57 +224,24 @@ extends:
187224
parameters:
188225
usersteps:
189226
- 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!
227+
- script: echo This step is stripped out and not run
228+
- bash: echo This step is stripped out and not run
229+
- powershell: echo "This step is stripped out and not run"
230+
- pwsh: echo "This step is stripped out and not run"
231+
- script: echo This step is stripped out and not run
195232
- task: CmdLine@2
196-
displayName: Test - Will be stripped out
233+
displayName: Test - stripped out
197234
inputs:
198-
script: echo This step will be stripped out and not run!
235+
script: echo This step is stripped out and not run
199236
- task: MyOtherTask@2
200237
```
201238

202239
:::moniker-end
203240

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-
236241
::: moniker range=">=azure-devops"
237-
238242
### Template steps
239243

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.
244+
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.
241245

242246
```yaml
243247
parameters:
@@ -258,22 +262,24 @@ jobs:
258262

259263
## Template enforcement
260264

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).
265+
The effectiveness of templates as a security mechanism relies on enforcement. The key control points for enforcing template usage are [protected resources](resources.md).
266+
267+
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).
262268

263269
<a name="set-required-templates"></a>
264270
### Required templates
265271

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.
272+
To enforce the use of a specific template, configure the [required template](../process/approvals.md#required-template) check on the service connection for a resource. This check applies only when the pipeline extends from a template.
267273

268-
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.
274+
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.
269275

270276
:::image type="content" source="../process/media/approval-fail.png" alt-text="Screenshot showing a failed approval check.":::
271277

272278
When you use the required template, the check passes.
273279

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

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

278284
```yaml
279285
# params.yml
@@ -295,7 +301,7 @@ steps:
295301
- script: echo ${{ parameters.image }}
296302
```
297303

298-
The following example pipeline extends the *params.yml* template and requires it for approval. To demonstrate a pipeline failure, comment out the reference to *params.yml*.
304+
The following example pipeline extends the *params.yml* template and requires it for approval. To demonstrate a pipeline failure, comment out the `extends` reference to *params.yml*.
299305

300306
```yaml
301307
# azure-pipeline.yml
@@ -315,7 +321,7 @@ extends:
315321

316322
## Related content
317323

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

0 commit comments

Comments
 (0)