|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from 'node:fs'; |
| 18 | +import path from 'node:path'; |
| 19 | + |
| 20 | +/** A legal document declared in the application build manifest. */ |
| 21 | +export interface LegalDocument { |
| 22 | + /** Stable identifier used when requesting the document over IPC. */ |
| 23 | + id: string; |
| 24 | + /** Human-readable title shown in the application. */ |
| 25 | + title: string; |
| 26 | + /** Resource filename relative to the packaged application resources directory. */ |
| 27 | + file: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** Public metadata for a legal document exposed to the renderer. */ |
| 31 | +export type LegalDocumentSummary = Pick<LegalDocument, 'id' | 'title'>; |
| 32 | + |
| 33 | +/** Result returned when the renderer requests legal document content. */ |
| 34 | +export interface LegalDocumentResult { |
| 35 | + /** Whether the requested document was read successfully. */ |
| 36 | + success: boolean; |
| 37 | + /** Document text when the request succeeds. */ |
| 38 | + content?: string; |
| 39 | + /** User-facing failure reason when the request fails. */ |
| 40 | + error?: string; |
| 41 | +} |
| 42 | + |
| 43 | +const VALID_ID = /^[a-z0-9][a-z0-9-]{0,63}$/; |
| 44 | + |
| 45 | +/** |
| 46 | + * Validates legal documents from parsed application build metadata. |
| 47 | + * |
| 48 | + * @param packageConfig - Parsed application build manifest content. |
| 49 | + * @returns Valid manifest-declared legal documents. |
| 50 | + */ |
| 51 | +export function getLegalDocuments(packageConfig: unknown): LegalDocument[] { |
| 52 | + if (typeof packageConfig !== 'object' || packageConfig === null) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + const configuredDocuments = (packageConfig as { legalDocuments?: unknown }).legalDocuments; |
| 57 | + |
| 58 | + if (!Array.isArray(configuredDocuments)) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + return configuredDocuments.filter((document): document is LegalDocument => { |
| 63 | + if (typeof document !== 'object' || document === null) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + const { id, title, file } = document as Partial<LegalDocument>; |
| 67 | + return ( |
| 68 | + typeof id === 'string' && |
| 69 | + VALID_ID.test(id) && |
| 70 | + typeof title === 'string' && |
| 71 | + title.length > 0 && |
| 72 | + title.length <= 128 && |
| 73 | + typeof file === 'string' && |
| 74 | + file.length > 0 && |
| 75 | + file.length <= 255 && |
| 76 | + !file.includes('/') && |
| 77 | + !file.includes('\\') && |
| 78 | + file !== '.' && |
| 79 | + file !== '..' |
| 80 | + ); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Loads legal document declarations from an application build manifest. |
| 86 | + * |
| 87 | + * @param packagePath - Path to the application build manifest JSON file. |
| 88 | + * @returns Valid legal document declarations, or an empty array when loading fails. |
| 89 | + */ |
| 90 | +export function loadLegalDocuments(packagePath: string): LegalDocument[] { |
| 91 | + try { |
| 92 | + return getLegalDocuments(JSON.parse(fs.readFileSync(packagePath, 'utf8'))); |
| 93 | + } catch { |
| 94 | + return []; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Reads one configured legal document from packaged application resources. |
| 100 | + * |
| 101 | + * @param resourcesPath - Root directory containing packaged resources. |
| 102 | + * @param documents - Valid legal document declarations. |
| 103 | + * @param id - Untrusted document identifier received over IPC. |
| 104 | + * @returns Document content on success, otherwise a stable failure result. |
| 105 | + */ |
| 106 | +export function readLegalDocument( |
| 107 | + resourcesPath: string, |
| 108 | + documents: LegalDocument[], |
| 109 | + id: unknown |
| 110 | +): LegalDocumentResult { |
| 111 | + const document = typeof id === 'string' ? documents.find(item => item.id === id) : undefined; |
| 112 | + if (!document) { |
| 113 | + return { success: false, error: 'Unknown legal document' }; |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + const filePath = path.resolve(resourcesPath, document.file); |
| 118 | + const relativePath = path.relative(path.resolve(resourcesPath), filePath); |
| 119 | + if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) { |
| 120 | + return { success: false, error: 'Invalid legal document path' }; |
| 121 | + } |
| 122 | + return { success: true, content: fs.readFileSync(filePath, 'utf8') }; |
| 123 | + } catch { |
| 124 | + return { success: false, error: 'Unable to read legal document' }; |
| 125 | + } |
| 126 | +} |
0 commit comments