Skip to content

Commit 5d1abaa

Browse files
FrancescoMolinaroatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2609 (pull request DSpace#3894)
[DSC-2609] sanitize schema for structured data Approved-by: Giuseppe Digilio
2 parents 3ef459b + a26ceca commit 5d1abaa

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/app/core/metadata/schema-json-ld/schema-json-ld.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { Inject, Injectable } from '@angular/core';
22
import { DOCUMENT } from '@angular/common';
3+
import { DomSanitizer } from '@angular/platform-browser';
34

45
import { Item } from '../../shared/item.model';
56
import { getSchemaJsonLDProviderByEntity, getSchemaJsonLDProviderByType } from './schema-types/schema-type-decorator';
67
import { GenericConstructor } from '../../shared/generic-constructor';
78
import { SchemaType } from './schema-types/schema-type';
89
import { isEmpty, isNotEmpty } from '../../../shared/empty.util';
910

10-
@Injectable()
11+
@Injectable({ providedIn: 'root' })
1112
export class SchemaJsonLDService {
1213
static scriptType = 'application/ld+json';
1314

14-
constructor(@Inject(DOCUMENT) private _document: Document) {}
15+
constructor(
16+
@Inject(DOCUMENT) private _document: Document,
17+
protected sanitizer: DomSanitizer,
18+
) {}
1519

1620
removeStructuredData(): void {
1721
const els = [];
@@ -57,7 +61,7 @@ export class SchemaJsonLDService {
5761
}
5862

5963
if (isNotEmpty(constructor)) {
60-
const provider: SchemaType = new constructor();
64+
const provider: SchemaType = new constructor(this.sanitizer);
6165
return provider.getSchema(item);
6266
} else {
6367
return null;

src/app/core/metadata/schema-json-ld/schema-types/schema-type.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { SecurityContext } from '@angular/core';
2+
import { DomSanitizer } from '@angular/platform-browser';
13
import isObject from 'lodash/isObject';
24

35
import { Item } from '../../../shared/item.model';
46
import { isNotEmpty } from '../../../../shared/empty.util';
57

68
export abstract class SchemaType {
9+
constructor(protected sanitizer: DomSanitizer) {}
10+
711
protected abstract createSchema(item: Item): Record<string, any>;
812
protected abstract createSchema(item: Item): Record<string, any>;
913

@@ -31,7 +35,36 @@ export abstract class SchemaType {
3135
}
3236
}
3337

38+
protected sanitizeSchema(obj: any): Record<string, any> {
39+
if (Array.isArray(obj)) {
40+
return obj.map(v =>
41+
typeof v === 'string'
42+
? this.sanitizer.sanitize(SecurityContext.HTML, v)
43+
: this.sanitizeSchema(v),
44+
);
45+
}
46+
47+
if (typeof obj === 'object' && obj !== null) {
48+
const sanitized: Record<string, any> = {};
49+
for (const key in obj) {
50+
if (obj.hasOwnProperty(key)) {
51+
const value = obj[key];
52+
sanitized[key] =
53+
typeof value === 'string'
54+
? this.sanitizer.sanitize(SecurityContext.HTML, value)
55+
: this.sanitizeSchema(value);
56+
}
57+
}
58+
return sanitized;
59+
}
60+
61+
return obj;
62+
}
63+
64+
3465
getSchema(item: Item): Record<string, any> {
3566
return SchemaType.removeEmpty(this.createSchema(item));
67+
const sanitizedRaw = this.sanitizeSchema(this.createSchema(item));
68+
return SchemaType.removeEmpty(sanitizedRaw);
3669
}
3770
}

0 commit comments

Comments
 (0)