-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.tpl
352 lines (324 loc) · 11.6 KB
/
template.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
___TERMS_OF_SERVICE___
By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
https://developers.google.com/tag-manager/gallery-tos (or such other URL as
Google may provide), as modified from time to time.
___INFO___
{
"type": "CLIENT",
"id": "cvt_temp_public_id",
"categories": [
"UTILITY"
],
"__wm": "VGVtcGxhdGUtQXV0aG9yX0NhdGNoLWFsbC1DbGllbnQtU2ltby1BaGF2YQ\u003d\u003d",
"version": 1,
"securityGroups": [],
"displayName": "Catch-all Client",
"brand":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oXRsT2RX0Rs+BE+h34WH0kbagBPBnDxGFrjrMOLiUWjETSIWSP8Vlk7PSI3wPoXBc6YyAu4/wzAL2x64LY/63KrnlfvAIuSSISu3z+7v0C/Cxs9VEHIdZJTcDpMK/V7r3T/Z3P71Q9QUu5AETuQXFI0UdsdefFwbaYCtlt64VlI/B4CCsJTeRntryePeC8LraLF+L2/jRYlM8pAiJ68z/o6oheBT8rA3Jz+XJ3It7c5W+I8wXxcVud77YCG7J+zlvb3MhaY7c+FV5hxhiLNbPlmIneG+IbyMniMUO/9bMJa55eIXAv8DvgpS+FXKxV4AAAAASUVORK5CYII\u003d"
},
"description": "Use this Client to act as a fallback for requests that have no Client to claim them. You can set the status code and message in the Client settings. Remember to set the Priority to low (e.g. -1000).",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "LABEL",
"name": "info",
"displayName": "Make sure you set the \u003cstrong\u003ePriority\u003c/strong\u003e value to a smaller number than any other client you are using (e.g. \u003cstrong\u003e-1000\u003c/strong\u003e) to ensure this fallback client doesn\u0027t claim a valid request."
},
{
"type": "TEXT",
"name": "statusCode",
"displayName": "Status Code",
"simpleValueType": true,
"defaultValue": 404,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "REGEX",
"args": [
"^\\d{3}$"
],
"errorMessage": "Must be a valid status code (e.g. 404)."
}
],
"alwaysInSummary": true
},
{
"type": "TEXT",
"name": "statusMessage",
"displayName": "Status Message",
"simpleValueType": true,
"defaultValue": "No client claimed the request.",
"alwaysInSummary": true
},
{
"type": "TEXT",
"name": "allowedOrigins",
"displayName": "Allowed Origins",
"simpleValueType": true,
"valueHint": "https://www.mydomain.com,https://sub.mydomain.com",
"help": "Set to a comma-separated list of origins (e.g. https://www.mydomain.com) from which HTTP requests are allowed. Set to \u003cstrong\u003e*\u003c/strong\u003e to allow from any origin.",
"alwaysInSummary": true
},
{
"type": "CHECKBOX",
"name": "robots",
"checkboxText": "Add \"noindex\" X-Robots-Tag Header To Response",
"simpleValueType": true,
"defaultValue": true,
"help": "With this header in place, search engines will be discouraged from indexing the requested resource."
}
]
___SANDBOXED_JS_FOR_SERVER___
const claimRequest = require('claimRequest');
const getRequestHeader = require('getRequestHeader');
const getRequestPath = require('getRequestPath');
const makeNumber = require('makeNumber');
const returnResponse = require('returnResponse');
const setResponseBody = require('setResponseBody');
const setResponseHeader = require('setResponseHeader');
const setResponseStatus = require('setResponseStatus');
// Abort if request is for _set_cookie, as that should not be caught by a Client
if (getRequestPath() === '/_set_cookie') return;
claimRequest();
const origin = getRequestHeader('origin');
const allowedOrigins = data.allowedOrigins || '';
if (allowedOrigins === '*') {
setResponseHeader('Access-Control-Allow-Origin', origin);
} else {
allowedOrigins.split(',').forEach(o => {
if (o.trim() === origin) setResponseHeader('Access-Control-Allow-Origin', o);
});
}
setResponseHeader('Access-Control-Allow-Credentials', 'true');
if (data.robots) setResponseHeader('X-Robots-Tag', 'noindex');
setResponseStatus(makeNumber(data.statusCode));
setResponseBody(data.statusMessage);
returnResponse();
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "return_response",
"versionId": "1"
},
"param": []
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "access_response",
"versionId": "1"
},
"param": [
{
"key": "writeResponseAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "writeStatusAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "writeHeaderAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "writeBodyAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "writeHeadersAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "writeHeaderWhitelist",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "Access-Control-Allow-Origin"
}
]
},
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "Access-Control-Allow-Credentials"
}
]
},
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "X-Robots-Tag"
}
]
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_request",
"versionId": "1"
},
"param": [
{
"key": "headerWhitelist",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "origin"
}
]
}
]
}
},
{
"key": "headersAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "pathAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "requestAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "headerAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "queryParameterAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios:
- name: Runs with * allowedOrigins
code: |-
// Call runCode to run the template's code.
runCode(mockData);
assertApi('claimRequest').wasCalled();
assertApi('getRequestHeader').wasCalledWith('origin');
assertApi('setResponseHeader').wasCalledWith('Access-Control-Allow-Origin', 'https://allow.com');
assertApi('setResponseHeader').wasCalledWith('Access-Control-Allow-Credentials', 'true');
assertApi('setResponseStatus').wasCalledWith(404);
assertApi('setResponseBody').wasCalledWith('test');
assertApi('returnResponse').wasCalled();
- name: Runs with set allowedOrigins
code: |-
mockData.allowedOrigins = 'https://allow1.com,https://allow2.com';
// Call runCode to run the template's code.
runCode(mockData);
assertApi('claimRequest').wasCalled();
assertApi('getRequestHeader').wasCalledWith('origin');
assertApi('setResponseHeader').wasCalledWith('Access-Control-Allow-Origin', 'https://allow1.com');
assertApi('setResponseHeader').wasCalledWith('Access-Control-Allow-Origin', 'https://allow2.com');
assertApi('setResponseHeader').wasCalledWith('Access-Control-Allow-Credentials', 'true');
assertApi('setResponseStatus').wasCalledWith(404);
assertApi('setResponseBody').wasCalledWith('test');
assertApi('returnResponse').wasCalled();
setup: "const mockData = {\n statusCode: '404',\n statusMessage: 'test',\n allowedOrigins:\
\ '*'\n};\n\nmock('getRequestHeader', header => {\n if (header === 'origin') {\n\
\ return 'https://allow.com';\n } else { \n fail('getRequestHeader() called\
\ with something other than \"origin\".'); \n }\n});\n\n\n "
___NOTES___
Created on 28/10/2020, 13:00:18