-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_interop_node.js
More file actions
127 lines (108 loc) · 3.74 KB
/
test_interop_node.js
File metadata and controls
127 lines (108 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Cross-platform interop test: Node.js side
* 1. Read Python output and verify its signature
* 2. Generate keys, sign a message, export for Python verification
*/
'use strict';
const fs = require('fs');
const path = require('path');
const mcps = require('./index.js');
const INTEROP_FILE = '/tmp/mcps_interop.json';
function phase1_verify() {
const data = JSON.parse(fs.readFileSync(INTEROP_FILE, 'utf8'));
if (!data.python_envelope) {
console.log('NODE PHASE 1: No Python data found, skipping');
return false;
}
const pyPub = data.python_public_key;
const pyEnvelope = data.python_envelope;
const pyTool = data.python_tool;
const pyToolSig = data.python_tool_signature;
// Verify Python signed message
const result = mcps.verifyMessage(pyEnvelope, pyPub);
if (result.valid) {
console.log('NODE PHASE 1: Python message verification: PASS');
} else {
console.log('NODE PHASE 1: Python message verification: FAIL -', JSON.stringify(result));
return false;
}
// Verify Python signed tool
const toolResult = mcps.verifyTool(pyTool, pyToolSig, pyPub);
if (toolResult.valid) {
console.log('NODE PHASE 1: Python tool verification: PASS');
} else {
console.log('NODE PHASE 1: Python tool verification: FAIL -', JSON.stringify(toolResult));
return false;
}
// Tamper test
const tampered = JSON.parse(JSON.stringify(pyEnvelope));
tampered.params.name = 'delete_everything';
const tamperResult = mcps.verifyMessage(tampered, pyPub);
if (!tamperResult.valid) {
console.log('NODE PHASE 1: Tamper detection on Python message: PASS');
} else {
console.log('NODE PHASE 1: Tamper detection on Python message: FAIL - should have detected tamper');
return false;
}
return true;
}
function phase2_sign() {
// Generate Node.js keys
const keys = mcps.generateKeyPair();
const passport = mcps.createPassport({
name: 'node-agent',
version: '1.0.0',
publicKey: keys.publicKey,
});
// Sign a message
const message = {
jsonrpc: '2.0',
method: 'tools/call',
params: { name: 'read_file', arguments: { path: '/etc/hosts' } },
id: 42,
};
const envelope = mcps.signMessage(message, passport.passport_id, keys.privateKey);
// Sign a tool
const tool = {
name: 'read_file',
description: 'Read a file from disk',
inputSchema: {
type: 'object',
properties: { path: { type: 'string' } },
required: ['path'],
},
};
const { signature: toolSig } = mcps.signTool(tool, keys.privateKey);
// Self-verify
const selfResult = mcps.verifyMessage(envelope, keys.publicKey);
if (!selfResult.valid) {
console.log('NODE PHASE 2: Self-verify FAIL -', JSON.stringify(selfResult));
process.exit(1);
}
const toolSelfResult = mcps.verifyTool(tool, toolSig, keys.publicKey);
if (!toolSelfResult.valid) {
console.log('NODE PHASE 2: Tool self-verify FAIL -', JSON.stringify(toolSelfResult));
process.exit(1);
}
// Read existing and merge
const data = JSON.parse(fs.readFileSync(INTEROP_FILE, 'utf8'));
data.node_public_key = keys.publicKey;
data.node_envelope = envelope;
data.node_tool = tool;
data.node_tool_signature = toolSig;
data.node_passport_id = passport.passport_id;
fs.writeFileSync(INTEROP_FILE, JSON.stringify(data, null, 2));
console.log('NODE PHASE 2: Keys generated, message signed, tool signed');
console.log(` Passport ID: ${passport.passport_id}`);
console.log(` Envelope nonce: ${envelope.mcps.nonce}`);
console.log(' Self-verify: PASS');
console.log(' Tool self-verify: PASS');
}
// Run
const pyVerified = phase1_verify();
phase2_sign();
if (!pyVerified) {
console.log('\n--- RESULT: Python verification FAILED ---');
process.exit(1);
}
console.log('\n--- NODE COMPLETE: All checks passed ---');