@semacode/cli 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -4
- package/dist/agentContext.d.ts +1 -1
- package/dist/agentContextPack.js +6 -1
- package/dist/agentContextTipos.d.ts +4 -2
- package/dist/agentContextTipos.js +7 -0
- package/dist/agentEntryPoints.js +70 -19
- package/dist/comandos.js +6 -0
- package/dist/discovery/catalog.d.ts +11 -0
- package/dist/discovery/catalog.js +521 -0
- package/dist/discovery/command.d.ts +11 -0
- package/dist/discovery/command.js +226 -0
- package/dist/discovery/index.d.ts +5 -0
- package/dist/discovery/index.js +8 -0
- package/dist/discovery/ranker.d.ts +3 -0
- package/dist/discovery/ranker.js +310 -0
- package/dist/discovery/redaction.d.ts +2 -0
- package/dist/discovery/redaction.js +21 -0
- package/dist/discovery/render.d.ts +2 -0
- package/dist/discovery/render.js +100 -0
- package/dist/discovery/types.d.ts +119 -0
- package/dist/discovery/types.js +13 -0
- package/dist/execucoesExternas.js +19 -5
- package/dist/exemplosOficiais.js +29 -8
- package/dist/iaPrompts.d.ts +1 -1
- package/dist/iaPrompts.js +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +24 -5
- package/dist/index.part01.d.ts +1 -1
- package/dist/index.part01.js +47 -3
- package/dist/index.part06.js +3 -0
- package/dist/initTemplatesBase.js +3 -2
- package/dist/pipelineConteudo/adapters.d.ts +84 -0
- package/dist/pipelineConteudo/adapters.js +449 -0
- package/dist/pipelineConteudo/canonical.d.ts +18 -0
- package/dist/pipelineConteudo/canonical.js +107 -0
- package/dist/pipelineConteudo/command.d.ts +3 -0
- package/dist/pipelineConteudo/command.js +572 -0
- package/dist/pipelineConteudo/index.d.ts +9 -0
- package/dist/pipelineConteudo/index.js +12 -0
- package/dist/pipelineConteudo/ledger.d.ts +68 -0
- package/dist/pipelineConteudo/ledger.js +732 -0
- package/dist/pipelineConteudo/planner.d.ts +12 -0
- package/dist/pipelineConteudo/planner.js +366 -0
- package/dist/pipelineConteudo/projection.d.ts +16 -0
- package/dist/pipelineConteudo/projection.js +57 -0
- package/dist/pipelineConteudo/state.d.ts +23 -0
- package/dist/pipelineConteudo/state.js +615 -0
- package/dist/pipelineConteudo/trust.d.ts +17 -0
- package/dist/pipelineConteudo/trust.js +750 -0
- package/dist/pipelineConteudo/types.d.ts +331 -0
- package/dist/pipelineConteudo/types.js +4 -0
- package/dist/profileArtefatoValidadores.d.ts +3 -2
- package/dist/profileArtefatoValidadores.js +81 -0
- package/dist/profileAuthorTipos.d.ts +4 -3
- package/dist/profileCatalogo.d.ts +5 -5
- package/dist/profileCatalogo.js +30 -0
- package/dist/profileCommand.d.ts +8 -2
- package/dist/profileCommand.js +15 -5
- package/dist/profileRegras.d.ts +5 -5
- package/dist/profileRegras.js +13 -1
- package/dist/profileRulePacks.js +68 -0
- package/dist/sistemasInterativos/adapterProtocol.d.ts +3 -0
- package/dist/sistemasInterativos/adapterProtocol.js +240 -0
- package/dist/sistemasInterativos/advancedCatalog.d.ts +5 -0
- package/dist/sistemasInterativos/advancedCatalog.js +442 -0
- package/dist/sistemasInterativos/autonomy.d.ts +230 -0
- package/dist/sistemasInterativos/autonomy.js +753 -0
- package/dist/sistemasInterativos/canonical.d.ts +3 -0
- package/dist/sistemasInterativos/canonical.js +43 -0
- package/dist/sistemasInterativos/catalog.d.ts +116 -0
- package/dist/sistemasInterativos/catalog.js +543 -0
- package/dist/sistemasInterativos/command.d.ts +7 -0
- package/dist/sistemasInterativos/command.js +311 -0
- package/dist/sistemasInterativos/controlRun.d.ts +56 -0
- package/dist/sistemasInterativos/controlRun.js +391 -0
- package/dist/sistemasInterativos/evidence.d.ts +5 -0
- package/dist/sistemasInterativos/evidence.js +306 -0
- package/dist/sistemasInterativos/experienceIr.d.ts +12 -0
- package/dist/sistemasInterativos/experienceIr.js +895 -0
- package/dist/sistemasInterativos/experienceIrSemantics.d.ts +30 -0
- package/dist/sistemasInterativos/experienceIrSemantics.js +368 -0
- package/dist/sistemasInterativos/experienceIrTypes.d.ts +370 -0
- package/dist/sistemasInterativos/experienceIrTypes.js +5 -0
- package/dist/sistemasInterativos/extensionCommand.d.ts +391 -0
- package/dist/sistemasInterativos/extensionCommand.js +383 -0
- package/dist/sistemasInterativos/index.d.ts +19 -0
- package/dist/sistemasInterativos/index.js +22 -0
- package/dist/sistemasInterativos/operations.d.ts +40 -0
- package/dist/sistemasInterativos/operations.js +1167 -0
- package/dist/sistemasInterativos/operationsTypes.d.ts +297 -0
- package/dist/sistemasInterativos/operationsTypes.js +11 -0
- package/dist/sistemasInterativos/planner.d.ts +2 -0
- package/dist/sistemasInterativos/planner.js +159 -0
- package/dist/sistemasInterativos/portability.d.ts +164 -0
- package/dist/sistemasInterativos/portability.js +539 -0
- package/dist/sistemasInterativos/render.d.ts +3 -0
- package/dist/sistemasInterativos/render.js +179 -0
- package/dist/sistemasInterativos/temporal.d.ts +183 -0
- package/dist/sistemasInterativos/temporal.js +543 -0
- package/dist/sistemasInterativos/types.d.ts +215 -0
- package/dist/sistemasInterativos/types.js +4 -0
- package/dist/sistemasInterativos/validator.d.ts +2 -0
- package/dist/sistemasInterativos/validator.js +305 -0
- package/docs/README.md +9 -0
- package/docs/ai-integration.md +7 -1
- package/docs/ai-workflow.md +5 -4
- package/docs/commands.md +43 -1
- package/docs/descoberta-capacidades.md +106 -0
- package/docs/documentation.md +11 -2
- package/docs/pipeline-conteudo.md +246 -0
- package/docs/profiles.md +11 -2
- package/docs/sistemas-interativos.md +381 -0
- package/docs/testing.md +11 -2
- package/exemplos/pipeline-conteudo/README.md +44 -0
- package/exemplos/pipeline-conteudo/alvos.json +23 -0
- package/exemplos/pipeline-conteudo/definicao.json +136 -0
- package/exemplos/profile_simulation.sema +211 -0
- package/exemplos/sistemas-interativos/README.md +100 -0
- package/exemplos/sistemas-interativos/advanced/acceptance-context-evaluate-valid.json +5 -0
- package/exemplos/sistemas-interativos/advanced/acceptance-lock-valid.json +16 -0
- package/exemplos/sistemas-interativos/advanced/asset-provenance-valid.json +32 -0
- package/exemplos/sistemas-interativos/advanced/autonomy-repair-valid.json +93 -0
- package/exemplos/sistemas-interativos/advanced/control-run-definition-valid.json +78 -0
- package/exemplos/sistemas-interativos/advanced/control-run-valid.json +27 -0
- package/exemplos/sistemas-interativos/advanced/distributed-workers-valid.json +79 -0
- package/exemplos/sistemas-interativos/advanced/editor-state-valid.json +42 -0
- package/exemplos/sistemas-interativos/advanced/engine-snapshot-after-valid.json +32 -0
- package/exemplos/sistemas-interativos/advanced/engine-snapshot-before-valid.json +23 -0
- package/exemplos/sistemas-interativos/advanced/job-orchestration-valid.json +39 -0
- package/exemplos/sistemas-interativos/advanced/multimodal-evidence-valid.json +46 -0
- package/exemplos/sistemas-interativos/advanced/multiplayer-authority-valid.json +39 -0
- package/exemplos/sistemas-interativos/advanced/playtest-fuzz-valid.json +37 -0
- package/exemplos/sistemas-interativos/advanced/portability-valid.json +104 -0
- package/exemplos/sistemas-interativos/advanced/temporal-evidence-valid.json +27 -0
- package/exemplos/sistemas-interativos/advanced/temporal-valid.json +118 -0
- package/exemplos/sistemas-interativos/experience-ir-valid.json +459 -0
- package/exemplos/sistemas-interativos/game-2d.json +83 -0
- package/exemplos/sistemas-interativos/game-3d-human.json +81 -0
- package/exemplos/sistemas-interativos/game-pixel-16-bit.json +97 -0
- package/exemplos/sistemas-interativos/game-pixel-8-bit.json +94 -0
- package/exemplos/sistemas-interativos/game-xr-human.json +80 -0
- package/exemplos/sistemas-interativos/hybrid-2-5d.json +108 -0
- package/exemplos/sistemas-interativos/protocol-mutating-rollback-valid.json +75 -0
- package/exemplos/sistemas-interativos/protocol-read-only-valid.json +58 -0
- package/exemplos/sistemas-interativos/simulation-2d-controlled.json +97 -0
- package/exemplos/sistemas-interativos/simulation-3d-calibrated-autonomous.json +141 -0
- package/exemplos/sistemas-interativos/simulation-headless-autonomous-batch.json +112 -0
- package/exemplos/sistemas-interativos/simulation-pixel-16-bit.json +103 -0
- package/exemplos/sistemas-interativos/simulation-text-controlled.json +95 -0
- package/node_modules/@sema/gerador-lua/dist/index.js +78 -4
- package/node_modules/@sema/gerador-lua/dist/index.js.map +1 -1
- package/node_modules/@sema/gerador-php/dist/index.js +8 -0
- package/node_modules/@sema/gerador-php/dist/index.js.map +1 -1
- package/package.json +8 -1
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
// SEMA-GOVERNED: sema.produto.pipeline_conteudo.ledger
|
|
2
|
+
// Descricao: ledger hash-chained verificavel, com payloads contextuais estritos e replay historico.
|
|
3
|
+
import { hashCanonicoConteudo } from "./canonical.js";
|
|
4
|
+
import { escopoAutorizacaoAlvo, escopoAutorizacaoGlobal, validarMetadataPublicaConteudo, validarReferenciaAccountScopeConteudo, } from "./adapters.js";
|
|
5
|
+
import { validarConfiguracaoConfiancaConteudo, verificarEnvelopeAssinadoConteudo, verificarEnvelopeAssinadoConteudoHistoricoInterno, } from "./trust.js";
|
|
6
|
+
export const HEAD_GENESIS_LEDGER_CONTEUDO = `sha256:${"0".repeat(64)}`;
|
|
7
|
+
const KINDS_EVENTO = new Set([
|
|
8
|
+
"RUN_STARTED",
|
|
9
|
+
"OPERATIONAL_CONDITION",
|
|
10
|
+
"ARTIFACT_REGISTERED",
|
|
11
|
+
"EVIDENCE_CLAIMED",
|
|
12
|
+
"EVIDENCE_ATTESTED",
|
|
13
|
+
"AI_ASSESSMENT",
|
|
14
|
+
]);
|
|
15
|
+
const CAMPOS_BASE = ["kind", "runId", "trustDomainId", "trustRootDigest", "ledgerId", "policyDigest", "definitionDigest"];
|
|
16
|
+
const CAMPOS_AUTORIZADOS = [...CAMPOS_BASE, "authorizationScope"];
|
|
17
|
+
const CAMPOS_POR_KIND = {
|
|
18
|
+
RUN_STARTED: [...CAMPOS_BASE, "targets", "startedAt"],
|
|
19
|
+
OPERATIONAL_CONDITION: [...CAMPOS_AUTORIZADOS, "gateId", "targetId", "condition", "reason", "reportedAt"],
|
|
20
|
+
ARTIFACT_REGISTERED: [
|
|
21
|
+
...CAMPOS_AUTORIZADOS,
|
|
22
|
+
"artifactId", "stageId", "targetId", "producerId", "producerLineageIds", "version",
|
|
23
|
+
"artifactType", "mediaType", "digest", "lineageDigests",
|
|
24
|
+
],
|
|
25
|
+
EVIDENCE_CLAIMED: [
|
|
26
|
+
...CAMPOS_AUTORIZADOS,
|
|
27
|
+
"claimId", "stageId", "targetId", "artifactDigest", "evidenceType", "producerId", "claimedAt", "data",
|
|
28
|
+
],
|
|
29
|
+
EVIDENCE_ATTESTED: [
|
|
30
|
+
...CAMPOS_AUTORIZADOS,
|
|
31
|
+
"evidenceId", "claimId", "gateId", "stageId", "targetId", "artifactDigest", "evidenceType",
|
|
32
|
+
"producerId", "attesterId", "result", "observedAt", "data",
|
|
33
|
+
],
|
|
34
|
+
AI_ASSESSMENT: [
|
|
35
|
+
...CAMPOS_AUTORIZADOS,
|
|
36
|
+
"assessmentId", "gateId", "targetId", "artifactDigest", "rubricDigest", "evaluatorId",
|
|
37
|
+
"capability", "result", "assessedAt", "rationaleDigest", "data",
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
const CONDICOES_OPERACIONAIS = new Set([
|
|
41
|
+
"PENDENTE", "PRONTA", "EXECUTANDO", "AGUARDANDO_EVIDENCIA", "AGUARDANDO_EVENTO_EXTERNO",
|
|
42
|
+
"FERRAMENTA_INDISPONIVEL", "CAPACIDADE_AUSENTE", "AUTORIZACAO_AUSENTE", "FALHA_TRANSITORIA",
|
|
43
|
+
"FALHA_TERMINAL", "EXECUCAO_ENCERRADA",
|
|
44
|
+
]);
|
|
45
|
+
const RESULTADOS = new Set(["APROVADO", "REPROVADO", "INCONCLUSIVO"]);
|
|
46
|
+
const CAMPOS_EVENTO_LEDGER = [
|
|
47
|
+
"schemaVersion", "ledgerId", "sequence", "eventId", "recordedAt", "previousHash", "envelope", "hash",
|
|
48
|
+
];
|
|
49
|
+
const IDADE_MAXIMA_APPEND_PADRAO_MS = 5 * 60 * 1000;
|
|
50
|
+
const DESVIO_FUTURO_APPEND_PADRAO_MS = 30 * 1000;
|
|
51
|
+
function objeto(valor) {
|
|
52
|
+
return valor !== null && typeof valor === "object" && !Array.isArray(valor);
|
|
53
|
+
}
|
|
54
|
+
function texto(valor) {
|
|
55
|
+
return typeof valor === "string" && valor.length > 0 && valor === valor.trim() && !/[\u0000-\u001f\u007f]/u.test(valor);
|
|
56
|
+
}
|
|
57
|
+
function instante(valor) {
|
|
58
|
+
return typeof valor === "string" && valor.length > 0 && Number.isFinite(Date.parse(valor));
|
|
59
|
+
}
|
|
60
|
+
function digest(valor) {
|
|
61
|
+
return typeof valor === "string" && /^sha256:[a-f0-9]{64}$/u.test(valor);
|
|
62
|
+
}
|
|
63
|
+
function listaTexto(valor, permitirVazia = false) {
|
|
64
|
+
return Array.isArray(valor)
|
|
65
|
+
&& (permitirVazia || valor.length > 0)
|
|
66
|
+
&& valor.every(texto)
|
|
67
|
+
&& new Set(valor).size === valor.length;
|
|
68
|
+
}
|
|
69
|
+
function adicionar(bloqueios, codigo) {
|
|
70
|
+
if (!bloqueios.includes(codigo))
|
|
71
|
+
bloqueios.push(codigo);
|
|
72
|
+
}
|
|
73
|
+
function validarCamposExatos(valor, permitidos, bloqueios) {
|
|
74
|
+
const conjunto = new Set(permitidos);
|
|
75
|
+
for (const campo of Object.keys(valor)) {
|
|
76
|
+
if (!conjunto.has(campo))
|
|
77
|
+
adicionar(bloqueios, "campo_nao_permitido");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function validarContextoBase(payload, esperado, bloqueios) {
|
|
81
|
+
for (const campo of ["runId", "trustDomainId", "ledgerId"]) {
|
|
82
|
+
if (!texto(payload[campo]))
|
|
83
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
84
|
+
}
|
|
85
|
+
for (const campo of ["trustRootDigest", "policyDigest", "definitionDigest"]) {
|
|
86
|
+
if (!digest(payload[campo]))
|
|
87
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
88
|
+
}
|
|
89
|
+
if (esperado === undefined)
|
|
90
|
+
return;
|
|
91
|
+
for (const campo of ["trustDomainId", "trustRootDigest", "ledgerId", "policyDigest", "definitionDigest"]) {
|
|
92
|
+
if (payload[campo] !== esperado[campo])
|
|
93
|
+
adicionar(bloqueios, `${campo}_divergente`);
|
|
94
|
+
}
|
|
95
|
+
if (esperado.runId !== undefined && payload.runId !== esperado.runId) {
|
|
96
|
+
adicionar(bloqueios, "runId_divergente");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function validarAlvo(valor, bloqueios, indice) {
|
|
100
|
+
if (!objeto(valor)) {
|
|
101
|
+
adicionar(bloqueios, `target_${indice}:estrutura_invalida`);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
validarCamposExatos(valor, ["targetId", "adapterId", "accountScope", "formatProfileId", "locale", "metadata"], bloqueios);
|
|
105
|
+
for (const campo of ["targetId", "adapterId", "accountScope", "formatProfileId", "locale"]) {
|
|
106
|
+
if (!texto(valor[campo]))
|
|
107
|
+
adicionar(bloqueios, `target_${indice}:${campo}_invalido`);
|
|
108
|
+
}
|
|
109
|
+
for (const item of validarReferenciaAccountScopeConteudo(valor.accountScope)) {
|
|
110
|
+
adicionar(bloqueios, `target_${indice}:${item}`);
|
|
111
|
+
}
|
|
112
|
+
if (!objeto(valor.metadata))
|
|
113
|
+
adicionar(bloqueios, `target_${indice}:metadata_invalido`);
|
|
114
|
+
else {
|
|
115
|
+
for (const item of Object.values(valor.metadata)) {
|
|
116
|
+
if (item !== null && !["string", "number", "boolean"].includes(typeof item)) {
|
|
117
|
+
adicionar(bloqueios, `target_${indice}:metadata_valor_nao_escalar`);
|
|
118
|
+
}
|
|
119
|
+
else if (typeof item === "number" && !Number.isFinite(item)) {
|
|
120
|
+
adicionar(bloqueios, `target_${indice}:metadata_numero_nao_finito`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const item of validarMetadataPublicaConteudo(valor.metadata)) {
|
|
124
|
+
adicionar(bloqueios, `target_${indice}:${item}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
function validarEscopoAutorizacao(payload, inicio, bloqueios) {
|
|
130
|
+
if (!texto(payload.authorizationScope)) {
|
|
131
|
+
adicionar(bloqueios, "authorizationScope_invalido");
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (inicio === undefined)
|
|
135
|
+
return;
|
|
136
|
+
const targetId = payload.targetId;
|
|
137
|
+
if (targetId === undefined) {
|
|
138
|
+
if (payload.authorizationScope !== escopoAutorizacaoGlobal(inicio.runId)) {
|
|
139
|
+
adicionar(bloqueios, "authorizationScope_divergente");
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!texto(targetId)) {
|
|
144
|
+
adicionar(bloqueios, "targetId_invalido");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const alvo = inicio.targets.find((item) => item.targetId === targetId);
|
|
148
|
+
if (alvo === undefined) {
|
|
149
|
+
adicionar(bloqueios, "target_nao_pertence_a_execucao");
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (payload.authorizationScope !== escopoAutorizacaoAlvo(inicio.runId, alvo)) {
|
|
153
|
+
adicionar(bloqueios, "authorizationScope_divergente");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function validarIdentidadePayload(kind, payload, principalId, bloqueios) {
|
|
157
|
+
if (principalId === undefined)
|
|
158
|
+
return;
|
|
159
|
+
const campo = kind === "ARTIFACT_REGISTERED" || kind === "EVIDENCE_CLAIMED"
|
|
160
|
+
? "producerId"
|
|
161
|
+
: kind === "EVIDENCE_ATTESTED"
|
|
162
|
+
? "attesterId"
|
|
163
|
+
: kind === "AI_ASSESSMENT"
|
|
164
|
+
? "evaluatorId"
|
|
165
|
+
: undefined;
|
|
166
|
+
if (campo !== undefined && payload[campo] !== principalId)
|
|
167
|
+
adicionar(bloqueios, `${campo}_divergente_do_signatario`);
|
|
168
|
+
}
|
|
169
|
+
function artefatoExato(payload, artefatos) {
|
|
170
|
+
return [...artefatos].reverse().find((item) => item.runId === payload.runId
|
|
171
|
+
&& item.stageId === payload.stageId
|
|
172
|
+
&& item.targetId === payload.targetId
|
|
173
|
+
&& item.digest === payload.artifactDigest
|
|
174
|
+
&& item.producerId === payload.producerId);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Validador estrutural/contextual publico usado tambem pela CLI antes do append.
|
|
178
|
+
* Ele nao valida assinatura; essa responsabilidade continua na raiz de confianca.
|
|
179
|
+
*/
|
|
180
|
+
export function validarPayloadEventoConteudo(valor, entrada = {}) {
|
|
181
|
+
const bloqueios = [];
|
|
182
|
+
if (!objeto(valor) || !texto(valor.kind) || !KINDS_EVENTO.has(valor.kind)) {
|
|
183
|
+
return { valido: false, bloqueios: ["payload_kind_invalido"] };
|
|
184
|
+
}
|
|
185
|
+
const kind = valor.kind;
|
|
186
|
+
validarCamposExatos(valor, CAMPOS_POR_KIND[kind], bloqueios);
|
|
187
|
+
validarContextoBase(valor, entrada.contextoEsperado, bloqueios);
|
|
188
|
+
validarIdentidadePayload(kind, valor, entrada.principalId, bloqueios);
|
|
189
|
+
if (kind === "RUN_STARTED") {
|
|
190
|
+
if (!instante(valor.startedAt))
|
|
191
|
+
adicionar(bloqueios, "startedAt_invalido");
|
|
192
|
+
if (!Array.isArray(valor.targets) || valor.targets.length === 0) {
|
|
193
|
+
adicionar(bloqueios, "targets_invalidos");
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const ids = new Set();
|
|
197
|
+
valor.targets.forEach((alvo, indice) => {
|
|
198
|
+
if (validarAlvo(alvo, bloqueios, indice) && objeto(alvo)) {
|
|
199
|
+
const id = alvo.targetId;
|
|
200
|
+
if (ids.has(id))
|
|
201
|
+
adicionar(bloqueios, `target_${indice}:targetId_duplicado`);
|
|
202
|
+
ids.add(id);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
validarEscopoAutorizacao(valor, entrada.inicio, bloqueios);
|
|
209
|
+
if (valor.targetId !== undefined && !texto(valor.targetId))
|
|
210
|
+
adicionar(bloqueios, "targetId_invalido");
|
|
211
|
+
}
|
|
212
|
+
if (kind === "OPERATIONAL_CONDITION") {
|
|
213
|
+
if (!texto(valor.gateId))
|
|
214
|
+
adicionar(bloqueios, "gateId_invalido");
|
|
215
|
+
if (!CONDICOES_OPERACIONAIS.has(String(valor.condition)))
|
|
216
|
+
adicionar(bloqueios, "condition_invalida");
|
|
217
|
+
if (!texto(valor.reason))
|
|
218
|
+
adicionar(bloqueios, "reason_invalido");
|
|
219
|
+
else if (validarMetadataPublicaConteudo({ reason: valor.reason }).length > 0) {
|
|
220
|
+
adicionar(bloqueios, "reason_contem_possivel_credencial");
|
|
221
|
+
}
|
|
222
|
+
if (!instante(valor.reportedAt))
|
|
223
|
+
adicionar(bloqueios, "reportedAt_invalido");
|
|
224
|
+
}
|
|
225
|
+
if (kind === "ARTIFACT_REGISTERED") {
|
|
226
|
+
for (const campo of ["artifactId", "stageId", "producerId", "version", "artifactType", "mediaType"]) {
|
|
227
|
+
if (!texto(valor[campo]))
|
|
228
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
229
|
+
}
|
|
230
|
+
if (!digest(valor.digest))
|
|
231
|
+
adicionar(bloqueios, "digest_invalido");
|
|
232
|
+
const lineageDigestsValidos = listaTexto(valor.lineageDigests, true)
|
|
233
|
+
&& valor.lineageDigests.every(digest);
|
|
234
|
+
if (!lineageDigestsValidos) {
|
|
235
|
+
adicionar(bloqueios, "lineageDigests_invalidos");
|
|
236
|
+
}
|
|
237
|
+
if (!listaTexto(valor.producerLineageIds, false) || !valor.producerLineageIds.includes(String(valor.producerId))) {
|
|
238
|
+
adicionar(bloqueios, "producerLineageIds_invalidos");
|
|
239
|
+
}
|
|
240
|
+
if (Object.prototype.hasOwnProperty.call(valor, "metadata"))
|
|
241
|
+
adicionar(bloqueios, "artifact_metadata_nao_permitida");
|
|
242
|
+
const anteriores = entrada.artefatosAnteriores ?? [];
|
|
243
|
+
if (lineageDigestsValidos) {
|
|
244
|
+
for (const anteriorDigest of valor.lineageDigests) {
|
|
245
|
+
if (!anteriores.some((item) => item.digest === anteriorDigest)) {
|
|
246
|
+
adicionar(bloqueios, "lineage_digest_nao_encontrado");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (kind === "EVIDENCE_CLAIMED") {
|
|
252
|
+
for (const campo of ["claimId", "stageId", "artifactDigest", "evidenceType", "producerId"]) {
|
|
253
|
+
if (!texto(valor[campo]))
|
|
254
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
255
|
+
}
|
|
256
|
+
if (!digest(valor.artifactDigest))
|
|
257
|
+
adicionar(bloqueios, "artifactDigest_invalido");
|
|
258
|
+
if (!instante(valor.claimedAt))
|
|
259
|
+
adicionar(bloqueios, "claimedAt_invalido");
|
|
260
|
+
if (valor.data !== undefined && !objeto(valor.data))
|
|
261
|
+
adicionar(bloqueios, "data_invalido");
|
|
262
|
+
else if (valor.data !== undefined) {
|
|
263
|
+
for (const item of validarMetadataPublicaConteudo(valor.data))
|
|
264
|
+
adicionar(bloqueios, item.replace("metadata", "data"));
|
|
265
|
+
}
|
|
266
|
+
if (artefatoExato(valor, entrada.artefatosAnteriores ?? []) === undefined) {
|
|
267
|
+
adicionar(bloqueios, "artefato_da_alegacao_nao_encontrado");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (kind === "EVIDENCE_ATTESTED") {
|
|
271
|
+
for (const campo of ["evidenceId", "gateId", "stageId", "artifactDigest", "evidenceType", "producerId", "attesterId"]) {
|
|
272
|
+
if (!texto(valor[campo]))
|
|
273
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
274
|
+
}
|
|
275
|
+
if (valor.claimId !== undefined && !texto(valor.claimId))
|
|
276
|
+
adicionar(bloqueios, "claimId_invalido");
|
|
277
|
+
if (!digest(valor.artifactDigest))
|
|
278
|
+
adicionar(bloqueios, "artifactDigest_invalido");
|
|
279
|
+
if (!RESULTADOS.has(String(valor.result)))
|
|
280
|
+
adicionar(bloqueios, "result_invalido");
|
|
281
|
+
if (!instante(valor.observedAt))
|
|
282
|
+
adicionar(bloqueios, "observedAt_invalido");
|
|
283
|
+
if (valor.data !== undefined && !objeto(valor.data))
|
|
284
|
+
adicionar(bloqueios, "data_invalido");
|
|
285
|
+
else if (valor.data !== undefined) {
|
|
286
|
+
for (const item of validarMetadataPublicaConteudo(valor.data))
|
|
287
|
+
adicionar(bloqueios, item.replace("metadata", "data"));
|
|
288
|
+
}
|
|
289
|
+
if (valor.claimId !== undefined) {
|
|
290
|
+
const claim = (entrada.alegacoesAnteriores ?? []).find((item) => item.claimId === valor.claimId);
|
|
291
|
+
if (claim === undefined
|
|
292
|
+
|| claim.runId !== valor.runId
|
|
293
|
+
|| claim.stageId !== valor.stageId
|
|
294
|
+
|| claim.targetId !== valor.targetId
|
|
295
|
+
|| claim.artifactDigest !== valor.artifactDigest
|
|
296
|
+
|| claim.evidenceType !== valor.evidenceType
|
|
297
|
+
|| claim.producerId !== valor.producerId) {
|
|
298
|
+
adicionar(bloqueios, "alegacao_do_atestado_divergente");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (kind === "AI_ASSESSMENT") {
|
|
303
|
+
for (const campo of ["assessmentId", "gateId", "artifactDigest", "rubricDigest", "evaluatorId", "capability"]) {
|
|
304
|
+
if (!texto(valor[campo]))
|
|
305
|
+
adicionar(bloqueios, `${campo}_invalido`);
|
|
306
|
+
}
|
|
307
|
+
if (!digest(valor.artifactDigest))
|
|
308
|
+
adicionar(bloqueios, "artifactDigest_invalido");
|
|
309
|
+
if (!digest(valor.rubricDigest))
|
|
310
|
+
adicionar(bloqueios, "rubricDigest_invalido");
|
|
311
|
+
if (valor.rationaleDigest !== undefined && !digest(valor.rationaleDigest))
|
|
312
|
+
adicionar(bloqueios, "rationaleDigest_invalido");
|
|
313
|
+
if (!RESULTADOS.has(String(valor.result)))
|
|
314
|
+
adicionar(bloqueios, "result_invalido");
|
|
315
|
+
if (!instante(valor.assessedAt))
|
|
316
|
+
adicionar(bloqueios, "assessedAt_invalido");
|
|
317
|
+
if (valor.data !== undefined && !objeto(valor.data))
|
|
318
|
+
adicionar(bloqueios, "data_invalido");
|
|
319
|
+
else if (valor.data !== undefined) {
|
|
320
|
+
for (const item of validarMetadataPublicaConteudo(valor.data))
|
|
321
|
+
adicionar(bloqueios, item.replace("metadata", "data"));
|
|
322
|
+
}
|
|
323
|
+
const artefato = [...(entrada.artefatosAnteriores ?? [])].reverse().find((item) => item.runId === valor.runId && item.targetId === valor.targetId && item.digest === valor.artifactDigest);
|
|
324
|
+
if (artefato === undefined)
|
|
325
|
+
adicionar(bloqueios, "artefato_do_parecer_nao_encontrado");
|
|
326
|
+
}
|
|
327
|
+
return { valido: bloqueios.length === 0, kind, bloqueios };
|
|
328
|
+
}
|
|
329
|
+
function semHash(evento) {
|
|
330
|
+
return {
|
|
331
|
+
schemaVersion: evento.schemaVersion,
|
|
332
|
+
ledgerId: evento.ledgerId,
|
|
333
|
+
sequence: evento.sequence,
|
|
334
|
+
eventId: evento.eventId,
|
|
335
|
+
recordedAt: evento.recordedAt,
|
|
336
|
+
previousHash: evento.previousHash,
|
|
337
|
+
envelope: evento.envelope,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
export function calcularHashEventoLedgerConteudo(evento) {
|
|
341
|
+
return hashCanonicoConteudo(evento);
|
|
342
|
+
}
|
|
343
|
+
function payloadTypeEsperado(payload) {
|
|
344
|
+
return payload.kind === "EVIDENCE_CLAIMED" ? "CLAIM_SUBMITTED" : payload.kind;
|
|
345
|
+
}
|
|
346
|
+
function papeisPermitidos(payload) {
|
|
347
|
+
switch (payload.kind) {
|
|
348
|
+
case "RUN_STARTED": return ["PIPELINE_CONTROLLER"];
|
|
349
|
+
case "ARTIFACT_REGISTERED": return ["PRODUCER"];
|
|
350
|
+
case "EVIDENCE_CLAIMED": return ["PRODUCER", "RUNNER", "ADAPTER"];
|
|
351
|
+
case "EVIDENCE_ATTESTED": return ["EVIDENCE_ATTESTER", "ADAPTER"];
|
|
352
|
+
case "AI_ASSESSMENT": return ["EVALUATOR"];
|
|
353
|
+
case "OPERATIONAL_CONDITION": return ["RUNNER", "ADAPTER"];
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function configuracaoConfiancaAtual(entrada) {
|
|
357
|
+
return {
|
|
358
|
+
trustDomainId: entrada.configuracaoConfianca?.trustDomainId ?? `ledger:${entrada.ledgerId}`,
|
|
359
|
+
principals: entrada.principals,
|
|
360
|
+
revokedKeyIds: entrada.configuracaoConfianca?.revokedKeyIds ?? [],
|
|
361
|
+
maxEnvelopeAgeMs: entrada.configuracaoConfianca?.maxEnvelopeAgeMs,
|
|
362
|
+
maxFutureSkewMs: entrada.configuracaoConfianca?.maxFutureSkewMs,
|
|
363
|
+
schemaVersionsAceitas: entrada.configuracaoConfianca?.schemaVersionsAceitas,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function contextoDoInicio(inicio) {
|
|
367
|
+
return {
|
|
368
|
+
runId: inicio.runId,
|
|
369
|
+
trustDomainId: inicio.trustDomainId,
|
|
370
|
+
trustRootDigest: inicio.trustRootDigest,
|
|
371
|
+
ledgerId: inicio.ledgerId,
|
|
372
|
+
policyDigest: inicio.policyDigest,
|
|
373
|
+
definitionDigest: inicio.definitionDigest,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
export function validarLedgerConteudo(entrada) {
|
|
377
|
+
const bloqueios = [];
|
|
378
|
+
const eventIds = new Set();
|
|
379
|
+
const nonces = new Set();
|
|
380
|
+
const artefatos = [];
|
|
381
|
+
const alegacoes = [];
|
|
382
|
+
const confiancaAtual = configuracaoConfiancaAtual(entrada);
|
|
383
|
+
const raizConfianca = validarConfiguracaoConfiancaConteudo(confiancaAtual, entrada.trustRootDigestEsperado, entrada.revocationDigestEsperado);
|
|
384
|
+
for (const bloqueio of raizConfianca.bloqueios)
|
|
385
|
+
bloqueios.push(`raiz_confianca_invalida:${bloqueio}`);
|
|
386
|
+
let politica;
|
|
387
|
+
let policyIssuedAt = Number.NaN;
|
|
388
|
+
let policyExpiresAt = Number.NaN;
|
|
389
|
+
if (entrada.envelopePolitica === undefined || entrada.envelopePolitica === null) {
|
|
390
|
+
adicionar(bloqueios, "envelope_politica_ausente");
|
|
391
|
+
}
|
|
392
|
+
else if (!objeto(entrada.envelopePolitica) || !objeto(entrada.envelopePolitica.payload)) {
|
|
393
|
+
adicionar(bloqueios, "envelope_politica_invalido");
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
politica = entrada.envelopePolitica.payload;
|
|
397
|
+
const verificacaoPolitica = verificarEnvelopeAssinadoConteudo(entrada.envelopePolitica, confiancaAtual, {
|
|
398
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
399
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
400
|
+
payloadTypeEsperado: "TRUST_POLICY",
|
|
401
|
+
papelRequerido: "POLICY_AUTHORITY",
|
|
402
|
+
agora: politica.issuedAt,
|
|
403
|
+
});
|
|
404
|
+
for (const bloqueio of verificacaoPolitica.bloqueios) {
|
|
405
|
+
adicionar(bloqueios, `politica_nao_confiavel:${bloqueio}`);
|
|
406
|
+
}
|
|
407
|
+
policyIssuedAt = Date.parse(politica.issuedAt);
|
|
408
|
+
policyExpiresAt = Date.parse(politica.expiresAt);
|
|
409
|
+
if (!Number.isFinite(policyIssuedAt) || !Number.isFinite(policyExpiresAt) || policyExpiresAt <= policyIssuedAt) {
|
|
410
|
+
adicionar(bloqueios, "politica_janela_temporal_invalida");
|
|
411
|
+
}
|
|
412
|
+
let policyDigest = "";
|
|
413
|
+
try {
|
|
414
|
+
policyDigest = hashCanonicoConteudo(politica);
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
adicionar(bloqueios, "politica_payload_nao_canonico");
|
|
418
|
+
}
|
|
419
|
+
if (politica.runId !== entrada.contextoEsperado.runId
|
|
420
|
+
|| politica.trustDomainId !== entrada.contextoEsperado.trustDomainId
|
|
421
|
+
|| politica.trustDomainId !== confiancaAtual.trustDomainId
|
|
422
|
+
|| politica.trustRootDigest !== entrada.contextoEsperado.trustRootDigest
|
|
423
|
+
|| politica.trustRootDigest !== entrada.trustRootDigestEsperado
|
|
424
|
+
|| politica.ledgerId !== entrada.contextoEsperado.ledgerId
|
|
425
|
+
|| politica.ledgerId !== entrada.ledgerId
|
|
426
|
+
|| politica.definitionDigest !== entrada.contextoEsperado.definitionDigest
|
|
427
|
+
|| policyDigest !== entrada.contextoEsperado.policyDigest
|
|
428
|
+
|| entrada.envelopePolitica.issuedAt !== politica.issuedAt) {
|
|
429
|
+
adicionar(bloqueios, "politica_contexto_divergente");
|
|
430
|
+
}
|
|
431
|
+
if (!digest(politica.targetSetDigest))
|
|
432
|
+
adicionar(bloqueios, "politica_target_set_digest_invalido");
|
|
433
|
+
}
|
|
434
|
+
let inicio;
|
|
435
|
+
let head = HEAD_GENESIS_LEDGER_CONTEUDO;
|
|
436
|
+
let eventosValidos = 0;
|
|
437
|
+
entrada.eventos.forEach((evento, indice) => {
|
|
438
|
+
const antes = bloqueios.length;
|
|
439
|
+
const prefixo = `evento_${indice}:`;
|
|
440
|
+
const bloquear = (codigo) => {
|
|
441
|
+
bloqueios.push(`${prefixo}${codigo}`);
|
|
442
|
+
};
|
|
443
|
+
const bloqueiosEstrutura = [];
|
|
444
|
+
validarCamposExatos(evento, CAMPOS_EVENTO_LEDGER, bloqueiosEstrutura);
|
|
445
|
+
for (const item of bloqueiosEstrutura)
|
|
446
|
+
bloquear(item);
|
|
447
|
+
if (!texto(evento.schemaVersion))
|
|
448
|
+
bloquear("schema_version_invalida");
|
|
449
|
+
if (evento.schemaVersion !== evento.envelope?.schemaVersion)
|
|
450
|
+
bloquear("schema_version_divergente_do_envelope");
|
|
451
|
+
if (evento.ledgerId !== entrada.ledgerId)
|
|
452
|
+
bloquear("ledger_id_divergente");
|
|
453
|
+
if (!Number.isSafeInteger(evento.sequence) || evento.sequence !== indice)
|
|
454
|
+
bloquear("sequence_invalida");
|
|
455
|
+
if (!texto(evento.eventId))
|
|
456
|
+
bloquear("event_id_ausente");
|
|
457
|
+
else if (eventIds.has(evento.eventId))
|
|
458
|
+
bloquear("event_id_repetido");
|
|
459
|
+
eventIds.add(evento.eventId);
|
|
460
|
+
if (!instante(evento.recordedAt))
|
|
461
|
+
bloquear("recorded_at_invalido");
|
|
462
|
+
else if (Number.isFinite(policyIssuedAt)
|
|
463
|
+
&& Number.isFinite(policyExpiresAt)
|
|
464
|
+
&& (Date.parse(evento.recordedAt) < policyIssuedAt || Date.parse(evento.recordedAt) > policyExpiresAt)) {
|
|
465
|
+
bloquear("recorded_at_fora_da_janela_da_politica");
|
|
466
|
+
}
|
|
467
|
+
if (evento.previousHash !== head)
|
|
468
|
+
bloquear("previous_hash_invalido");
|
|
469
|
+
try {
|
|
470
|
+
if (evento.hash !== calcularHashEventoLedgerConteudo(semHash(evento)))
|
|
471
|
+
bloquear("hash_invalido");
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
bloquear("evento_nao_canonico");
|
|
475
|
+
}
|
|
476
|
+
const payload = evento.envelope?.payload;
|
|
477
|
+
const contexto = inicio === undefined ? entrada.contextoEsperado : contextoDoInicio(inicio);
|
|
478
|
+
const validacaoPayload = validarPayloadEventoConteudo(payload, {
|
|
479
|
+
principalId: evento.envelope?.principalId,
|
|
480
|
+
contextoEsperado: contexto,
|
|
481
|
+
inicio,
|
|
482
|
+
artefatosAnteriores: artefatos,
|
|
483
|
+
alegacoesAnteriores: alegacoes,
|
|
484
|
+
});
|
|
485
|
+
for (const item of validacaoPayload.bloqueios)
|
|
486
|
+
bloquear(item);
|
|
487
|
+
if (validacaoPayload.valido) {
|
|
488
|
+
const tipado = payload;
|
|
489
|
+
if (tipado.kind === "RUN_STARTED") {
|
|
490
|
+
if (indice !== 0)
|
|
491
|
+
bloquear("run_started_fora_da_genesis");
|
|
492
|
+
if (inicio !== undefined)
|
|
493
|
+
bloquear("run_started_duplicado");
|
|
494
|
+
if (politica !== undefined) {
|
|
495
|
+
try {
|
|
496
|
+
if (hashCanonicoConteudo(tipado.targets) !== politica.targetSetDigest) {
|
|
497
|
+
bloquear("target_set_digest_divergente_da_politica");
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
catch {
|
|
501
|
+
bloquear("target_set_nao_canonico");
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
inicio = tipado;
|
|
505
|
+
}
|
|
506
|
+
else if (inicio === undefined) {
|
|
507
|
+
bloquear("run_nao_iniciado");
|
|
508
|
+
}
|
|
509
|
+
if (tipado.kind === "ARTIFACT_REGISTERED") {
|
|
510
|
+
if (artefatos.some((item) => item.digest === tipado.digest))
|
|
511
|
+
bloquear("artifact_digest_repetido");
|
|
512
|
+
else
|
|
513
|
+
artefatos.push(tipado);
|
|
514
|
+
}
|
|
515
|
+
if (tipado.kind === "EVIDENCE_CLAIMED") {
|
|
516
|
+
if (alegacoes.some((item) => item.claimId === tipado.claimId))
|
|
517
|
+
bloquear("claim_id_repetido");
|
|
518
|
+
else
|
|
519
|
+
alegacoes.push(tipado);
|
|
520
|
+
}
|
|
521
|
+
const esperado = payloadTypeEsperado(tipado);
|
|
522
|
+
if (evento.envelope.payloadType !== esperado)
|
|
523
|
+
bloquear("payload_type_invalido");
|
|
524
|
+
const verificacao = verificarEnvelopeAssinadoConteudoHistoricoInterno(evento.envelope, confiancaAtual, {
|
|
525
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
526
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
527
|
+
payloadTypeEsperado: esperado,
|
|
528
|
+
papeisPermitidos: papeisPermitidos(tipado),
|
|
529
|
+
scopeRequerido: tipado.kind === "RUN_STARTED" ? undefined : tipado.authorizationScope,
|
|
530
|
+
agora: instante(evento.recordedAt) ? evento.recordedAt : undefined,
|
|
531
|
+
noncesUsados: nonces,
|
|
532
|
+
});
|
|
533
|
+
for (const item of verificacao.bloqueios)
|
|
534
|
+
bloquear(item);
|
|
535
|
+
}
|
|
536
|
+
if (nonces.has(evento.envelope?.nonce))
|
|
537
|
+
bloquear("nonce_repetido");
|
|
538
|
+
if (texto(evento.envelope?.nonce))
|
|
539
|
+
nonces.add(evento.envelope.nonce);
|
|
540
|
+
if (bloqueios.length === antes)
|
|
541
|
+
eventosValidos += 1;
|
|
542
|
+
head = evento.hash;
|
|
543
|
+
});
|
|
544
|
+
if (!digest(entrada.expectedHead))
|
|
545
|
+
bloqueios.push("expected_head_invalido");
|
|
546
|
+
else if (entrada.expectedHead !== head)
|
|
547
|
+
bloqueios.push("expected_head_divergente");
|
|
548
|
+
return { valido: bloqueios.length === 0, head, eventosValidos, bloqueios: [...new Set(bloqueios)] };
|
|
549
|
+
}
|
|
550
|
+
function validarEstruturaAntesDoAppend(eventos, ledgerId) {
|
|
551
|
+
let head = HEAD_GENESIS_LEDGER_CONTEUDO;
|
|
552
|
+
const ids = new Set();
|
|
553
|
+
const nonces = new Set();
|
|
554
|
+
eventos.forEach((evento, indice) => {
|
|
555
|
+
const bloqueiosEstrutura = [];
|
|
556
|
+
validarCamposExatos(evento, CAMPOS_EVENTO_LEDGER, bloqueiosEstrutura);
|
|
557
|
+
if (bloqueiosEstrutura.length > 0)
|
|
558
|
+
throw new Error(`${bloqueiosEstrutura[0]}:${indice}`);
|
|
559
|
+
if (evento.ledgerId !== ledgerId)
|
|
560
|
+
throw new Error(`ledger_id_divergente:${indice}`);
|
|
561
|
+
if (evento.sequence !== indice)
|
|
562
|
+
throw new Error(`sequence_invalida:${indice}`);
|
|
563
|
+
if (evento.previousHash !== head)
|
|
564
|
+
throw new Error(`previous_hash_invalido:${indice}`);
|
|
565
|
+
if (calcularHashEventoLedgerConteudo(semHash(evento)) !== evento.hash)
|
|
566
|
+
throw new Error(`hash_invalido:${indice}`);
|
|
567
|
+
if (ids.has(evento.eventId))
|
|
568
|
+
throw new Error(`event_id_repetido:${indice}`);
|
|
569
|
+
if (nonces.has(evento.envelope.nonce))
|
|
570
|
+
throw new Error(`nonce_repetido:${indice}`);
|
|
571
|
+
ids.add(evento.eventId);
|
|
572
|
+
nonces.add(evento.envelope.nonce);
|
|
573
|
+
head = evento.hash;
|
|
574
|
+
});
|
|
575
|
+
return head;
|
|
576
|
+
}
|
|
577
|
+
export function anexarEventoLedgerConteudo(eventosAtuais, entrada) {
|
|
578
|
+
const raizConfianca = validarConfiguracaoConfiancaConteudo(entrada.configuracaoConfianca, entrada.trustRootDigestEsperado, entrada.revocationDigestEsperado);
|
|
579
|
+
if (!raizConfianca.valida)
|
|
580
|
+
throw new Error(`raiz_confianca_invalida:${raizConfianca.bloqueios.join(",")}`);
|
|
581
|
+
if (entrada.envelopePolitica === undefined || entrada.envelopePolitica === null) {
|
|
582
|
+
throw new Error("envelope_politica_ausente");
|
|
583
|
+
}
|
|
584
|
+
const politica = entrada.envelopePolitica.payload;
|
|
585
|
+
const agora = Date.now();
|
|
586
|
+
const verificacaoPolitica = verificarEnvelopeAssinadoConteudo(entrada.envelopePolitica, entrada.configuracaoConfianca, {
|
|
587
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
588
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
589
|
+
payloadTypeEsperado: "TRUST_POLICY",
|
|
590
|
+
papelRequerido: "POLICY_AUTHORITY",
|
|
591
|
+
agora: politica.issuedAt,
|
|
592
|
+
});
|
|
593
|
+
if (!verificacaoPolitica.valido) {
|
|
594
|
+
throw new Error(`politica_nao_confiavel:${verificacaoPolitica.bloqueios.join(",")}`);
|
|
595
|
+
}
|
|
596
|
+
const policyIssuedAt = Date.parse(politica.issuedAt);
|
|
597
|
+
const policyExpiresAt = Date.parse(politica.expiresAt);
|
|
598
|
+
if (!Number.isFinite(policyIssuedAt) ||
|
|
599
|
+
!Number.isFinite(policyExpiresAt) ||
|
|
600
|
+
policyExpiresAt <= policyIssuedAt) {
|
|
601
|
+
throw new Error("politica_janela_temporal_invalida");
|
|
602
|
+
}
|
|
603
|
+
if (agora > policyExpiresAt)
|
|
604
|
+
throw new Error("politica_expirada");
|
|
605
|
+
const policyDigest = hashCanonicoConteudo(politica);
|
|
606
|
+
const contextoPolitica = {
|
|
607
|
+
runId: politica.runId,
|
|
608
|
+
trustDomainId: politica.trustDomainId,
|
|
609
|
+
trustRootDigest: politica.trustRootDigest,
|
|
610
|
+
ledgerId: politica.ledgerId,
|
|
611
|
+
policyDigest,
|
|
612
|
+
definitionDigest: politica.definitionDigest,
|
|
613
|
+
};
|
|
614
|
+
if (!texto(politica.runId) ||
|
|
615
|
+
politica.trustDomainId !== entrada.configuracaoConfianca.trustDomainId ||
|
|
616
|
+
politica.trustRootDigest !== entrada.trustRootDigestEsperado ||
|
|
617
|
+
politica.ledgerId !== entrada.ledgerId ||
|
|
618
|
+
entrada.envelopePolitica.issuedAt !== politica.issuedAt ||
|
|
619
|
+
entrada.envelope.payload.runId !== politica.runId ||
|
|
620
|
+
entrada.envelope.payload.policyDigest !== policyDigest ||
|
|
621
|
+
entrada.envelope.payload.definitionDigest !== politica.definitionDigest) {
|
|
622
|
+
throw new Error("politica_contexto_divergente");
|
|
623
|
+
}
|
|
624
|
+
const headAtual = validarEstruturaAntesDoAppend(eventosAtuais, entrada.ledgerId);
|
|
625
|
+
if (entrada.expectedHead !== headAtual)
|
|
626
|
+
throw new Error("expected_head_divergente");
|
|
627
|
+
const ledgerAtual = validarLedgerConteudo({
|
|
628
|
+
eventos: eventosAtuais,
|
|
629
|
+
envelopePolitica: entrada.envelopePolitica,
|
|
630
|
+
ledgerId: entrada.ledgerId,
|
|
631
|
+
expectedHead: entrada.expectedHead,
|
|
632
|
+
principals: entrada.configuracaoConfianca.principals,
|
|
633
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
634
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
635
|
+
configuracaoConfianca: entrada.configuracaoConfianca,
|
|
636
|
+
contextoEsperado: contextoPolitica,
|
|
637
|
+
});
|
|
638
|
+
if (!ledgerAtual.valido)
|
|
639
|
+
throw new Error(`ledger_invalido:${ledgerAtual.bloqueios.join(",")}`);
|
|
640
|
+
if (!instante(entrada.recordedAt))
|
|
641
|
+
throw new Error("recorded_at_invalido");
|
|
642
|
+
const registradoEm = Date.parse(entrada.recordedAt);
|
|
643
|
+
const idadeMaxima = entrada.configuracaoConfianca.maxEnvelopeAgeMs ?? IDADE_MAXIMA_APPEND_PADRAO_MS;
|
|
644
|
+
const desvioFuturo = entrada.configuracaoConfianca.maxFutureSkewMs ?? DESVIO_FUTURO_APPEND_PADRAO_MS;
|
|
645
|
+
if (registradoEm < agora - idadeMaxima)
|
|
646
|
+
throw new Error("recorded_at_expirado");
|
|
647
|
+
if (registradoEm > agora + desvioFuturo)
|
|
648
|
+
throw new Error("recorded_at_no_futuro");
|
|
649
|
+
if (registradoEm < policyIssuedAt || registradoEm > policyExpiresAt) {
|
|
650
|
+
throw new Error("evento_fora_da_janela_da_politica");
|
|
651
|
+
}
|
|
652
|
+
if (eventosAtuais.length === 0) {
|
|
653
|
+
if (entrada.envelope.payload.kind !== "RUN_STARTED")
|
|
654
|
+
throw new Error("genesis_exige_run_started");
|
|
655
|
+
if (policyIssuedAt > registradoEm + desvioFuturo ||
|
|
656
|
+
registradoEm - policyIssuedAt > idadeMaxima ||
|
|
657
|
+
registradoEm > policyExpiresAt) {
|
|
658
|
+
throw new Error("politica_fora_da_janela_de_ativacao");
|
|
659
|
+
}
|
|
660
|
+
if (hashCanonicoConteudo(entrada.envelope.payload.targets) !== politica.targetSetDigest) {
|
|
661
|
+
throw new Error("politica_target_set_digest_divergente");
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
if (eventosAtuais.some((evento) => evento.eventId === entrada.eventId))
|
|
665
|
+
throw new Error("event_id_repetido");
|
|
666
|
+
if (eventosAtuais.some((evento) => evento.envelope.nonce === entrada.envelope.nonce))
|
|
667
|
+
throw new Error("nonce_repetido");
|
|
668
|
+
const inicio = eventosAtuais.find((evento) => evento.envelope.payload.kind === "RUN_STARTED")
|
|
669
|
+
?.envelope.payload;
|
|
670
|
+
const artefatos = eventosAtuais
|
|
671
|
+
.filter((evento) => evento.envelope.payload.kind === "ARTIFACT_REGISTERED")
|
|
672
|
+
.map((evento) => evento.envelope.payload);
|
|
673
|
+
const alegacoes = eventosAtuais
|
|
674
|
+
.filter((evento) => evento.envelope.payload.kind === "EVIDENCE_CLAIMED")
|
|
675
|
+
.map((evento) => evento.envelope.payload);
|
|
676
|
+
const validacaoPayload = validarPayloadEventoConteudo(entrada.envelope.payload, {
|
|
677
|
+
principalId: entrada.envelope.principalId,
|
|
678
|
+
contextoEsperado: inicio ? contextoDoInicio(inicio) : contextoPolitica,
|
|
679
|
+
inicio,
|
|
680
|
+
artefatosAnteriores: artefatos,
|
|
681
|
+
alegacoesAnteriores: alegacoes,
|
|
682
|
+
});
|
|
683
|
+
if (!validacaoPayload.valido)
|
|
684
|
+
throw new Error(`payload_invalido:${validacaoPayload.bloqueios.join(",")}`);
|
|
685
|
+
const payload = entrada.envelope.payload;
|
|
686
|
+
const verificacaoEnvelope = verificarEnvelopeAssinadoConteudo(entrada.envelope, entrada.configuracaoConfianca, {
|
|
687
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
688
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
689
|
+
payloadTypeEsperado: payloadTypeEsperado(payload),
|
|
690
|
+
papeisPermitidos: papeisPermitidos(payload),
|
|
691
|
+
scopeRequerido: payload.kind === "RUN_STARTED" ? undefined : payload.authorizationScope,
|
|
692
|
+
agora,
|
|
693
|
+
noncesUsados: new Set(eventosAtuais.map((evento) => evento.envelope.nonce)),
|
|
694
|
+
});
|
|
695
|
+
if (!verificacaoEnvelope.valido) {
|
|
696
|
+
throw new Error(`envelope_nao_confiavel:${verificacaoEnvelope.bloqueios.join(",")}`);
|
|
697
|
+
}
|
|
698
|
+
const eventoSemHash = {
|
|
699
|
+
schemaVersion: entrada.schemaVersion,
|
|
700
|
+
ledgerId: entrada.ledgerId,
|
|
701
|
+
sequence: eventosAtuais.length,
|
|
702
|
+
eventId: entrada.eventId,
|
|
703
|
+
recordedAt: entrada.recordedAt,
|
|
704
|
+
previousHash: headAtual,
|
|
705
|
+
envelope: entrada.envelope,
|
|
706
|
+
};
|
|
707
|
+
const evento = {
|
|
708
|
+
...eventoSemHash,
|
|
709
|
+
hash: calcularHashEventoLedgerConteudo(eventoSemHash),
|
|
710
|
+
};
|
|
711
|
+
const candidato = [...eventosAtuais, evento];
|
|
712
|
+
const validacaoCandidato = validarLedgerConteudo({
|
|
713
|
+
eventos: candidato,
|
|
714
|
+
envelopePolitica: entrada.envelopePolitica,
|
|
715
|
+
ledgerId: entrada.ledgerId,
|
|
716
|
+
expectedHead: evento.hash,
|
|
717
|
+
principals: entrada.configuracaoConfianca.principals,
|
|
718
|
+
trustRootDigestEsperado: entrada.trustRootDigestEsperado,
|
|
719
|
+
revocationDigestEsperado: entrada.revocationDigestEsperado,
|
|
720
|
+
configuracaoConfianca: entrada.configuracaoConfianca,
|
|
721
|
+
contextoEsperado: contextoPolitica,
|
|
722
|
+
});
|
|
723
|
+
if (!validacaoCandidato.valido) {
|
|
724
|
+
throw new Error(`evento_nao_registravel:${validacaoCandidato.bloqueios.join(",")}`);
|
|
725
|
+
}
|
|
726
|
+
return {
|
|
727
|
+
eventos: candidato,
|
|
728
|
+
evento,
|
|
729
|
+
head: evento.hash,
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
//# sourceMappingURL=ledger.js.map
|