Skip to content

Commit

Permalink
patch linkbyid logic
Browse files Browse the repository at this point in the history
  • Loading branch information
clemiller committed Apr 19, 2024
1 parent 4cb4719 commit 904ab39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/src/app/classes/stix/stix-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,15 +520,15 @@ export abstract class StixObject extends Serializable {
let ids = [];
for (let link of links) {
let id = link.split("(LinkById: ")[1].slice(0, -1);
if(!ids.includes(id)) ids.push(id);
if(!ids.includes(id) && id != '') ids.push(id);
}

return restAPIService.getAllObjects({attackIDs: ids, revoked: true, deprecated: true, deserialize: true}).pipe(
map((results: any) => {
// objects must be validated in cases where more than one object is
// returned by the given ATT&CK ID, this occurs due to older versions
// of ATT&CK in which techniques shared their IDs with mitigations
let validObjects = (results.data as StixObject[]).filter(obj => obj.isValidAttackId());
let validObjects = (results.data as StixObject[]).filter(obj => obj.supportsAttackID && obj.isValidAttackId());
let retrieved_ids = validObjects.map(obj => obj.attackID);
for (let id of ids) {
if (!retrieved_ids.includes(id)) result.missingLinks.add(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class SaveDialogComponent implements OnInit {
this.patch_objects.push(x);
}
});
this.patch_objects.push(this.config.object);
this.stage = 2;
},
complete: () => objSubscription.unsubscribe()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class DescriptiveViewComponent implements OnInit {

private reReference = /\(Citation: (.*?)\)/gmu;
private reLinkById = /\(LinkById: (.*?)\)/gmu;
private notFound = '[linked object not found]';
private objectLookup = {};
private sub: Subscription = new Subscription(); // prevent async issues
private parseReferences = true;
Expand Down Expand Up @@ -107,12 +108,14 @@ export class DescriptiveViewComponent implements OnInit {
map((results: any) => {
let data = results.data as StixObject[];
// store retrieved objects in dictionary for quick lookup
data.forEach(obj => {
// objects must be validated in cases where more than one object is
// returned by the given ATT&CK ID, this occurs due to older versions
// of ATT&CK in which techniques shared their IDs with mitigations
if (obj.isValidAttackId()) this.objectLookup[obj.attackID] = obj;
});
if (data?.length > 0) {
data.forEach(obj => {
// objects must be validated in cases where more than one object is
// returned by the given ATT&CK ID, this occurs due to older versions
// of ATT&CK in which techniques shared their IDs with mitigations
if (obj.isValidAttackId()) this.objectLookup[obj.attackID] = obj;
});
}
return results;
})
);
Expand All @@ -124,12 +127,19 @@ export class DescriptiveViewComponent implements OnInit {
private replaceLinkByIds(displayStr: string, linkedIDs: string[]): string {
for (let id of linkedIDs) {
let obj = this.objectLookup[id];
if (obj?.name) {
let rep = `(LinkById: ${obj.attackID})`;
let target = this.config.mode == 'edit' ? ` target="_blank"` : ``; // open linked object in new tab when editing
let linkHTML = `<span><a href="${obj.attackType}/${obj.stixID}"${target}>${obj.name}</a></span>`;
displayStr = displayStr.replace(rep, linkHTML);
}

let rep = `(LinkById: ${id})`;

let linkHTML;
if (obj?.name) {
let url = `${obj.attackType}/${obj.stixID}`;
let target = this.config.mode == 'edit' ? ` target="_blank"` : ``; // open linked object in new tab when editing
linkHTML = `<a href="${url}"${target}>${obj.name}</a>`
} else {
linkHTML = this.notFound;
}
let newStr = `<span>${linkHTML}</span>`;
displayStr = displayStr.replace(rep, newStr);
}
return displayStr;
}
Expand Down Expand Up @@ -168,6 +178,7 @@ export class DescriptiveViewComponent implements OnInit {

// Check for LinkById tags
let linkedIDs = this.getLinkedIds(this.preview);
linkedIDs = linkedIDs.filter(id => id != '');
if (!linkedIDs) {
this.loading = false;
return;
Expand All @@ -180,13 +191,13 @@ export class DescriptiveViewComponent implements OnInit {
this.loading = false;
} else {
let missing = linkedIDs.filter(id => Object.keys(this.objectLookup).indexOf(id) < 0);
this.sub = this.loadLinkedObjects(missing).subscribe({
next: (results: any) => {
this.preview = this.replaceLinkByIds(this.preview, linkedIDs);
this.loading = false;
},
complete: () => { this.sub.unsubscribe(); }
})
this.sub = this.loadLinkedObjects(missing).subscribe({
next: (results: any) => {
this.preview = this.replaceLinkByIds(this.preview, linkedIDs);
this.loading = false;
},
complete: () => { this.sub.unsubscribe(); }
});
}
}
}
Expand Down

0 comments on commit 904ab39

Please sign in to comment.