nehonix-uri-processor
Version:
A powerful URI processor for encoding, decoding, and analyzing URI data securely.
236 lines • 12.2 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useContext, useCallback, useMemo } from "react";
import { NehonixShieldContext } from "../context/REACT.ShieldContext";
import { NehonixDomProcessorContext, NehonixDomProcessorProvider, } from "../provider/REACT.NehonixDomProcessor";
import { NehonixShieldProvider } from "../provider/REACT.NehonixShield";
/**
* Combined hook for accessing Nehonix security features
* This hook provides a unified interface to both NehonixShield and NehonixDomProcessor
*
* @returns Combined security context with methods and properties from both systems
*/
export const useNehonixShieldPlugging = () => {
const shield = useContext(NehonixShieldContext);
const domProcessor = useContext(NehonixDomProcessorContext);
// Check if contexts are available
if (!shield) {
throw new Error("useNehonixShieldPlugging must be used within a NehonixShieldProvider");
}
if (!domProcessor) {
throw new Error("useNehonixShieldPlugging must be used within a NehonixDomProcessorProvider");
}
/**
* Force scan in both systems
*/
const forceFullScan = useCallback(() => {
shield.forceScan();
domProcessor.scanDOM();
}, [shield, domProcessor]);
/**
* Reset all stats and results
*/
const resetAllStats = useCallback(() => {
shield.clearResults();
domProcessor.resetStats();
}, [shield, domProcessor]);
/**
* Combined configuration updater
*/
const updateConfig = useCallback((shieldConfig, domConfig) => {
if (shieldConfig) {
shield.updateConfig(shieldConfig);
}
if (domConfig) {
domProcessor.updateConfig(domConfig);
}
}, [shield, domProcessor]);
/**
* Get comprehensive security report combining both systems
*/
const getComprehensiveReport = useCallback(() => {
const currentTimestamp = Date.now();
return {
shield: {
config: shield.config,
results: shield.analysisResults,
},
domProcessor: domProcessor.getSecurityReport(),
timestamp: currentTimestamp,
summary: {
totalThreatsDetected: shield.analysisResults.totalBlocked +
domProcessor.stats.threatsDetected,
isActive: shield.config.enableBackgroundScanning && domProcessor.config.enabled,
lastScanTime: Math.max(shield.analysisResults.lastScanTimestamp, domProcessor.stats.lastScanTimestamp),
overallStatus: shield.analysisResults.totalBlocked +
domProcessor.stats.threatsDetected >
0
? "threats-detected"
: "secure",
scanCoverage: {
domElementsScanned: domProcessor.stats.elementsScanned,
urlsScanned: shield.analysisResults.totalScanned,
},
},
};
}, [shield, domProcessor]);
/**
* Get simple security status
*/
const getSecurityStatus = useCallback(() => {
return {
isSecure: shield.analysisResults.totalBlocked +
domProcessor.stats.threatsDetected ===
0,
activeThreats: [
...shield.analysisResults.activeThreats,
...domProcessor.stats.blockedElements.map((el) => ({
type: el.threatTypes[0],
severity: "high",
timestamp: el.timestamp,
source: `DOM element (${el.elementType})`,
metadata: el.metadata, // Include metadata
})),
],
lastScanTime: Math.max(shield.analysisResults.lastScanTimestamp, domProcessor.stats.lastScanTimestamp),
scanning: shield.isScanning || domProcessor.stats.scanningActive,
};
}, [shield, domProcessor]);
/**
* Pause all scanning activities
*/
const pauseAllScanning = useCallback(() => {
shield.pauseScanning();
domProcessor.stopScanning();
}, [shield, domProcessor]);
/**
* Resume all scanning activities
*/
const resumeAllScanning = useCallback(() => {
shield.resumeScanning();
if (domProcessor.config.enabled) {
domProcessor.scanDOM();
}
}, [shield, domProcessor]);
/**
* Combined stats object
*/
const combinedStats = useMemo(() => {
const shieldStats = shield.analysisResults;
const domStats = domProcessor.stats;
return {
elementsScanned: domStats.elementsScanned,
urlsScanned: shieldStats.totalScanned,
threatsDetected: shieldStats.totalBlocked + domStats.threatsDetected,
lastScanTimestamp: Math.max(shieldStats.lastScanTimestamp, domStats.lastScanTimestamp),
scanDuration: domStats.scanDuration,
scanningActive: shield.isScanning || domStats.scanningActive,
elementTypeStats: domStats.elementTypeStats,
threatsByType: {
...domStats.threatsByType,
// Could merge with shield threat types if available
},
blockedElements: [
...domStats.blockedElements,
// Could add shield blocked URLs in a compatible format
],
performance: {
avgElementScanTime: domStats.avgProcessingTimePerElement,
avgUrlScanTime: shieldStats.performanceMetrics.avgScanTime,
},
};
}, [shield.analysisResults, domProcessor.stats]);
// Return the combined interface
return {
// Original contexts for direct access if needed
shield,
domProcessor,
// Combined methods
forceFullScan,
resetAllStats,
getComprehensiveReport,
getSecurityStatus,
updateConfig,
pauseAllScanning,
resumeAllScanning,
// Combined properties
stats: combinedStats,
isScanning: shield.isScanning || domProcessor.stats.scanningActive,
};
};
/**
* Combined provider component for Nehonix security plugging
* This is a convenience wrapper that sets up both security contexts
*/
export const NehonixShieldProviderPlugging = ({ children, shieldConfig = {}, domProcessorConfig = {} }) => {
return (_jsx(NehonixShieldProvider, { initialConfig: shieldConfig, children: _jsx(NehonixDomProcessorProvider, { initialConfig: domProcessorConfig, children: children }) }));
};
/**
* Higher-order component that injects the Nehonix security context
*
* @param Component The component to wrap
* @returns A new component with the security context injected as a prop
*/
export const withNehonixShieldPlugging = (Component) => {
return (props) => {
const security = useNehonixShieldPlugging();
return _jsx(Component, { ...props, security: security });
};
};
/**
* Security badge component - shows current security status
*/
export const NehonixShieldPluggingBadge = ({ variant = "standard", className = "", style = {} }) => {
const security = useNehonixShieldPlugging();
const securityStatus = security.getSecurityStatus();
const getStatusColor = () => {
if (securityStatus.scanning)
return "#FFA500"; // Orange for scanning
return securityStatus.isSecure ? "#4CAF50" : "#FF0000"; // Green for secure, red for threats
};
const getBadgeContent = () => {
switch (variant) {
case "minimal":
return (_jsxs("div", { style: { display: "flex", alignItems: "center" }, children: [_jsx("div", { style: {
width: 10,
height: 10,
borderRadius: "50%",
backgroundColor: getStatusColor(),
marginRight: 5,
} }), securityStatus.isSecure ? "Secure" : "Alert"] }));
case "detailed":
return (_jsxs("div", { children: [_jsxs("div", { style: { display: "flex", alignItems: "center" }, children: [_jsx("div", { style: {
width: 10,
height: 10,
borderRadius: "50%",
backgroundColor: getStatusColor(),
marginRight: 5,
} }), _jsx("strong", { children: securityStatus.isSecure ? "Secure" : "Threats Detected" })] }), _jsxs("div", { style: { fontSize: "0.8em", marginTop: 5 }, children: [_jsxs("div", { children: ["Elements scanned: ", security.stats.elementsScanned] }), _jsxs("div", { children: ["URLs scanned: ", security.stats.urlsScanned] }), securityStatus.activeThreats.length > 0 && (_jsxs("div", { children: ["Active threats: ", securityStatus.activeThreats.length, _jsx("ul", { style: { margin: "5px 0", paddingLeft: 20 }, children: securityStatus.activeThreats.map((threat, index) => (_jsxs("li", { children: [_jsx("strong", { children: "Type:" }), " ", threat.type || "Unknown", _jsx("br", {}), _jsx("strong", { children: "Source:" }), " ", threat.source || "N/A", _jsx("br", {}), threat.metadata && (_jsxs(_Fragment, { children: [_jsx("strong", { children: "DOM Location:" }), " ", threat.metadata.domLocation.substring(0, 100) +
(threat.metadata.domLocation.length > 100
? "..."
: ""), _jsx("br", {}), threat.metadata.elementId && (_jsxs(_Fragment, { children: [_jsx("strong", { children: "ID:" }), " ", threat.metadata.elementId, _jsx("br", {})] })), threat.metadata.className && (_jsxs(_Fragment, { children: [_jsx("strong", { children: "Class:" }), " ", threat.metadata.className, _jsx("br", {})] })), threat.metadata.parentElement && (_jsxs(_Fragment, { children: [_jsx("strong", { children: "Parent:" }), " ", threat.metadata.parentElement.tagName, threat.metadata.parentElement.id
? ` (#${threat.metadata.parentElement.id})`
: "", _jsx("br", {})] })), _jsx("strong", { children: "DOM Path:" }), " ", threat.metadata.domPath, _jsx("br", {}), threat.metadata.innerTextSnippet && (_jsxs(_Fragment, { children: [_jsx("strong", { children: "Inner Text:" }), " ", threat.metadata.innerTextSnippet, _jsx("br", {})] }))] }))] }, index))) })] }))] }), _jsx("button", { onClick: security.forceFullScan, style: {
marginTop: 5,
padding: "2px 5px",
fontSize: "0.8em",
}, children: "Scan Now" })] }));
default: // standard
return (_jsxs("div", { style: { display: "flex", alignItems: "center" }, children: [_jsx("div", { style: {
width: 10,
height: 10,
borderRadius: "50%",
backgroundColor: getStatusColor(),
marginRight: 5,
} }), _jsxs("div", { children: [_jsx("div", { children: securityStatus.isSecure
? "Secure"
: `Threats: ${securityStatus.activeThreats.length}` }), _jsx("div", { style: { fontSize: "0.8em" }, children: securityStatus.scanning ? "Scanning..." : "Idle" })] })] }));
}
};
return (_jsx("div", { className: `nehonix-security-badge ${className}`, style: {
padding: 10,
border: `1px solid ${getStatusColor()}`,
borderRadius: 4,
...style,
}, children: getBadgeContent() }));
};
//# sourceMappingURL=REACT.NehonixDomPlugging.hooks.js.map