Skip to content

Commit 6871574

Browse files
authored
fix(core): redact sensitive fields within collections (#2342)
1 parent fe13e99 commit 6871574

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/core": patch
3+
---
4+
5+
Redact sensitive fields within list items and map values in schema-based logging.

β€Žpackages/core/src/submodules/client/smithy-client/schemaLogFilter.spec.tsβ€Ž

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { describe, expect, test as it } from "vitest";
1010
import { schemaLogFilter } from "./schemaLogFilter";
1111

1212
describe(schemaLogFilter.name, () => {
13-
it("should filter sensitive trait-marked fields", () => {
14-
const sensitiveString: StaticSimpleSchema = [0, "ns", "SensitiveString", { sensitive: 1 }, 0];
13+
const sensitiveString: StaticSimpleSchema = [0, "ns", "SensitiveString", { sensitive: 1 }, 0];
1514

15+
it("should filter sensitive trait-marked fields", () => {
1616
const schema: StaticStructureSchema = [
1717
3,
1818
"ns",
@@ -93,4 +93,36 @@ describe(schemaLogFilter.name, () => {
9393
},
9494
});
9595
});
96+
97+
it("should filter sensitive fields within list and map values", () => {
98+
const value: StaticStructureSchema = [
99+
3,
100+
"ns",
101+
"Value",
102+
0,
103+
["public", "sensitive"],
104+
[0 satisfies StringSchema, sensitiveString],
105+
];
106+
const schema: StaticStructureSchema = [
107+
3,
108+
"ns",
109+
"Collections",
110+
0,
111+
["list", "map"],
112+
[
113+
[1, "ns", "ValueList", 0, value],
114+
[2, "ns", "ValueMap", 0, 0 satisfies StringSchema, value],
115+
],
116+
];
117+
118+
expect(
119+
schemaLogFilter(schema, {
120+
list: [{ public: "visible", sensitive: "secret" }],
121+
map: { key: { public: "visible", sensitive: "secret" } },
122+
})
123+
).toEqual({
124+
list: [{ public: "visible", sensitive: "***SensitiveInformation***" }],
125+
map: { key: { public: "visible", sensitive: "***SensitiveInformation***" } },
126+
});
127+
});
96128
});

β€Žpackages/core/src/submodules/client/smithy-client/schemaLogFilter.tsβ€Ž

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ export function schemaLogFilter(schema: SchemaRef, data: unknown): any {
2020
}
2121

2222
if (ns.isListSchema()) {
23-
const isSensitive = !!ns.getValueSchema().getMergedTraits().sensitive;
24-
if (isSensitive) {
23+
const valueSchema = ns.getValueSchema();
24+
if (valueSchema.getMergedTraits().sensitive) {
2525
return SENSITIVE_STRING;
2626
}
27+
if (Array.isArray(data)) {
28+
return data.map((value) => schemaLogFilter(valueSchema, value));
29+
}
2730
} else if (ns.isMapSchema()) {
28-
const isSensitive =
29-
!!ns.getKeySchema().getMergedTraits().sensitive || !!ns.getValueSchema().getMergedTraits().sensitive;
30-
if (isSensitive) {
31+
const valueSchema = ns.getValueSchema();
32+
if (ns.getKeySchema().getMergedTraits().sensitive || valueSchema.getMergedTraits().sensitive) {
3133
return SENSITIVE_STRING;
3234
}
35+
if (typeof data === "object") {
36+
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key, schemaLogFilter(valueSchema, value)]));
37+
}
3338
} else if (ns.isStructSchema() && typeof data === "object") {
3439
const object = data as Record<string, unknown>;
3540

0 commit comments

Comments
Β (0)