<commit_before>import * as PDFjs from "pdfjs-dist/legacy/build/pdf.js";
import * as XmlCore from "xml-core";
import * as XAdES from "xadesjs";

import { TrustedList } from "../tl";

// TODO: Add signature check
export class AATL {
  public static URL = "https://trustlist.adobe.com/tl12.acrobatsecuritysettings";

  protected url: string;

  constructor({ url = AATL.URL } = {}) {
    this.url = url;
  }

  async extractFile() {
    const doc = await PDFjs.getDocument(this.url).promise;

    const attachments = await doc.getAttachments();
    const attachmentName = "SecuritySettings.xml";
    const attachment = attachments[attachmentName];

    if (!attachment) {
      throw new Error(`Attachment ${attachmentName} not found in the source document`);
    }

    if (!attachment.content) {
      throw new Error(`Attachment ${attachmentName} content is unavailable or invalid`);
    }

    const xml = Buffer.from(attachment.content).toString("utf8");

    return xml;
  }

  async getTrusted(data?: string) {
    if (!data) {
      data = await this.extractFile();
    }

    const parsed = XAdES.Parse(data);

    if (!(XmlCore.isElement(parsed.lastChild))) {
      throw new Error("AATL: Invalid XML structure");
    }

    const settings = SecuritySettings.LoadXml(parsed.lastChild);

    let tl = new TrustedList();

    const trustItems: (keyof (typeof XmlSecuritySettings.TrustTypeMapping))[] = [
      "ROOT",
      "CERTIFIED_DOCUMENTS",
      "DYNAMIC_CONTENT",
      "JAVASCRIPT",
    ];

    for (const identity of settings.TrustedIdentities!.GetIterator()) {
      if (identity.Trust === null || identity.Certificate === null) {
        continue;
      }

      // if someway xml will contain non-AATL sourced items, skip them
      if (identity.Identification!.Source !== "AATL") {
        continue;
      }

      const trust = [];

      for (const trustName of trustItems) {
        const localName = XmlSecuritySettings.TrustTypeMapping[trustName];

        if (identity.Trust[localName] === true) {
          trust.push(trustName);
        }
      }

      const policyArray = identity.PolicyRestrictions?.CertificatePolicies?.GetIterator() || [];
      const evpolicy = policyArray.map(oid => oid.Value!);

      tl.AddCertificate({
        trust,
        evpolicy,
        raw: identity.Certificate,
        source: "AATL"
      });
    }

    return tl;
  }

  async getDisallowed(data?: string) {
    if (!data) {
      data = await this.extractFile();
    }
<commit_msg>chore(AATL): Temporary fix for operator<commit_after>import * as PDFjs from "pdfjs-dist/legacy/build/pdf.js";
import * as XmlCore from "xml-core";
import * as XAdES from "xadesjs";

import { TrustedList } from "../tl";

// TODO: Add signature check
export class AATL {
  public static URL = "https://trustlist.adobe.com/tl12.acrobatsecuritysettings";

  protected url: string;

  constructor({ url = AATL.URL } = {}) {
    this.url = url;
  }

  async extractFile() {
    const doc = await PDFjs.getDocument(this.url).promise;

    const attachments = await doc.getAttachments();
    const attachmentName = "SecuritySettings.xml";
    const attachment = attachments[attachmentName];

    if (!attachment) {
      throw new Error(`Attachment ${attachmentName} not found in the source document`);
    }

    if (!attachment.content) {
      throw new Error(`Attachment ${attachmentName} content is unavailable or invalid`);
    }

    const xml = Buffer.from(attachment.content).toString("utf8");

    return xml;
  }

  async getTrusted(data?: string) {
    if (!data) {
      data = await this.extractFile();
    }

    const parsed = XAdES.Parse(data);

    if (!(XmlCore.isElement(parsed.lastChild))) {
      throw new Error("AATL: Invalid XML structure");
    }

    const settings = SecuritySettings.LoadXml(parsed.lastChild);

    let tl = new TrustedList();

    const trustItems: (keyof (typeof XmlSecuritySettings.TrustTypeMapping))[] = [
      "ROOT",
      "CERTIFIED_DOCUMENTS",
      "DYNAMIC_CONTENT",
      "JAVASCRIPT",
    ];

    for (const identity of settings.TrustedIdentities!.GetIterator()) {
      if (identity.Trust === null || identity.Certificate === null) {
        continue;
      }

      // if someway xml will contain non-AATL sourced items, skip them
      if (identity.Identification!.Source !== "AATL") {
        continue;
      }

      const trust = [];

      for (const trustName of trustItems) {
        const localName = XmlSecuritySettings.TrustTypeMapping[trustName];

        if (identity.Trust[localName] === true) {
          trust.push(trustName);
        }
      }

      const policyArray = identity.PolicyRestrictions?.CertificatePolicies?.GetIterator() || [];
      const evpolicy = policyArray.map(oid => oid.Value!);

      tl.AddCertificate({
        trust,
        evpolicy,
        // TODO: Add operator detection if reasonable
        operator: "Unknown",
        raw: identity.Certificate,
        source: "AATL"
      });
    }

    return tl;
  }

  async getDisallowed(data?: string) {
    if (!data) {
      data = await this.extractFile();
    }

{'fraction_non_alphanumeric': 0.1257098100646172, 'fraction_numerical': 0.0011748580379870766, 'mean_word_length': 2.8119402985074626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3667379', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1486, 'n_words': 372}