Copilot commented on code in PR #71144:
URL: https://github.com/apache/airflow/pull/71144#discussion_r3773763521


##########
ts-sdk/src/cli/pack.ts:
##########
@@ -143,20 +143,52 @@ function readBundleManifest(bundlePath: string): 
BundleManifest {
     throw new Error(`Bundle produced no ${AIRFLOW_METADATA_FLAG} output`);
   }
 
-  let manifest: BundleManifest;
+  let parsed: unknown;
   try {
-    manifest = JSON.parse(line.slice(AIRFLOW_METADATA_SENTINEL.length)) as 
BundleManifest;
+    parsed = JSON.parse(line.slice(AIRFLOW_METADATA_SENTINEL.length));
   } catch (error) {
     throw new Error(`Bundle produced invalid ${AIRFLOW_METADATA_FLAG} output: 
${String(error)}`, {
       cause: error,
     });
   }
-  if (!manifest.supervisor_schema_version || !manifest.dags || typeof 
manifest.dags !== "object") {
+  if (!isBundleManifest(parsed)) {
     throw new Error(`Bundle produced incomplete ${AIRFLOW_METADATA_FLAG} 
output`);
   }
+  const manifest = parsed;
+  // The line is whatever the bundle printed and nothing downstream 
re-validates
+  // it, so check each Dag entry down to the task-id element.
+  for (const [dagId, dag] of Object.entries(manifest.dags)) {
+    if (dag == null || !isDagIdList(dag.tasks)) {
+      throw new Error(
+        `Bundle produced ${AIRFLOW_METADATA_FLAG} output with a malformed 
entry for Dag "${dagId}"`,
+      );
+    }
+  }
   return manifest;
 }
 
+// The document is checked before anything is read off it: JSON.parse also 
yields
+// null and primitives, and `null.supervisor_schema_version` would surface as a
+// raw TypeError rather than a report about the bundle.
+function isBundleManifest(value: unknown): value is BundleManifest {
+  if (typeof value !== "object" || value === null || Array.isArray(value)) 
return false;
+  const { supervisor_schema_version: version, dags } = value as 
Partial<BundleManifest>;
+  return (
+    // Rendered into the manifest verbatim, where the schema requires a 
non-empty
+    // string, so a truthy number or boolean would travel to Airflow as-is.
+    typeof version === "string" &&
+    version.length > 0 &&
+    typeof dags === "object" &&
+    dags !== null &&
+    // An array would pass the typeof check and yield Dags named "0", "1", ...
+    !Array.isArray(dags)
+  );
+}
+
+function isDagIdList(value: unknown): value is string[] {
+  return Array.isArray(value) && value.every((item) => typeof item === 
"string" && item.length > 0);
+}

Review Comment:
   `isDagIdList` is validating a list of *task IDs* (`dag.tasks`), not Dag IDs. 
Renaming it (and its call site in readBundleManifest) would make the intent 
clearer and avoid confusion when reading the schema validation logic.



##########
ts-sdk/src/sdk/dag.ts:
##########
@@ -0,0 +1,241 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// The Dag authoring surface: `new Dag(dagId)` plus `dag.task(taskId, 
handler)`.
+
+import { brand, hasBrand } from "./brand.js";
+import type { TaskHandler } from "./task.js";
+
+function validateEmptySpec(name: string, value: unknown): void {
+  if (
+    typeof value !== "object" ||
+    value === null ||
+    Array.isArray(value) ||
+    Reflect.ownKeys(value).length > 0
+  ) {
+    throw new Error(`${name} must be an empty object`);
+  }
+}

Review Comment:
   validateEmptySpec currently treats any object with zero own keys as an 
“empty object”, so values like `new Date()` (or other non-plain objects) are 
accepted for Dag/task `spec` even though they are not `{}`. Since callers can 
bypass TypeScript via JS or casts, this should reject non-plain objects to 
avoid silently accepting invalid specs.
   
   This issue also appears in the following locations of the same file:
   - line 195
   - line 206



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to