All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.artifact.enrichment.vulnerability.ghsa.matchers.NpmGhsaMatcher Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed 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.
 */
package com.metaeffekt.artifact.enrichment.vulnerability.ghsa.matchers;

import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.metaeffekt.artifact.analysis.utils.StringUtils;
import com.metaeffekt.artifact.analysis.vulnerability.enrichment.InventoryAttribute;
import com.metaeffekt.artifact.enrichment.vulnerability.ghsa.ArtifactIdVersionGhsaMatcher;
import com.metaeffekt.artifact.enrichment.vulnerability.ghsa.GhsaEcosystem;
import com.metaeffekt.mirror.query.GhsaAdvisorIndexQuery;
import org.metaeffekt.core.inventory.processor.model.Artifact;
import org.metaeffekt.core.inventory.processor.model.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NpmGhsaMatcher extends ArtifactIdVersionGhsaMatcher {

    private final static Logger LOG = LoggerFactory.getLogger(NpmGhsaMatcher.class);

    public NpmGhsaMatcher(GhsaAdvisorIndexQuery ghsaQuery, boolean githubReviewed) {
        super(ghsaQuery, githubReviewed);
    }

    @Override
    protected GhsaEcosystem getEcosystem() {
        return GhsaEcosystem.NPM;
    }

    @Override
    public boolean mayMatch(Artifact artifact) {
        return super.mayMatch(artifact) && isNpmModuleOrAlike(artifact);
    }

    private boolean isNpmModuleOrAlike(Artifact artifact) {
        final String type = artifact.get(Constants.KEY_TYPE);
        return isTypeMatch(type) || isNpmPurl(artifact) || isNpmEcosystem(artifact);
    }

    private boolean isNpmEcosystem(Artifact artifact) {
        boolean isNpmEcosystem = false;
        String ecosystem = artifact.get(InventoryAttribute.ECOSYSTEM);
        if (StringUtils.hasText(ecosystem)) {
            isNpmEcosystem = PackageURL.StandardTypes.NPM.equalsIgnoreCase(ecosystem);
        }
        return isNpmEcosystem;
    }

    private boolean isNpmPurl(Artifact artifact) {
        boolean isNpmPurl = false;
        String purlString = artifact.get(Artifact.Attribute.PURL);
        if (StringUtils.hasText(purlString)) {
            try {
                PackageURL purl = new PackageURL(purlString);
                isNpmPurl = PackageURL.StandardTypes.NPM.equalsIgnoreCase(purl.getType());
            } catch (MalformedPackageURLException e) {
                LOG.debug("Cannot extract package url from [{}].", purlString);
            }
        }
        return isNpmPurl;
    }

    private boolean isTypeMatch(String type) {
        // FIXME: revise ArtifactType again; identify different questions
        boolean isTypeMatch = Constants.ARTIFACT_TYPE_NODEJS_MODULE.equalsIgnoreCase(type) ||
                Constants.ARTIFACT_TYPE_WEB_MODULE.equalsIgnoreCase(type);
        return isTypeMatch;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy