keanu-weblite/src/models/proof.ts

438 lines
15 KiB
TypeScript
Raw Normal View History

2025-10-22 15:03:56 +02:00
import utils from "@/plugins/utils";
import dayjs from "dayjs";
export type AIInferenceResult = {
aiGenerated: boolean;
aiProbability: number;
humanProbability: number;
2025-08-28 10:27:57 +02:00
};
2025-07-11 14:36:58 +02:00
export const C2PASourceTypeScreenCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/screenCapture";
export const C2PASourceTypeDigitalCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture";
export const C2PASourceTypeComputationalCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/computationalCapture";
export const C2PASourceTypeCompositeCapture = "http://cv.iptc.org/newscodes/digitalsourcetype/compositeCapture";
2025-08-28 10:27:57 +02:00
export const C2PASourceTypeTrainedAlgorithmicMedia =
"http://cv.iptc.org/newscodes/digitalsourcetype/trainedAlgorithmicMedia";
export const C2PASourceTypeCompositeWithTrainedAlgorithmicMedia =
"http://cv.iptc.org/newscodes/digitalsourcetype/compositeWithTrainedAlgorithmicMedia";
2025-07-11 14:36:58 +02:00
export type C2PAActionsAssertion = {
2025-08-28 10:27:57 +02:00
actions: {
action: string;
softwareAgent?: string;
digitalSourceType?: string;
}[];
};
export type C2PAAssertion = {
2025-08-28 10:27:57 +02:00
label: string;
data: C2PAActionsAssertion | undefined;
};
export type C2PAManifest = {
2025-08-28 10:27:57 +02:00
assertions: C2PAAssertion[];
signature_info: {
time: string;
};
};
2025-07-11 14:36:58 +02:00
export type C2PAValidationResults = {
2025-08-28 10:27:57 +02:00
activeManifest?: {
failure: any[];
success: any[];
informational: any[];
};
};
2025-07-11 14:36:58 +02:00
export type C2PAManifestInfo = {
2025-08-28 10:27:57 +02:00
active_manifest: string;
manifests: { [key: string]: C2PAManifest };
validation_results?: C2PAValidationResults;
};
export type C2PAData = {
2025-08-28 10:27:57 +02:00
manifest_info: C2PAManifestInfo;
};
export type Proof = {
2025-08-28 10:27:57 +02:00
data?: any;
name?: string;
json?: string;
integrity?: { pgp?: any; c2pa?: C2PAData; exif?: { [key: string]: string | Object }; opentimestamps?: any };
ai?: { inferenceResult?: AIInferenceResult };
};
2025-08-20 15:12:04 +02:00
2025-09-09 10:56:15 +02:00
export type ProofHintFlagsGenerator = "unknown" | "camera" | "screenshot" | "ai";
2025-09-23 17:17:26 +02:00
export type ProofHintFlagSource = "c2pa" | "exif" | "metadata";
2025-09-09 10:56:15 +02:00
export type ProofHintFlagsEdit = {
editor: string;
2025-09-09 10:56:15 +02:00
date?: Date;
}
2025-08-20 15:12:04 +02:00
export type ProofHintFlags = {
2025-09-09 10:56:15 +02:00
device?: string;
creationDate?: Date;
2025-09-23 17:17:26 +02:00
creationDateSource?: ProofHintFlagSource;
2025-09-09 10:56:15 +02:00
generator?: ProofHintFlagsGenerator;
2025-09-23 17:17:26 +02:00
generatorSource?: ProofHintFlagSource;
2025-09-09 10:56:15 +02:00
edits?: ProofHintFlagsEdit[];
2025-09-23 16:10:28 +02:00
containsC2PA?: boolean;
containsEXIF?: boolean;
2025-08-28 10:27:57 +02:00
};
2025-08-20 15:12:04 +02:00
2025-08-28 10:27:57 +02:00
type FlagMatchRule = {
field: string;
match: string[];
description: string;
};
2025-08-20 15:12:04 +02:00
type FlagMatchRulePathSegment = {
object: any;
2025-09-01 16:26:05 +02:00
path: string;
}
type FlagMatchRuleValue = {
path: FlagMatchRulePathSegment[];
value: string | object;
2025-09-01 16:26:05 +02:00
};
2025-08-28 10:27:57 +02:00
type FlagMatchInfo = {
field: string;
value: string;
re: string;
};
2025-10-22 15:03:56 +02:00
type FlagValue = {
path: string;
transform?: (value: any, match: FlagMatchRuleValue) => any;
}
const pathsC2PASource = (): FlagValue[] => {
return [
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=stds.exif]/data/exif:Make", transform: getExifMakeModel },
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=stds.exif]/data/exif:Model", transform: getExifMakeModel },
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/softwareAgent/name"},
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/softwareAgent"},
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/../../../claim_generator"}
];
};
const pathsC2PACreationDate = (): FlagValue[] => {
return [
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/when"},
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/../../metadata/dateTime"},
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/../../../signature_info/time"},
{path: "integrity/c2pa/manifest_info/manifests[]/assertions[label=stds.exif]/data/exif:DateTimeOriginal", transform: (value, match) => {
// Check if we have a timestamp offset
let offset = extractFlagValues("../exif:OffsetTimeOriginal", match.path)?.at(0)?.value;
return dayjs(Date.parse(value + (offset ? offset : ""))).format("lll");
}}
];
}
const getExifValue = (val: string): string => {
return val.replace(/^"(.+(?="$))"$/, "$1");
};
const pathsExifCreationDate = (): FlagValue[] => {
return [
{path: "integrity/exif/DateTimeOriginal", transform: (value, match) => {
let dateTimeOriginal = getExifValue(value);
// Check if we have a timestamp offset
let offset = extractFlagValues("../OffsetTimeOriginal", match.path)?.at(0)?.value as string;
return dayjs(Date.parse(dateTimeOriginal + (offset ? getExifValue(offset) : ""))).format("lll");
}}
];
}
const getExifMakeModel = (ignoredvalue: any, match: FlagMatchRuleValue): string => {
// Make and model
let makeAndModel = "";
const make = extractFlagValues("../exif:Make", match.path)?.at(0)?.value as string;
const model = extractFlagValues("../exif:Model", match.path)?.at(0)?.value as string;
if (make) {
makeAndModel += make;
}
if (model) {
if (makeAndModel.length > 0) {
makeAndModel += ", ";
}
makeAndModel += model;
}
return makeAndModel;
}
2025-09-09 10:56:15 +02:00
const ruleScreenshotMeta = (): FlagMatchRule[] => {
return [
{
field:
"name",
match: ["screenshot"],
description: "Screen capture",
},
];
};
2025-09-09 11:34:48 +02:00
const ruleAiMeta = (): FlagMatchRule[] => {
2025-08-28 10:27:57 +02:00
const knownAIServices = [
"ChatGPT",
"OpenAI-API",
"Adobe Firefly",
"RunwayML",
"Runway AI",
"Google AI",
"Stable Diffusion",
];
return [
{ field: "name", match: ["^DALL_E_", "^Gen-3"], description: "File name" },
{
field: "integrity/c2pa/manifest_info/manifests[]/claim_generator",
match: knownAIServices,
description: "C2PA claim generator",
},
{ field: "iptc/Credit", match: knownAIServices, description: "IPTC Credit" },
{ field: "iptc/Provider", match: knownAIServices, description: "IPTC Provider" },
{ field: "iptc/ImageSupplier[]", match: knownAIServices, description: "IPTC ImageSupplier" },
{ field: "iptc/ImageCreator[]", match: knownAIServices, description: "IPTC ImageCreator" },
];
};
const matchFlag = (rules: FlagMatchRule[], path: FlagMatchRulePathSegment[]) => {
2025-08-28 10:27:57 +02:00
let result = false;
let resultInfo: FlagMatchInfo[] = [];
for (let rule of rules) {
try {
const re: RegExp[] = (!Array.isArray(rule.match) ? [rule.match] : rule.match).map((m) => new RegExp(m, "gi"));
const values = extractFlagValues(rule.field, path);
2025-09-04 14:20:45 +02:00
values.forEach((v) => {
re.forEach((r) => {
if (typeof v.value === "string" && r.test(v.value)) {
result = true;
resultInfo.push({ field: v.path.map((v) => v.path).join("/"), value: v.value, re: r.source });
2025-09-04 14:20:45 +02:00
}
2025-08-28 10:27:57 +02:00
});
});
2025-08-28 10:27:57 +02:00
} catch (e) {
console.error("Invalid RE", e);
}
}
return { result: result, matches: resultInfo };
};
const extractFlagValues = (flagPath: string, path: FlagMatchRulePathSegment[]): FlagMatchRuleValue[] => {
const getValues = (
keys: string[],
path: FlagMatchRulePathSegment[]
): FlagMatchRuleValue[] | undefined => {
if (keys.length == 0 || path.length == 0) return undefined;
const o = path[0].object;
let key = keys[0];
const lastBracket = key.lastIndexOf("[");
if (key === "..") {
return getValues(keys.slice(1), path.slice(1));
2025-09-01 16:26:05 +02:00
}
if (key.endsWith("]") && lastBracket > 0) {
const optionalConstraint = key.substring(lastBracket + 1, key.length - 1);
key = key.substring(0, lastBracket);
if (o[key] != undefined) {
let opart: any[] = o[key];
2025-09-01 16:26:05 +02:00
if (!Array.isArray(opart)) {
opart = Object.values(opart) ?? [];
}
// Any constraints controlling what array object(s) to consider?
if (optionalConstraint) {
if (optionalConstraint.startsWith("!!")) {
// Ignore this path in the tree if ANY of the values contain the constraint match.
const [prop, val] = optionalConstraint.substring(2).split("=");
if (opart.some((item) => item[prop] === val)) {
return undefined;
}
} else if (optionalConstraint.startsWith("!")) {
const [prop, val] = optionalConstraint.substring(1).split("=");
opart = opart.filter((item) => item[prop] !== val);
} else {
const [prop, val] = optionalConstraint.split("=");
2025-10-22 15:03:56 +02:00
let valarray = val.split("|");
opart = opart.filter((item) => valarray.includes(item[prop]));
}
2025-09-01 16:26:05 +02:00
}
if (keys.length == 1) {
return opart.map((s, i) => {
return { value: o, path: [{ object: s, path: key + "[" + i + "]"}, ... path] };
});
2025-09-01 16:26:05 +02:00
} else {
return opart.reduce((res: FlagMatchRuleValue[] | undefined, oin: any, i: number) => {
let matches = getValues(keys.slice(1), [ { object: oin, path: key + "[" + i + "]"}, ... path]);
2025-09-01 16:26:05 +02:00
if (matches) {
const r2 = res || [];
r2.push(...matches);
return r2;
}
return res;
}, undefined);
}
} else {
return undefined;
}
} else {
if (o[key] != undefined) {
if (keys.length == 1) {
return [{ value: o[key], path: [{object: o[key], path: key}, ...path] }];
2025-09-01 16:26:05 +02:00
} else {
return getValues(keys.slice(1), [{object: o[key], path: key}, ...path]);
2025-09-01 16:26:05 +02:00
}
} else {
return undefined;
}
}
};
let result: FlagMatchRuleValue[] = [];
try {
let keys = flagPath.split("/");
result = getValues(keys, path) ?? [];
2025-09-01 16:26:05 +02:00
} catch (e) {
console.error("Invalid RE", e);
}
return result;
};
2025-10-22 15:03:56 +02:00
const getFirstWithData = (flagValues: FlagValue[], path: FlagMatchRulePathSegment[]): string | undefined => {
let first: FlagMatchRuleValue | undefined = undefined;
let transform: ((value: any, match: FlagMatchRuleValue) => any) | undefined = undefined;
for (let idx = 0; idx < flagValues.length; idx++) {
const result = extractFlagValues(flagValues[idx].path, path);
if (result.length > 0) {
2025-10-22 15:03:56 +02:00
first = result[0]
transform = flagValues[idx].transform;
break;
}
}
if (first && first.value as string) {
try {
let val = first.value as string;
if (val && transform) {
val = transform(val, first);
}
return val;
} catch (error) {
}
}
return undefined;
};
2025-10-22 15:03:56 +02:00
const getFirstWithDataAsDate = (flagValues: FlagValue[], path: FlagMatchRulePathSegment[]): Date | undefined => {
let val = getFirstWithData(flagValues, path);
if (val) {
try {
2025-10-22 15:03:56 +02:00
let date = new Date(Date.parse(val));
if (isNaN(date.valueOf())) {
// Try EXIF format
date = utils.parseExifDate(val);
}
return date;
2025-10-22 15:03:56 +02:00
} catch (error) {
}
}
return undefined;
};
2025-08-28 10:27:57 +02:00
export const extractProofHintFlags = (proof?: Proof): ProofHintFlags | undefined => {
if (!proof) return undefined;
2025-09-09 10:56:15 +02:00
let edits: ProofHintFlagsEdit[] | undefined = undefined;
2025-08-28 10:27:57 +02:00
let valid = false;
try {
let results = proof.integrity?.c2pa?.manifest_info.validation_results?.activeManifest;
if (results) {
valid = results.failure.length == 0 && results.success.length > 0;
}
const rootMatchPath = [{object: proof, path: ""}];
let source: string | undefined = undefined;
let dateCreated: Date | undefined = undefined;
2025-09-23 17:17:26 +02:00
let dateCreatedSource: ProofHintFlagSource | undefined = undefined;
2025-10-22 15:03:56 +02:00
source = getFirstWithData(pathsC2PASource(), rootMatchPath);
dateCreated = getFirstWithDataAsDate(pathsC2PACreationDate(), rootMatchPath);
if (dateCreated) {
2025-09-23 17:17:26 +02:00
dateCreatedSource = "c2pa";
2025-10-22 15:03:56 +02:00
} else {
dateCreated = getFirstWithDataAsDate(pathsExifCreationDate(), rootMatchPath);
if (dateCreated) {
dateCreatedSource = "exif";
}
2025-08-28 10:27:57 +02:00
}
2025-10-22 15:27:43 +02:00
let generator: ProofHintFlagsGenerator = "unknown";
2025-09-23 17:17:26 +02:00
let generatorSource: ProofHintFlagSource | undefined = undefined;
2025-09-09 10:56:15 +02:00
2025-10-22 15:27:43 +02:00
let digitalSourceType = extractFlagValues("integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[action=c2pa.created]/digitalSourceType", rootMatchPath)?.at(0)?.value as string;
if ([C2PASourceTypeScreenCapture].includes(digitalSourceType)) {
generator = "screenshot";
2025-09-09 10:56:15 +02:00
generatorSource = "c2pa";
2025-10-22 15:27:43 +02:00
} else if ([C2PASourceTypeDigitalCapture, C2PASourceTypeComputationalCapture, C2PASourceTypeCompositeCapture].includes(digitalSourceType)) {
generator = "camera";
generatorSource = "c2pa";
} else if ([C2PASourceTypeTrainedAlgorithmicMedia, C2PASourceTypeCompositeWithTrainedAlgorithmicMedia].includes(digitalSourceType)) {
generator = "ai";
generatorSource = "c2pa";
} else if (matchFlag(ruleScreenshotMeta(), rootMatchPath).result) {
generator = "screenshot";
generatorSource = "metadata";
} else if (matchFlag(ruleAiMeta(), rootMatchPath).result) {
generator = "ai";
generatorSource = "metadata";
2025-09-09 10:56:15 +02:00
}
console.error("PROOF", proof);
const c2paEdits = extractFlagValues(
2025-10-22 15:27:43 +02:00
"integrity/c2pa/manifest_info/manifests[]/assertions[label=c2pa.actions|c2pa.actions.v2]/data/actions[!!action=c2pa.created]",
rootMatchPath
);
if (c2paEdits.length > 0) {
edits = c2paEdits.map((edit) => {
return {
2025-10-22 15:03:56 +02:00
editor: getFirstWithData([
{path: "softwareAgent/name"},
{path: "softwareAgent"},
{path: "../../../claim_generator"}
], edit.path) ?? "",
date: getFirstWithDataAsDate([
{path: "when"},
{path: "../../metadata/dateTime"},
{path: "../../../signature_info/time"}
], edit.path)
}
});
}
2025-09-09 11:34:48 +02:00
// Do we have any data? Else, return "undefined", we don't just want to send an object with all defaults.
2025-09-23 16:10:28 +02:00
if (source === undefined && dateCreated === undefined && generator === "unknown" && (!edits || edits.length == 0)) {
2025-09-09 11:34:48 +02:00
return undefined;
}
const flags: ProofHintFlags = {
device: source,
creationDate: dateCreated,
2025-09-23 17:17:26 +02:00
creationDateSource: dateCreatedSource,
2025-09-09 10:56:15 +02:00
generator: generator,
generatorSource: generatorSource,
edits: edits,
2025-09-23 16:10:28 +02:00
containsC2PA: proof.integrity?.c2pa !== undefined,
containsEXIF: proof.integrity?.exif !== undefined
};
return flags;
2025-08-28 10:27:57 +02:00
} catch (error) {}
return undefined;
};