Skip to content

Commit 9040cf0

Browse files
authored
Merge branch 'main' into revert-3134
2 parents f8eaf54 + f31448e commit 9040cf0

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ All notable changes to this project will be documented in this file.
1919
[#3295](https://github.com/open-telemetry/opentelemetry-js/issues/3295)
2020
* fix(trace): fix an issue which caused negative span durations in web based spans
2121
[#3359](https://github.com/open-telemetry/opentelemetry-js/pull/3359) @dyladan
22+
* fix(resources): strict OTEL_RESOURCE_ATTRIBUTES baggage octet decoding
23+
[#3341](https://github.com/open-telemetry/opentelemetry-js/pull/3341) @legendecas
2224

2325
### :books: (Refine Doc)
2426

experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ describe('Node SDK', () => {
416416

417417
describe('with a faulty environment variable', () => {
418418
beforeEach(() => {
419-
process.env.OTEL_RESOURCE_ATTRIBUTES = 'bad=~attribute';
419+
process.env.OTEL_RESOURCE_ATTRIBUTES = 'bad=\\attribute';
420420
});
421421

422422
it('prints correct error messages when EnvDetector has an invalid variable', async () => {

packages/opentelemetry-resources/src/detectors/EnvDetector.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class EnvDetector implements Detector {
117117
if (!this._isValid(value)) {
118118
throw new Error(`Attribute value ${this._ERROR_MESSAGE_INVALID_VALUE}`);
119119
}
120-
attributes[key] = value;
120+
attributes[key] = decodeURIComponent(value);
121121
}
122122
return attributes;
123123
}
@@ -130,13 +130,14 @@ class EnvDetector implements Detector {
130130
* @returns Whether the String is valid.
131131
*/
132132
private _isValid(name: string): boolean {
133-
return name.length <= this._MAX_LENGTH && this._isPrintableString(name);
133+
return name.length <= this._MAX_LENGTH && this._isBaggageOctetString(name);
134134
}
135135

136-
private _isPrintableString(str: string): boolean {
136+
// https://www.w3.org/TR/baggage/#definition
137+
private _isBaggageOctetString(str: string): boolean {
137138
for (let i = 0; i < str.length; i++) {
138-
const ch: string = str.charAt(i);
139-
if (ch < ' ' || ch >= '~') {
139+
const ch = str.charCodeAt(i);
140+
if (ch < 0x21 || ch === 0x2C || ch === 0x3B || ch === 0x5C || ch > 0x7E) {
140141
return false;
141142
}
142143
}

packages/opentelemetry-resources/test/detectors/browser/EnvDetector.test.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as assert from 'assert';
1718
import { RAW_ENVIRONMENT } from '@opentelemetry/core';
1819
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
1920
import { envDetector, Resource } from '../../../src';
@@ -27,7 +28,7 @@ describeBrowser('envDetector() on web browser', () => {
2728
describe('with valid env', () => {
2829
before(() => {
2930
(globalThis as typeof globalThis & RAW_ENVIRONMENT).OTEL_RESOURCE_ATTRIBUTES =
30-
'webengine.name="chromium",webengine.version="99",webengine.description="Chromium"';
31+
'webengine.name="chromium",webengine.version="99",webengine.description="Chromium",custom.key="custom%20value"';
3132
});
3233

3334
after(() => {
@@ -41,6 +42,38 @@ describeBrowser('envDetector() on web browser', () => {
4142
[SemanticResourceAttributes.WEBENGINE_VERSION]: '99',
4243
[SemanticResourceAttributes.WEBENGINE_DESCRIPTION]: 'Chromium',
4344
});
45+
assert.strictEqual(resource.attributes['custom.key'], 'custom value');
46+
});
47+
});
48+
49+
50+
describe('with invalid env', () => {
51+
const values = [
52+
'webengine.description="with spaces"',
53+
];
54+
55+
for (const value of values) {
56+
describe(`value: '${value}'`, () => {
57+
before(() => {
58+
(globalThis as typeof globalThis & RAW_ENVIRONMENT).OTEL_RESOURCE_ATTRIBUTES = value;
59+
});
60+
61+
after(() => {
62+
delete (globalThis as typeof globalThis & RAW_ENVIRONMENT).OTEL_RESOURCE_ATTRIBUTES;
63+
});
64+
65+
it('should return empty resource', async () => {
66+
const resource: Resource = await envDetector.detect();
67+
assertEmptyResource(resource);
68+
});
69+
});
70+
}
71+
});
72+
73+
describe('with empty env', () => {
74+
it('should return empty resource', async () => {
75+
const resource: Resource = await envDetector.detect();
76+
assertEmptyResource(resource);
4477
});
4578
});
4679

packages/opentelemetry-resources/test/detectors/node/EnvDetector.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describeNode('envDetector() on Node.js', () => {
2525
describe('with valid env', () => {
2626
before(() => {
2727
process.env.OTEL_RESOURCE_ATTRIBUTES =
28-
'k8s.pod.name="pod-xyz-123",k8s.cluster.name="c1",k8s.namespace.name="default",k8s.deployment.name="deployment name"';
28+
'k8s.pod.name="pod-xyz-123",k8s.cluster.name="c1",k8s.namespace.name="default",k8s.deployment.name="deployment%20name"';
2929
});
3030

3131
after(() => {
@@ -43,6 +43,29 @@ describeNode('envDetector() on Node.js', () => {
4343
});
4444
});
4545

46+
describe('with invalid env', () => {
47+
const values = [
48+
'k8s.deployment.name="with spaces"',
49+
];
50+
51+
for (const value of values) {
52+
describe(`value: '${value}'`, () => {
53+
before(() => {
54+
process.env.OTEL_RESOURCE_ATTRIBUTES = value;
55+
});
56+
57+
after(() => {
58+
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
59+
});
60+
61+
it('should return empty resource', async () => {
62+
const resource: Resource = await envDetector.detect();
63+
assertEmptyResource(resource);
64+
});
65+
});
66+
}
67+
});
68+
4669
describe('with empty env', () => {
4770
it('should return empty resource', async () => {
4871
const resource: Resource = await envDetector.detect();

0 commit comments

Comments
 (0)